Menu

Data Structures [ Lab Programs ]


C program that implements Radix sorting methods to sort a given list of integers in ascending order

Radix sorting methods
#include <stdio.h>

// Function to get the maximum value in the array
int getMax(int arr[], int n) {
    int max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > max)
            max = arr[i];
    }
    return max;
}

// A function to perform counting sort based on a specific digit (exp)
void countingSort(int arr[], int n, int exp) {
    int output[n];        // output array
    int count[10] = {0};  // count array for digits (0–9)

    // Count occurrences of digits
    for (int i = 0; i < n; i++)
        count[(arr[i] / exp) % 10]++;

    // Convert count[] to cumulative count
    for (int i = 1; i < 10; i++)
        count[i] += count[i - 1];

    // Build the output array (stable sort)
    for (int i = n - 1; i >= 0; i--) {
        int digit = (arr[i] / exp) % 10;
        output[count[digit] - 1] = arr[i];
        count[digit]--;
    }

    // Copy output array back to arr[]
    for (int i = 0; i < n; i++)
        arr[i] = output[i];
}

// Main Radix Sort function
void radixSort(int arr[], int n) {
    int max = getMax(arr, n);

    // Apply counting sort for every digit
    for (int exp = 1; max / exp > 0; exp *= 10)
        countingSort(arr, n, exp);
}

// Function to print the array
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}


int main() {
    int n,arr[20];

    printf("Enter number of elements: ");
    scanf("%d", &n);

    printf("Enter %d integers:\n", n);
    for (int i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    radixSort(arr, n);

    printf("Sorted array: ");
    printArray(arr, n);

    return 0;
}

OUTPUT
Enter number of elements: 9
Enter 9 integers:
7 4 1 8 5 2 9 6 3
Sorted array: 1 2 3 4 5 6 7 8 9 

Related Content :

1. Write a program that uses functions to perform the following operations on singly linkedlist.:
   i) Creation    ii) Insertion    iii) Deletion    iv) Traversal    View Solution

2. Write a program that uses functions to perform the following operations on doubly linkedlist.:
   i) Creation    ii) Insertion    iii) Deletion    iv) Traversal    View Solution

3. Write a program that uses functions to perform the following operations on circular linkedlist.:
   i) Creation    ii) Insertion    iii) Deletion    iv) Traversal    View Solution

4. Write a program that implement Stack (its operations) using Array    View Solution

5. Write a program that implement Stack (its operations) using Linked List (Pointer)    View Solution

6. Write a program that implement Queue(its operations) using Array    View Solution

7. Write a program that implement Queue (its operations) using Linked List (Pointer)    View Solution

8. Write a program that implements Radix sorting methods to sort a given list of integers in ascending order    View Solution

9. Write a program that implements Heap sorting methods to sort a given list of integers in ascending order    View Solution

10. Write a program that implements Shell sorting methods to sort a given list of integers in ascending order    View Solution

11. Write a program that implements Tree sorting methods to sort a given list of integers in ascending order    View Solution

12. Write a program to implement the tree traversal methods using Recursive    View Solution

13. Write a program to implement the tree traversal methods using Non Recursive    View Solution

14. Write a program to implement Binary Search Tree (its operations)    View Solution

15. Write a program to implement AVL Tree (its operations)    View Solution

16. Write a program to implement Red - Black Tree (its operations)    View Solution

17. Write a program to implement the graph traversal methods (Breadth First Search)    View Solution

18.Write a program to implement the graph traversal methods (Depth First Search)    View Solution

19. Write a program to implement the following Hash Functions: i) Division Method, ii) Multiplication Method, iii) Mid-square Method, iv) Folding Method    View Solution