The document describes the Radix Sort algorithm and provides a C program to implement it. Radix sort sorts data by grouping keys based on their individual digit values in each significant position. The program implements Radix Sort using counting sort as a subroutine to sort digits from least significant to most significant. It takes an input array, finds the largest number to determine number of passes, and calls counting sort repeatedly by increasing the exponent value to sort the array. The output of the sample run shows the sorted array.
1. G H PATEL COLLEGE OF ENGINEERING & TECHNOLOGY
GCET, V. V. Nagar – 388 120 1 | P a g e
DATA STRUCTURE OPEN ENDED PROBLEM
Topic: Radix Sort.
Prepared by: - Tejoy Vachhrajani (160110116057)
Khyati Valera (160110116059)
Bhavik Vashi (160110116061)
Submitted to: - Prof. Dipti Mathpal
Class: B.E. (IT) 2nd
Year
Batch: - 1C16
1. INTRODUCTION
In computer science, radix sort is a non-comparative integer sorting algorithm that sorts
data with integer keys by grouping keys by the individual digits which share the same
significant position and value. A positional notation is required, but because integers can
represent strings of characters (e.g., names or dates) and specially formatted floating point
numbers, radix sort is not limited to integers. Radix sort dates back as far as 1887 to the
work of Herman Hollerith on tabulating machines.
Most digital computers internally represent all of their data as electronic representations of
binary numbers, so processing the digits of integer representations by groups of binary digit
representations is most convenient. Two classifications of radix sorts are least significant
digit (LSD) radix sorts and most significant digit (MSD) radix sorts. LSD radix sorts process the
integer representations starting from the least digit and move towards the most significant
digit. MSD radix sorts work the other way around.
This is a C Program to implement Radix Sort. The idea of Radix Sort is to do digit by digit sort
starting from least significant digit to most significant digit. Radix sort uses counting sort as a
subroutine to sort.
Here is source code of the C Program to Implement Radix Sort. The C program is successfully
compiled and run on a Linux system. The program output is also shown below.
2. G H PATEL COLLEGE OF ENGINEERING & TECHNOLOGY
GCET, V. V. Nagar – 388 120 2 | P a g e
THE ALGORITHM
The program implementation is done by using the below
algorithm.
Step 1: Find the largest number in array as LARGE
Step 2: [INITIALIZE] Set NOP = Number of digits in LARGE.
Step 3: Set PASS=0
Step 4: Repeat Step 5 while PASS<=NOP-1.
Step 5: Set i=0 and initialize the bucket
Step 6: Repeat steps 7 to 9 while i<n-1
Step 7: Set digit=digit at PASSth place in A[i]
Step 8: Add A[i] to bucket numbered digit
Step 9: Increment bucket count for bucket number digit
(End of Loop)
Step 10: Collect the numbers in the bucket
(End of Loop)
Step 11: End
3. G H PATEL COLLEGE OF ENGINEERING & TECHNOLOGY
GCET, V. V. Nagar – 388 120 3 | P a g e
Program :-
1. #include<stdio.h>
2. #include<conio.h>
3.
4. int getMax(int arr[], int n) {
5. int mx = arr[0];
6. int i;
7. for (i = 1; i < n; i++)
8. if (arr[i] > mx)
9. mx = arr[i];
10. return mx;
11. }
12.
13. void countSort(int arr[], int n, int exp) {
14. int output[n]; // output array
15. int i, count[10] = { 0 };
16.
17. // Store count of occurrences in count[]
18. for (i = 0; i < n; i++)
19. count[(arr[i] / exp) % 10]++;
20.
21. for (i = 1; i < 10; i++)
22. count[i] += count[i - 1];
23.
24. // Build the output array
4. G H PATEL COLLEGE OF ENGINEERING & TECHNOLOGY
GCET, V. V. Nagar – 388 120 4 | P a g e
25. for (i = n - 1; i >= 0; i--) {
26. output[count[(arr[i] / exp) % 10] - 1] = arr[i];
27. count[(arr[i] / exp) % 10]--;
28. }
29.
30. for (i = 0; i < n; i++)
31. arr[i] = output[i];
32. }
33.
34. // The main function to that sorts arr[] of size n using Radix Sort
35. void radixsort(int arr[], int n) {
36. int m = getMax(arr, n);
37.
38. int exp;
39. for (exp = 1; m / exp > 0; exp *= 10)
40. countSort(arr, n, exp);
41. }
42.
43. void print(int arr[], int n) {
44. int i;
45. for (i = 0; i < n; i++)
46. printf("%d ", arr[i]);
47. }
48.
49. int main() {
50. int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 };
51. int n = sizeof(arr) / sizeof(arr[0]);
5. G H PATEL COLLEGE OF ENGINEERING & TECHNOLOGY
GCET, V. V. Nagar – 388 120 5 | P a g e
52. radixsort(arr, n);
53. Print(arr, n);
54. return 0;
55. }
Output:
2 24 45 66 75 90 170 802