Radix sort

Topics Covered

  • Algorithm for radix sort
  • C program to implement radix sort
  • C++ program to implement radix sort

Algorithm for radix sort

1. Take input in the array.
2. Get the maximum element.
3. Run the countsort() till (m/exp) > 0.
4. Sort the elements on the basis of the element at (exp)th place.
5. Assign the sorted elements back to array[].
6. Check the condition in step 3.
7. If false, print the sorted output array.
8. Exit the program.

C program to implement radix sort


#include <stdio.h>
#include <conio.h>
// Function applied to obtain the largest element from the array
int getMax(int array[], int n) {
  int max = array[0];
  for (int i = 1; i < n; i++)
    if (array[i] > max)
      max = array[i];
  return max;
}
// Apply counting sort to sort the elements on the basis of specific positions
void countingsort(int array[], int size, int place) {
  int output[size + 1];
  int max = (array[0] / place) % 10;
  for (int i = 1; i < size; i++) {
    if (((array[i] / place) % 10) > max)
      max = array[i];
  }
  int count[max + 1];
  for (int i = 0; i < max; ++i)
    count[i] = 0;
  // Calculate the total count of elements
  for (int i = 0; i < size; i++)
    count[(array[i] / place) % 10]++;
      // Calculate cummulative counts
  for (int i = 1; i < 10; i++)
    count[i] += count[i - 1];
 // Place all the elements in the sorted order
  for (int i = size - 1; i >= 0; i--) {
    output[count[(array[i] / place) % 10] - 1] = array[i];
    count[(array[i] / place) % 10]--;
  }
  for (int i = 0; i < size; i++)
    array[i] = output[i];
}
// main function to implement radix sort
void radixsort(int array[], int size) {
  // Get the maximum element among them
  int max = getMax(array, size);
  // Apply counting sort to sort elements based on place value
  for (int place = 1; max / place > 0; place *= 10)
    countingsort(array, size, place);
}
// Print an array
void printArray(int array[], int size) {
  for (int i = 0; i < size; ++i) {
    printf("%d  ", array[i]);
  }
  printf("\n");
}
// Driver code
int main() {
  int array[] = {121, 432, 564, 23, 1, 45, 788};
  int n = sizeof(array) / sizeof(array[0]);
  radixsort(array, n);
  printf("The final sorted array is : \n");
  printArray(array, n);
}

C++ program to implement radix sort

#include <iostream>
using namespace std;
 
// Get maximum value from the array
int getMax(int array[], int n)
{
	int maximum = array[0];
	for (int i = 1; i < n; i++)
		if (array[i] > maximum)
			maximum = array[i];
	return maximum;
}
 
// Apply count sort for arr[].
void countSort(int array[], int n, int exp)
{
	// Count[i] array counting the number of array values having that 'i' digit at their (exp)th place.  
	int output[n], i, count[10] = {0};
 
	// Count the number of times each digit occurred at (exp)th place in every input.
	for (i = 0; i < n; i++)
		count[(array[i] / exp) % 10]++;
 
	// Calculating their cumulative count
	for (i = 1; i < 10; i++)
		count[i] += count[i-1];
 
	// Inserting values according to the digit '(arr[i] / exp) % 10' fetched into count[(arr[i] / exp) % 10].
	for (i = n - 1; i >= 0; i--)
	{
		output[count[(array[i] / exp) % 10] - 1] = array[i];
		count[(array[i] / exp) % 10]--;
	}
 
	// Assigning the result to the array pointer of main() function.
	for (i = 0; i < n; i++)
		array[i] = output[i];
}
 
// Sorting the array[] of size n using Radix Sort.
void radixsort(int array[], int n)
{
	int exp, m;
	m = getMax(array, n);
 
	// Calling countSort() for digit at (exp)th place in every input.
	for (exp = 1; m/exp > 0; exp *= 10)
		countSort(array, n, exp);
}
 
int main()
{
	int n, i;
	cout<<"\nEnter the size of the array to be sorted: ";
	cin>>n;
   //apply loop for taking the elements in the array 
	int array[n];
	for(i = 0; i < n; i++)
	{
		cout<<"Enter element "<<i+1<<": ";
		cin>>array[i];
	}
 
	radixsort(array, n);
 
	// Printing the final sorted data
	cout<<"Final sorted array is : "<<endl;
	for (i = 0; i < n; i++)
		cout<<" "<<array[i];
	return 0;
}
We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!๐Ÿ‘‹
Can we help you?