1. Hill sort
Hill sort is an improved version of Quick Insert Sort, the hill sort is to group records by a certain increment of subscript, sort by using the direct insertion sorting algorithm for each group; As the increment decreases, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the entire file is divided into a group, the algorithm terminates
Basic idea: First take an integer less than n D1 as the first increment, grouping all the records in the file. All records with a multiple of D1 are placed in the same group. First, the direct insertion sort is performed within each group, and then the second increment d2<d1 repeats the above groupings and sorts until the increment =1 ( < ... <d2<d1) is taken. That is, all records are placed in the same group for the direct insertion of the sort, generally select D1 is half of the array, D2 is half of the D1 and so on.
Specific code:
PackageSort; Public classShell_sort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub int[] A = {78,68,48,39,95,48,94,73}; //Hill Sort intD=a.length; while(d>1) {D=d/2; for(intx=0;x<d;x++) {//Group D as tolerance, divided into D arrays, each of which is {a[i+d],a[i+2d],a[i+3d] ....} for(inti=x+d;i<a.length;i=i+d) {//sort these arrays by direct insert sort (see my direct Insert sort http://www.cnblogs.com/pipixiao/p/7674142.html) inttemp=A[i]; intJ; for(j=i-d;j>=0&&a[j]>temp;j=j-d) {a[j+d]=A[j]; } a[j+d]=temp; }}} System.out.println ("After sorting:"); for(inti=0;i<a.length;i++) {System.out.print (A[i]+" "); }}}
Operation Result:
2. Heap Sequencing
Dagen sort, small Gan sort.
Dagen sort: Build a binary tree first (build a two-fork tree must satisfy the parent node must be greater than its left and right child nodes, the first number in the array is a "0" and its left and right child nodes should be a "1", a "2", the foot is defined as both the parent node is a "I", the left and right child nodes are a "2i+1", a "2i+2") , the first number in the two-fork tree is taken into another empty array, the remaining number is re-formed into a new array, and then the heap is re-built, repeating the above steps until the number in the original array is taken out, and the resulting new array is both a large and small array of sort completion
This binary tree shows that 2 is 5,6 parent node, will 2,5,6 three positions of the number comparison size, get the largest number and 2 position of the exchange, when the number is 2 itself, do not exchange, (1,3,4), (0,1,2) The number of positions should also do the same steps, so that the final 0 position to obtain the number is the largest number , and then the resulting array of a "0" is taken out, the following number constructs a new array, and builds the tree again. Repeat the above steps
Note: Here you can see that the number of builds loops is 3 times: for(int i= (a.lenght-1)/2-1,i>=0,i--), but when this is the case, it is clear that this condition is not satisfied, the number of cycles below is 4 times, We can not change the principle of the above loop to add a little bit both: for(int i= (a.lenght-1)/2-1+ (a.lenght-1)%2;i>=0;i--) In fact, the comparison is not difficult to see when the length of the array is even, Get the two fork tree most regret single out a position, the condition of the comparison will change, and (a.lenght-1)%2 can get the length of an even number plus one cycle, the odd time is unchanged.
The number of builds is this case when (3,7) the position of the number comparison when there is only two number comparison, so there is a difference here, it should be added a judging condition (this time the comparison whether there is a "2i+2" this one, if not exist then the number of comparisons is only a "I" and a "2i+1")
Specific code:
PackageSort;Importjava.util.Arrays; Public classHeapsort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub int[] arr = {78,68,48,39,95,48,94,73}; int[] A =New int[8]; for(inti=0;i<a.length;i++) {heap (arr); A[i]=ARR[0];//gets the first number of the current array to the array aArr=arrays.copyofrange (arr,1,arr.length);//intercepts all numbers following the first number of ARR arrays and re-gives Arr } for(intm = 0; M < a.length; m++) {//iterating through the output array A, array A is both an array after sortingSystem.out.print (a[m]+ ""); } } Public Static voidHeapint[] arr) {//Create Heap intTemp=0; for(intj= (arr.length-1)/2-1+ (arr.length-1)%2;j>=0;j--) {//get the loop condition for each heap creation if(2*j+2<=arr.length-1) {//determine whether the current a[2*j+2] exists, then execute a[j], a[2*j+1], a[2*j+2] comparison, do not exist then execute A[J], a[2*j+1] comparisonTEMP=ARR[J]>ARR[2*J+1]? (arr[j]>arr[2*j+2]?j:2*j+2):(arr[2*j+1]>arr[2*j+2]?2*j+1:2*j+2);//compare to get the maximum number of foot markers}Else{Temp=arr[j]>arr[2*j+1]?j:2*j+1; } if(Arr[j]==arr[temp]) {//if the largest number is a[j] itself exits for the next comparison Continue; }Else{//Maximum number is not A[J] the maximum number and a[j] swap positionarr[j]=arr[j]^Arr[temp]; Arr[temp]=arr[j]^Arr[temp]; ARR[J]=arr[j]^Arr[temp]; } } }}
Operation Result:
"Cicada Hall Study Notes" Java writing several common sorting algorithms 3