Given an array arr = [a1,a2,a3,a4,a5...,an] , we need to sort the array the array with the help of Shell Sort algorithm.
Shell Sort is an improvement in the Insertion sort algorithm. It can be observed that the insertion sort works extremely well on almost sorted array. Shell sort makes use of this. We make the array h-sorted for a large value of h using the technique of insertion sort. We then keep reducing the value of h until it becomes 1.
while(h<n/3)h=3*h+1;while(h>=1){
for(int i=h;i<n;i++){
//perform insertion sort for every h'th element
for(int j=i;j>=h && less(a[j],a[j-h]);j-=h){
exch(a,j,j-h);
}
}
h=h/3;
}