🌳
B.TECH I YEAR β€’ II SEMESTER β€’ R25

Data Structures - Lab Manual

Data Structures Laboratory β€’ C Programming Solution

πŸ“˜ R25 Regulation πŸŽ“ I Year πŸ“š II Semester πŸ’» C Programming πŸ§ͺ Lab Program
Selected Program

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

← All 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 

πŸ’» Run This C Program

C program that implements Radix sorting methods to sort a given list of integers in ascending order
The selected C program is loaded automatically. Edit the code, provide input through STDIN and click Run.

The compiler is embedded using OneCompiler. Your program runs in the embedded C environment; Study Glance does not execute C code directly on your web server.
← Back to Data Structures Lab Programs πŸ“š All Lab Manuals