Shell sort/Reduce incremental sort (c + +)Shell sort/reduce incremental sort:
He works by comparing elements that are spaced apart, and the distances used by the comparisons are reduced as the algorithm progresses until the last order of the adjacent elements is compared. (Very complex)
If you look at the implementation code, you'll find it looks like a sort of insertion, except for a vest outside.
The following code shows the increment rule on the base of the insertion sort, with the original increment of 1 becoming delta to Gap.
Code Implementation:
1#include <iostream>2#include <vector>3 4 using namespacestd;5 6 //Schell Sort (reduced incremental sort)7Template<typename comparable>8 voidShellsort (Vector<comparable> &a)9 {Ten for(intGap = A.size ()/2; Gap >0; Gap/=2) One { A for(inti = Gap;i < A.size (); i++) - { -Comparable TMP =A[i]; the intj =i; - - for(; J >= Gap&&tmp < a[j-gap];j-=Gap) - { +A[J] = a[j-Gap]; - } +A[J] =tmp; A } at } - } - - intMain () - { -vector<int> A = {3,4,5,8,7,1 }; in Shellrtsort (a); - for(auto c:a) to { +cout << c<<Endl; - } theSystem"Pause"); *}
Shell sort/Reduce incremental sort (c + +)