Advertisement
Shailrshah

Shell Sort using arrays

Nov 3rd, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void swap(int *a, int *b){
  4.     if(*a != *b){
  5.         int t = *a; *a = *b; *b = t;
  6.     }
  7. }
  8. void shellSort(int a[], int n){
  9.     int i, j, k;
  10.        for(i = n/2; i > 0; i = i/2)
  11.             for(j = i; j < n; j++)
  12.                 for(k = j - i; k >= 0; k = k - i)
  13.                     if(a[k+i] < a[k]) swap(&a[k+i], &a[k]);
  14.                     else break;
  15. }
  16. int main(){
  17.     int *a, i, n;
  18.     printf("Enter the number of elements : ");
  19.     scanf("%d", &n);
  20.     a = (int *) malloc(sizeof(int) *n);
  21.     for(i = 0; i < n; i++) scanf("%d", &a[i]);
  22.     shellSort(a, n);
  23.     printf("The sorted list is:-\n");
  24.     for(i=0; i<n; i++) printf("%d\t", a[i]);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement