0% found this document useful (0 votes)
4 views

Shellsort

Shell sort analysis

Uploaded by

sheradha20
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Shellsort

Shell sort analysis

Uploaded by

sheradha20
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Shell Sort

1
• Shell sort is a highly efficient sorting algorithm
and is based on insertion sort algorithm. This
algorithm avoids large shifts as in case of
insertion sort, if the smaller value is to the far
right and has to be moved to the far left.

• This algorithm uses insertion sort on a widely


spread elements, first to sort them and then
sorts the less widely spaced elements. This
spacing is termed as interval. This interval is
calculated based on Knuth's formula as −
2
• h=h*3+1
• where −
• h is interval with initial value 1
• This algorithm is quite efficient for medium-sized
data sets as its average and worst case
complexity are of O(n), where n is the number of
items.

3
Shell Sort Algorithm
• Following is the algorithm for shell sort.

1. Initialize the value of h.


2. Divide the list into smaller sub-list of equal
interval h.
3. Sort these sub-lists using insertion sort.
4. Repeat until complete list is sorted.
Pseudocode

4
procedure shellSort()
• A : array of items

• /* calculate interval*/
• while interval < A.length /3 do:
• interval = interval * 3 + 1
• end while

5
Shell Sort Algorithm
• Following is the algorithm for shell sort.

• 1. Initialize the value of h.


• 2. Divide the list into smaller sub-list of equal
interval h.
• 3. Sort these sub-lists using insertion sort.
• 4. Repeat until complete list is sorted.

6
Pseudocode

• while interval > 0 do:


• for outer = interval; outer < A.length; outer ++ do:

• /* select value to be inserted */


• valueToInsert = A[outer]
• inner = outer;

• /*shift element towards right*/
• while inner > interval -1 && A[inner - interval]
• >= valueToInsert do:
• A[inner] = A[inner - interval]
• inner = inner – interval
• end while

7
• /* insert the number at hole position */
• A[inner] = valueToInsert
• end for

• /* calculate interval*/
• interval = (interval -1) /3;
• end while
• end procedure

8
• void shellSort(int arr[], int n){
• int gap, j, k;
• for(gap = n/2; gap > 0; gap = gap / 2) { //initially gap = n/2,
decreasing by gap /2
• for(j = gap; j<n; j++) {
• for(k = j-gap; k>=0; k -= gap) {
• if(arr[k+gap] >= arr[k])
• break;
• else {
• int temp;
• temp = arr[k+gap];
• arr[k+gap] = arr[k];
• arr[k] = temp;
• } } } }}
9
• int main(){
• int n;
• n = 5;
• int arr[5] = {33, 45, 62, 12, 98}; // initialize the array
• printf("Array before Sorting: ");
• for(int i = 0; i<n; i++)
• printf("%d ",arr[i]);
• printf("\n");
• shellSort(arr, n);
• printf("Array after Sorting: ");
• for(int i = 0; i<n; i++)
• printf("%d ", arr[i]);
• printf("\n");
• }
10

You might also like