Quick sort public class Quick_sort {///Sort main algorithm private int Partition (int[] data, int start, int end) {int mid_data = data[end];/ /Select the last number as the median sentinel, from the beginning of the traversal, each number compared with the int index = start; The number of records smaller than the Sentinel at the left end of the position or number//note to consider the case of start = 0, and the "Introduction to the algorithm," the pseudo-code has a difference for (int i = start; i < end-1; i++) {//To determine the size relationship of each value to the Sentinel, less than or equal to the The left index position, and the index++//can be understood as the number of values less than Sentinel by index, and all the less than or equal to the number by swapping over the IF (Data[i] <= mid_data) {Swap (data, I, index); index++;}} Move the sentry to the middle, because Data[index] is certainly greater than mid_data, so move to the end still meet the requirements of the algorithm swap (data, index, end); return index; Divide and conquer public void QuickSort (int[] data, int start, int end) {if (Start < end) {///home order, find middle value divide array into half, divide and conquer to continue recursively int mid = Pa Rtition (data, start, end); QuickSort (data, start, mid-1); QuickSort (data, mid + 1, end);}} Cannot use reference implementations like C + +, so it has to be changed with the data array private void Swap (int[] data, int a, int b) {int temp = Data[a];d ata[a] = data[b];d ata[b] = temp;} public void Print_array (int[] data) {for (int num:data) {System.out.print (num); System.out.print ("");}} public static void Main (string[] args) {inT data[] = {2, 34, 45, 2, 13, 24, 5, 24, 57}; Quick_sort quick_sort = new Quick_sort () quick_sort.quicksort (data, 0, data.length-1);//Note The parameter value of the initial call Quick_sort.print_ Array (data);}}
Quick ordering of sorting algorithms (Java)