package com.test.sort;/** * <Description> <br> * * @author liu.mingqing<br> * @version 1.0<br> * @taskId <br> * @CreateDate 2017年2月4日 <br> * @since V7.3<br> * @see com.test.sort <br> */public class QuickSort { /** * Description: <br> * * @author liu.mingqing<br> * @taskId <br> * @param args <br> */ public static void main(String[] args) { Integer[] test = {305,65,7,90,120,110,8}; quickSort(test,0,6); System.out.PRintln(test.toString()); } public static <T extends Comparable<T>> void quickSort(T[] data, int min, int max) { if (min < max) { int indexOfpartion = partion(data, min, max); quickSort(data,min,indexOfpartion); quickSort(data, indexOfpartion+1,max); } } /** * * Description: <br> * * @author liu.mingqing<br> * @taskId <br> * @param data * @param min * @param max <br> */ public static <T extends Comparable<T>> int partion(T[] data, int min, int max) { int mid = (min+max)/2; T partionElement = data[mid]; int first = min,last = max; swap(data,min,mid); while(first < last) { while (first < last && data[first].compareTo(partionElement) <= 0) { first++; } while (data[last].compareTo(partionElement) > 0) { last--; } if (first < last) swap(data,first,last); } swap(data,min,last); return last; } public static <T extends Comparable<T>> void swap(T[] data, int m, int n ) { if (m >= data.length || n >= data.length) return; T temp = data[m]; data[m] = data[n]; data[n] = temp; }}
新闻热点
疑难解答