Embed presentation
Downloaded 98 times








![9
Shell sorting code
for( gap=n/2; gap>0; gap/=2)
{
for( i= gap; i<n; i++ )
{
temp=array[i];
for(j=i; j>=gap && array[j-gap]>temp ; j - = gap)
{
array[j]=array[j-gap];
}
array[j]=temp;
}
}
N=8
Gap = 4](https://image.slidesharecdn.com/shellsort-140511114834-phpapp02/75/Shell-sort-9-2048.jpg)



Shell sort is a generalization of insertion sort that improves performance by sorting elements with gaps between them. It works by first sorting elements with a large gap between them, then reducing the gap and sorting again until the gap is 1. For example, with an array of 8 elements and an initial gap of 4, elements are first sorted in groups of 4 apart, then with a gap of 2, and finally with a gap of 1 to fully sort the array. The algorithm uses this gap sequence: n/2, n/2, n/2, ..., 1.








![9
Shell sorting code
for( gap=n/2; gap>0; gap/=2)
{
for( i= gap; i<n; i++ )
{
temp=array[i];
for(j=i; j>=gap && array[j-gap]>temp ; j - = gap)
{
array[j]=array[j-gap];
}
array[j]=temp;
}
}
N=8
Gap = 4](https://image.slidesharecdn.com/shellsort-140511114834-phpapp02/75/Shell-sort-9-2048.jpg)

