The quick sort was developed by C.a.r.hoare (1962), which selects an element called the main element (pivot) in the array, divides the array into two parts, and all the elements in the first part are less than or equal to the primary, and all the elements of the second part are larger than the primary. Apply fast sorting algorithm recursively to the first part, then apply fast sorting algorithm recursively to the second part.
Package ss.sort;/** * Quick Sort * @author zhangss 2016-5-3 11:42:32 * */public class QuickSort {public static void QuickSort (in T[] list) {quickSort (list, 0, list.length-1);} private static void QuickSort (int[] list, int first, int last) {if (last > first) {int pivotindex = partition (list, first, QuickSort (list, first, pivotIndex-1), QuickSort (list, Pivotindex + 1, last);}} private static int partition (int[] list, int first, int last) {int pivot = List[first]; Choose the first element as the pivotint low = first + 1; Index for forward searchint = last; Index for backward Searchwhile (High > Low) {//Search forward from leftwhile (Low <= high && List[low] < = pivot) low++;//Search forward from rightwhile (Low <= high && List[high] > Pivot) high--;//Swap, Element s in the ListIf (High > low) {int temp = List[high];list[high] = List[low];list[low] = temp;}} while (High > First && list[high] >= pivot) high--;//Swap PivotWith list[high]if (Pivot > List[high]) {List[first] = List[high];list[high] = Pivot;return high;} Else{return first;}} /** * A test method */public static void Main (string[] args) {int[] list = {2, 3, 2, 5, 6, 1,-2, 3, +, 12};quicksort (LIS t); for (int i = 0; i < list.length; i++) System.out.print (List[i] + "");}}
Sort-quick Sort