To understand the shell sort, the first thing to do is to put a solid foundation into the sort directly.
Learning materials: Vernacular classical algorithm series of two direct insertion sort of three implementations, direct insertion sort
According to my train of thought, direct insert sort setting 3 heavy loop.
Loop 1: traverse the i= "unordered sequence" ∈[1, length).
Loop 2: traverse the j= "ordered sequence" ∈[0, I).
Comparisons: Nums [i] < Nums [j] (found that elements in ordered sequences do not conform to descending order , i.e. there are elements in unordered sequences smaller than ordered sequences )
Loop 3: The elements of index= I are inserted into the position of J, and the element [J, I] moves backwards.
Java code:
1 classinsertsort{2 int[] Sortans;3Insertsort (int[] nums) {4 intlen=nums.length;5 inti,j,k;6 for(i=1;i<len;i++) {//I represents the number of sorted elements, traversing in the unordered sequence7 //inserting unordered elements into an ordered sequence within the range of "I, Len"8 for(j=0;j<i;j++) {//to traverse an ordered sequence9 if(Nums[j]>nums[i]) {//if the element in the ordered sequence is greater than the first element of the unordered sequenceTen intTmp=nums[i];//temporarily save "unordered" One for(k=i;k>j;k--) {//Move Right ANums[k]=nums[k-1]; - } -nums[j]=tmp; the } - } - } -sortans=nums; + } - PublicString toString () { + inti; AString str=NewString (""); at for(i=0;i<sortans.length;i++) str+=string.valueof (Sortans[i]) + ""; -str+= "\ n"; - returnstr; - } -}
The shell sort uses the idea of narrowing the increment, the concrete principle no longer repeat. Reference Learning Links:
Algorithm Chapter---Shell sort (hill) algorithm, eight sort algorithm-shell sort
There are different methods of implementation on different data. But the concrete idea is consistent. According to my understanding, I designed a 5-heavy loop:
Loop 1: Increment with I, cycle decrement. (to ensure that the last cycle increment is 1, a control statement should be added)
Loop 2: Use J to indicate the position of the first element. J∈[0,length-i)
Loop 3: Use K to denote the subscript of each element in the unordered sequence.
Loop 4: Uses M to denote the subscript of each element in the ordered sequence.
Loop 5: Use N to indicate the subscript in the right-hand loop.
Java code:
1 classshellsort{2 int[] Sortans;3Shellsort (int[] nums) {4 intlen=nums.length;5 intI,j,k,m,n;6 Booleanok=false;7 for(i=len/2;i>=-1;i-=2) {//first layer loop: increment decrement8 if(i<=0) {i=1;ok=true;}9 for(j=0;j+i<len;j++) {//Second loop: first element step upTen for(k=j+i;k<len;k+=i) {//third layer loop: first element plus increment. Traversal of k "unordered sequence" One for(m=j;m<k;m+=i) {//fourth Loop, traversing m "ordered sequence" A if(Nums[m]>nums[k]) {//if "ordered" > "unordered" (Breaking the DESC rule) - intTMP=NUMS[K];//inserts "unordered" into the "ordered" position. Save temporary variable "ordered" - for(n=k;n>m;n-=i) { theNums[n]=nums[n-i];//Loop Right Shift - } -nums[m]=tmp; - } + } - } + } A if(OK) Break; at } -sortans=nums; - } - PublicString toString () { - inti; -String str=NewString (""); in for(i=0;i<sortans.length;i++) str+=string.valueof (Sortans[i]) + ""; -str+= "\ n"; to returnstr; + } -}
Direct insert sort and narrow incremental insert sort (Hill sort Shellsort)