C Program to Perform Shell Sort without using Recursion

This C Program Performs Shell Sort without using Recursion.

Here is source code of the C Program to Perform Shell Sort without using Recursion. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Perform Shell Sort without using Recursion
  3.  */
  4. #include  <stdio.h>
  5. #define size 7
  6.  
  7. /* Function Prototype */
  8. int shell_sort(int []);
  9.  
  10. void main()
  11. {
  12.     int arr[size], i;
  13.     printf("Enter the elements to be sorted:");
  14.     for (i = 0;i < size;i++)
  15.     {
  16.         scanf("%d", &arr[i]);
  17.     }
  18.     shell_sort(arr);
  19.     printf("The array after sorting is:");
  20.     for (i = 0;i < size;i++)
  21.     {
  22.         printf("\n%d", arr[i]);
  23.     }
  24. }
  25.  
  26. /* Code to sort array using shell sort */
  27. int shell_sort(int array[])
  28. {
  29.     int i = 0, j = 0, k = 0, mid = 0;
  30.     for (k = size / 2;k > 0;k /= 2)
  31.     {
  32.         for (j = k;j < size;j++)
  33.         {
  34.             for (i = j - k;i >= 0;i -= k)
  35.             {
  36.                 if (array[i + k] >= array[i])
  37.                 {
  38.                     break;
  39.                 }
  40.                 else
  41.                 {
  42.                     mid = array[i];
  43.                     array[i] = array[i + k];
  44.                     array[i + k] = mid;
  45.                 }
  46.             }
  47.         }
  48.     }
  49.     return 0;
  50. }

$ cc shellsort.c
Average case:
$ a.out
Enter the elements to be sorted:57
67
48
93
42
84
95
The array after sorting is:
42
48
57
67
84
93
95
 
Best case:
$ a.out
Enter the elements of array:22
33
74
85
86
87
98
The array after sorting is:22
33
74
85
86
87
98
 
Worst case:
$ a.out
Enter the elements of array:94
92
91
89
85
80
43
The array after sorting is:43
80
85
89
91
92
94

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.