1 /*Hill Sort: An improvement to the insertion sort, which is sorted according to an increment sequence2 * The number of incremental sequences is the number of sequential trips. Under any increment K, guarantee a[i]<=a[i+k]3 * The efficiency of the algorithm and the selection of incremental need sequence are very important. 4 * Incremental Sequence general rule: h = 3h + 1;5 * The efficiency of the hill sort is generally better than the simple sort. 6 * */7 Public classShellsort {8 9 Public Static voidMain (string[] args) {Ten int[] List = {6,9,2,3,5,50,30,8,7,4,23,12}; One displaylist (list); A Shellsort (list); - displaylist (list); - } the - Public Static voidShellsort (int[] arr) { - intInner,outer; - inttemp; + intH = 1; - while(H <= ARR.LENGTH/3){ +H = h * 3 + 1; A } at //Outer loop control sequencing number of trips - while(H > 0){ - //control loops for each increment - for(outer = h; outer < arr.length; outer++){ -temp =Arr[outer]; -Inner =outer; in - while(Inner > H-1 && arr[inner-h] >=temp) { toArr[inner] = Arr[inner-h]; +Inner = inner-h; - } theArr[inner] =temp; * } $h = (h-1)/3;Panax Notoginseng } - } the + Public Static voidDisplaylist (int[] arr) { A for(inti = 0; I < arr.length;i++){ theSystem.out.print (Arr[i] + ""); + } - System.out.println (); $ } $ -}
1 /*Quick sort: Divides an array into two sub-arrays, recursively calling itself to quickly sort each sub-array. 2 * Note: To determine a good recursive termination condition right-left <= 03 * Each division: the boundary of the next group is determined according to the thought of division4 Divide the mind: divide the sequence into two parts according to a value in the sequence, and the right value is all less than the portion to the left of the value.5 * The selection of this value is as close as possible to the average of the sequence of the group. By setting the two pointers, you find a greater than the median value in the opposite direction, respectively.6 * and smaller than the median value, swap, and finally put the median value in its position. 7 Quick sort: Usually a better sorting algorithm O (NLOGN)8 Central value selection key, preferably on both sides of the length is almost good, if the two sides of the more bad, will lead to more than one side too much division9 * efficiency is reduced, sequence disorder is better--for randomly selected central valuesTen * */ One Public classQuickSort { A - Public Static voidMain (string[] args) { - int[] List = {3,2,5,7,5,38,20,18,27,39,12}; the displaylist (list); - QuickSort (list); - displaylist (list); - } + - Public Static voidQuickSort (int[] arr) { +Recquicksort (0,arr.length-1, arr); A } at - Private Static voidRecquicksort (intLeftintRightint[] arr) { - if(right-left <= 0) {//size<=1. Recursive datum conditions - return; - } - Else{ in intPivot =Arr[right]; - //Tidy up the small one side big on the side and get the next grouped border to intPartition =Partitionit (Left,right,pivot,arr); +Recquicksort (left, partition-1, arr); -Recquicksort (partition + 1, right, arr); the } * $ }Panax Notoginseng - Public Static intPartitionit (intLeftintRightintPivotint[] arr) { the intLefter = left-1;//left of first element + intRighter = right;//to the right of the last element A the while(true){ + while(Arr[++lefter] < pivot);//left until you find an element that is larger than the median value - $ while(Righter > 0 && arr[--righter] > Pivot);//right until you find an element that is smaller than the middle value $ - if(Lefter >=righter) { - Break; the } - Else{WuyiSwap (Lefter,righter,arr);//exchange of two elements the } - } WuSwap (Lefter,right,arr);//Places the selected median value in its position - returnLefter; About $ } - - Private Static voidSwapintLefter,intRighter,int[] list) { - inttemp =List[lefter]; AList[lefter] =List[righter]; +List[righter] =temp; the } - $ Public Static voidDisplaylist (int[] arr) { the for(inti = 0; I < arr.length;i++){ theSystem.out.print (Arr[i] + ""); the } the System.out.println (); - } in the}
Data Structures--hill sort and quick sort