First, the idea of rapid sequencing
Based on the idea of divided treatment, it is an improved type of bubble sort. First select a datum point in the array (the selection of the datum point may affect the efficiency of the quick sort, followed by the selected method), and then scan the array from both ends of the array, set two indicator flags (LO points to start position, hi to end), first starting from the second half, if the element is found to be smaller than the Datum point, The values for the lo and hi positions are exchanged, and then the first half is scanned for seconds, and the values of the elements greater than the datum points are exchanged, and the value of the lo and hi positions is swapped, so that the loop is repeated until Lo>=hi, and the value of the datum point is placed at the hi position. Once the sort is done. In the future, the first and second halves are sorted by recursion, and the array is naturally ordered when the current half and the second half are ordered.
Sorting process:
Second, let's look at a specific example:
Here's a case to illustrate the basic steps for quick sorting: Sequence 46 30 82 90 56 17 95 15 Total 8 elements
Initial state: 46 30 82 90 56 17 95 15 Select 46 as the reference value, i = 0, j = 7
i = 0 J = 7
46, 15 and 46, mobile I, I = 1, at
i = 1 J = 7
2-46, no swap required, moving i, i =
i = 2 J = 7
46, Exchange 82 and 46, move J, j = 6.
i = 2 J = 6
46, no swap, move J, j = 5, All
i = 2 J = 5
46, 46 and 17, mobile I, I = 3, from
i = 3 J = 5
46, Exchange 90 and 46, move J, j = 4.
3 = i j = 4
46, no swap, move J, j = 3, all
i = j = 3
i = j = 3, so that the sequence is divided into two parts, the left part {15, 30, 17} is less than the base value (46), and the right part {56, 90,95,82} is greater than the base value. In this way we reach the goal of the split sequence. Then the subsequence is split in the same way until the subsequence is no more than one element, then the order ends and the entire sequence is in an orderly state.
Third, the implementation of the fast sorting algorithm:
public static int Getmiddle (int[] list, int. low, int high) {int tmp = list[low];//Array's first as a middle axis while (low < high) {while (lo W < high && List[high] >= tmp) {high--;} List[low] = list[high];//smaller than the middle axis of the record moved to the low side while (lower < high && List[low] <= tmp) {low++;} List[high] = list[low];//larger than mid-axis record moved to high-end}list[low] = tmp;//pivot record to end return low;//return the position of the middle axis}public static void _quicksort (int[] list, int low, int.) {if (Low < high) {int middle = getmiddle (list, low, high),//To divide the list array into _quicksort (list, low, M IDDLE-1);//recursive Ordering of low-word tables _quicksort (list, middle + 1, high);//recursive ordering of tall character tables}}
The position of the so-called Central axis is the position of the selected base number.
When used, call the _quicksort method directly, the low is the subscript of the first element of the array , and the subscript of the last element of the array is set to A.length-1. 0 .
The time complexity for quick sorting is O (NLOGN).
Java enables quick sorting