Time Complexity of O (n^ (3/2))
Not a stable sorting algorithm
How to see if an algorithm is stable:
{("Scala", "+"), ("Python", "+"), ("C", "+"), ("Java", 44)}
Scala and C + + values are equal and Scala precedes C + + before sorting
If after sorting
{("Scala", "+"), ("C + +", "+"), ("Python", "$"), ("C", "}//scala"), or in front of C + +, stabilize
{("C + +", +), ("Scala", "+"), ("Python", "}//scala"), ("Java", "$"), ("C", "the"), is unstable after C + +
Package Com.sort.shell; Public classShellsort { Public Static voidSwapint[] list,intAintb) { inttemp; Temp=List[a]; List[a]=List[b]; LIST[B]=temp; } Public Static voidPrintintlist[]) { for(intI=0; i<list.length; i++) {System. out. Print (" "+List[i]); } } /** * {0,9,1,5,8,3,7,4,6,2} * * int increment = l.length-1; * Set an increment increment * increment = increment/3+1; * increment=4 * i=5, from number 5th to 9th # Traverse * Let 1th and 5th compare, if 1th number is greater than 5th, on the Exchange position * 2nd and 6th comparison, if the 2nd number is not greater than 6th, will not move * 3rd and 7th comparison, (3-4)!> 0, end * 4th and 8th comparisons (4-4)!> 0, End * 5th and 9th compare, find (5-4=1) >0, let 1th and 5th compare, (1-4)!>0, end * * Increment = increment/3+ 1; * increment=2 * i=3, from number 3rd to 9th Traverse * 1th and 3rd comparison (1-3)!> 0, End * 2nd and 4th Comparison * 3rd and 5th comparisons, 1th and 3rd comparisons * 4th and 6th comparisons, 2nd and 4th comparisons * Comparison of Numbers 5th and 7th, comparisons between numbers 3rd and 5th, 1th and 3rd compared with 6th and 8th comparisons, 4th and 6th comparisons, 2nd and 4th comparisons, 7th and 9th comparisons, and comparison between the numbers of the number, 5th and 7th, comparisons of 3rd and * * Inc Rement = increment/3+1; * Increment = 1 * 2nd to 9th Traverse * 2nd and 1th Comparison * 3rd and 2nd comparison * .... **/ Public Static voidShellsort (int[] L) { inti,j; intincrement = l.length-1; Do{Increment= increment/3+1;//Incremental Sequence for(i=increment+1; i<=l.length-1; i++){ if(L[i] < l[i-Increment]) {l[0] = L[i];//temporary presence L[0] for(J=i-increment; j>0&& l[0]<l[j]; j-=increment) {L[j+increment] = L[j];//record and move back to find the insertion position} l[j+increment] = l[0];//Insert } } } while(Increment >1); } Public Static voidSortint[] list) { //Prelude Work int[] L =New int[list.length+1]; l[0] =0;//let the No. 0 element be the location of the staging data for(intI=1; i<l.length; i++) {L[i]= list[i-1]; } shellsort (L);//make a sort of hill//Finishing Work for(intI=1; i<l.length; i++) {List[i-1] =L[i]; } } Public Static voidMain (string[] args) {int[] List =New int[]{9,1,5,8,3,7,4,6,2}; print (list); System. out. println (); Sort (list); print (list); } }
Hill Sort (Java)