[go: up one dir, main page]

0% found this document useful (0 votes)
2 views3 pages

Redix Sort

The document contains a C++ program implementing the Radix Sort algorithm. It includes functions to find the maximum element, perform counting sort, and execute the radix sort on an array of integers. The program reads an array size and its elements from user input, sorts the array, and prints the sorted result.

Uploaded by

Movie Active
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Redix Sort

The document contains a C++ program implementing the Radix Sort algorithm. It includes functions to find the maximum element, perform counting sort, and execute the radix sort on an array of integers. The program reads an array size and its elements from user input, sorts the array, and prints the sorted result.

Uploaded by

Movie Active
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DAA Lab

Experiment no 7
Name: Tushar Singh
Roll No: 2300290110195
Class: CSIT 5C
Ques WAP for Radix Sort
Code:
#include <iostream>
#include <algorithm>
using namespace std;
int getMax(int arr[], int n) {
int mx = arr[0];
for (int i=1; i<n; i++)
if (arr[i] > mx) mx = arr[i];
return mx;
}
void countSort(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
for (int i=0; i<n; i++) count[(arr[i]/exp)%10]++;
for (int i=1; i<10; i++) count[i] += count[i-1];
for (int i=n-1; i>=0; i--) {
output[count[(arr[i]/exp)%10] - 1] = arr[i];
count[(arr[i]/exp)%10]--;
}
for (int i=0; i<n; i++) arr[i] = output[i];
}
void radixSort(int arr[], int n) {
int m = getMax(arr, n);
for (int exp=1; m/exp>0; exp*=10) countSort(arr, n, exp);
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i=0; i<n; i++) cin >> arr[i];
radixSort(arr, n);
for (int i=0; i<n; i++) cout << arr[i] << " ";
return 0;
}

You might also like