C Program to Sort an Integer Array using LSDRadix Sort Algorithm

This C Program sorts an integer array using lsdradix sort algorithm.

Here is source code of the C Program to sort an integer array using lsdradix sort algorithm. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program to Sort an Integer Array using LSDRadix Sort Algorithm
  3.  */
  4. #include <stdio.h>
  5.  
  6. int min = 0, count = 0, array[100] = {0}, array1[100] = {0};
  7.  
  8. void main()
  9. {
  10.     int k, i, j, temp, t, n;
  11.  
  12.     printf("Enter size of array :");
  13.     scanf("%d", &count);
  14.     printf("Enter elements into array :");
  15.     for (i = 0; i < count; i++)
  16.     {
  17.         scanf("%d", &array[i]);
  18.         array1[i] = array[i];
  19.     }
  20.     for (k = 0; k < 3; k++)
  21.     {    
  22.         for (i = 0; i < count; i++)
  23.         {
  24.             min = array[i] % 10;        /* To find minimum lsd */
  25.             t = i;
  26.             for (j = i + 1; j < count; j++)    
  27.             {
  28.                 if (min > (array[j] % 10))
  29.                 {
  30.                     min = array[j] % 10; 
  31.                     t = j;
  32.                 }
  33.             }
  34.             temp = array1[t];
  35.             array1[t] = array1[i];
  36.             array1[i] = temp;
  37.             temp = array[t];
  38.             array[t] = array[i];
  39.             array[i] = temp;
  40.  
  41.         }
  42.         for (j = 0; j < count; j++)        /*to find MSB */
  43.             array[j] = array[j] / 10;
  44.     }
  45.     printf("Sorted Array (lSdradix sort) : ");
  46.     for (i = 0; i < count; i++)
  47.         printf("%d ", array1[i]);
  48. }

$ cc lsdradix.c
$ a.out
/* Average Case */
Enter size of array :7
Enter elements into array :170    
45 
90
75
802
24
2
Sorted Array (ladradix sort) : 2 24 45 75 90 170 802 
 
$ a.out
/*Best case */
Enter size of array :7    
Enter elements into array :22
64
121
78
159
206
348
Sorted Array (ladradix sort) : 22 64 78 159 121 206 348
 
$ a.out
/* Worst case */
Enter size of array :7
Enter elements into array :985
27
64
129
345
325
091
Sorted Array (ladradix sort) : 27 64 91 129 325 345 985

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.

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.