Shell Sort
Shell sort is the generalization of insertion sort which overcomes the drawbacks of insertion sort by comparing elements separated by a gap of several positions. In general, Shell sort performs the following steps.
- Step 1: Arrange the elements in the tabular form and sort the columns by using insertion sort.
- Step 2: Repeat Step 1; each time with smaller number of longer columns in such a way that at the end, there is only one column of data to be sorted.
Complexity
Complexity | Best Case | Average Case | Worst Case |
---|---|---|---|
Time Complexity | Ω(n log(n)) | θ(n log(n)2) | O(n log(n)2) |
Space Complexity | O(1) |
Algorithm
Shell_Sort(Arr, n)
- Step 1: SET FLAG = 1, GAP_SIZE = N
- Step 2: Repeat Steps 3 to 6 while FLAG = 1 OR GAP_SIZE > 1
- Step 3:SET FLAG = 0
- Step 4:SET GAP_SIZE = (GAP_SIZE + 1) / 2
- Step 5:Repeat Step 6 for I = 0 to I < (N -GAP_SIZE)
- Step 6:IF Arr[I + GAP_SIZE] > Arr[I]
SWAP Arr[I + GAP_SIZE], Arr[I]
SET FLAG = 0 - Step 7: END
C Program
- #include <stdio.h>
- void shellsort(int arr[], int num)
- {
- int i, j, k, tmp;
- for (i = num / 2; i > 0; i = i / 2)
- {
- for (j = i; j < num; j++)
- {
- for(k = j - i; k >= 0; k = k - i)
- {
- if (arr[k+i] >= arr[k])
- break;
- else
- {
- tmp = arr[k];
- arr[k] = arr[k+i];
- arr[k+i] = tmp;
- }
- }
- }
- }
- }
- int main()
- {
- int arr[30];
- int k, num;
- printf("Enter total no. of elements : ");
- scanf("%d", &num);
- printf("\nEnter %d numbers: ", num);
- for (k = 0 ; k < num; k++)
- {
- scanf("%d", &arr[k]);
- }
- shellsort(arr, num);
- printf("\n Sorted array is: ");
- for (k = 0; k < num; k++)
- printf("%d ", arr[k]);
- return 0;
- }
Output:
Enter total no. of elements : 6 Enter 6 numbers: 3 2 4 10 2 1 Sorted array is: 1 2 2 3 4 10
Java Program
- import java.util.*;
- public class Shell_Sort
- {
- static void shellsort(int[] arr, intnum)
- {
- int i, j, k, tmp;
- for (i = num / 2; i> 0; i = i / 2)
- {
- for (j = i; j<num; j++)
- {
- for(k = j - i; k>= 0; k = k - i)
- {
- if (arr[k+i] >= arr[k])
- break;
- else
- {
- tmp = arr[k];
- arr[k] = arr[k+i];
- arr[k+i] = tmp;
- }
- }
- }
- }
- }
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- int arr[]= newint[30];
- intk, num;
- System.out.println("Enter total no. of elements : ");
- num = sc.nextInt();
- for (k = 0 ; k<num; k++)
- {
- arr[k]=sc.nextInt();
- }
- shellsort(arr, num);
- System.out.println("\n Sorted array is: ");
- for (k = 0; k<num; k++)
- System.out.println(arr[k]);
- }
- }
Output:
Enter total no. of elements : 6 3 2 4 10 2 1 Sorted array is: 1 2 2 3 4 10
C# Program
- using System;
- public class Shell_Sort
- {
- static void shellsort(int[] arr, int num)
- {
- int i, j, k, tmp;
- for (i = num / 2; i > 0; i = i / 2)
- {
- for (j = i; j < num; j++)
- {
- for(k = j - i; k >= 0; k = k - i)
- {
- if (arr[k+i] >= arr[k])
- break;
- else
- {
- tmp = arr[k];
- arr[k] = arr[k+i];
- arr[k+i] = tmp;
- }
- }
- }
- }
- }
- public void Main()
- {
- int[] arr=new int[10];
- int k, num;
- Console.WriteLine("Enter total no. of elements : ");
- num = Convert.ToInt32(Console.ReadLine());
- for (k = 0 ; k < num; k++)
- {
- arr[k]=Convert.ToInt32(Console.ReadLine());
- }
- shellsort(arr, num);
- Console.WriteLine("\n Sorted array is: ");
- for (k = 0; k < num; k++)
- Console.WriteLine(arr[k]);
- }
- }
Output:
Enter total no. of elements : 6 3 2 4 10 2 1 Sorted array is: 1 2 2 3 4 10