Fast sorting of algorithm notes
1.1Algorithm concept --
This algorithm selects an element in the array as the principal element (usually the first one), and then uses this principal element as the reference object to divide the array into two parts. The first part is less than or equal to the principal element, the second part is greater than or equal to the principal component. Use the quick sorting algorithm recursively for the first and second parts until they are assigned to the smallest group.
1.2Time Complexity --
In the worst case, we need to divide the arrays of n elements, which requires n comparisons and n moves. Suppose T (n) is used to represent the time it takes to sort arrays of n elements using a fast sorting algorithm. So
T (n) = T (n/2) + T (n/2) + 2n
Therefore, the time complexity of fast sorting should be T (n) = O (nlogn ).
1.3Program flowchart
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel + sLrPC9zdHJvbmc + pc9wpgo8cd48c3ryb25np1_vc3ryb25npjxwcmugy2xhc3m9 "brush: java;"> package lin. algo; public class QuickSort {int [] list; public QuickSort (int [] list) {this. list = list;} public int [] quickSort () {quick (list, 0, list. length-1); return list;} private void quick (int [] list, int first, int last) {if (last> first) {int second xindex = partition (list, first, last); quick (list, first, pivoxIndex-1); quick (list, distinct xindex + 1, last );}} /*** divide the Array Based on the first element. * The input is the original array to be divided, the first element subscript and the last element subscript * @ param list * @ param first * @ param last * @ return the principal component subscript after the grouping */private int partition (int [] list, int first, int last) {// principal element int privot = list [first]; int secondIndex = first + 1; int lastIndex = last; // traverses the query, greater than the principal component on the right, less than on the left // implementation method: Check the two sides, the left is greater than the principal component and the right is less than the principal component of the exchange, // put the principal component in the middle of the two groups while (lastIndex> secondIndex) {// find the first while (secondIndex <= lastIndex & list [secondIndex] <= privot) on the left that is larger than the primary element {secondIndex ++ ;} // find the first while (secondIndex <= lastIndex & list [lastIndex]> privot) {lastIndex --;} on the right, if the sequence is not checked, it indicates that the sequence is found separately. if (secondIndex <lastIndex) {int temp = list [secondIndex]; list [secondIndex] = list [lastIndex]; list [lastIndex] = temp ;}} while (list [lastIndex] >=privot & lastIndex> first) {lastIndex --;} // return the new subscript of the primary element if (privot> list [lastIndex]) {list [first] = list [lastIndex]; list [lastIndex] = privot; return lastIndex ;} else {return first ;}}}