-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
967990e
commit 5754fd1
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include<iostream> | ||
using namespace std; | ||
class radixsort | ||
{ | ||
public: | ||
int a[10]; | ||
radixsort() | ||
{ | ||
int count = 0; | ||
} | ||
int getmax(int arr[], int n); | ||
void countsort(int arr[], int n, int exp); | ||
void radsort(int arr[], int n); | ||
void print(int arr[], int n); | ||
}; | ||
|
||
int radixsort::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 radixsort::countsort(int arr[], int n, int exp) | ||
{ | ||
int output[20]; | ||
int i, count[10] = {0}; | ||
for (i = 0; i < n; i++) | ||
count[(arr[i] / exp) % 10]++; | ||
for (i = 0; i < 10; i++) | ||
count[i] += count[i - 1]; | ||
for (i = n - 1; i >= 0; i--) | ||
{ | ||
output[count[(arr[i] / exp) % 10] - 1] = arr[i]; | ||
count[(arr[i] / exp) % 10]--; | ||
} | ||
for ( i = 0; i < n; i++) | ||
arr[i] = output[i]; | ||
} | ||
void radixsort::radsort(int arr[], int n) | ||
{ | ||
int m = getmax(arr, n); | ||
for (int exp = 1; m / exp > 0; exp *= 10) | ||
countsort(arr, n, exp); | ||
} | ||
void radixsort::print(int arr[], int n) | ||
{ | ||
for (int i = 0; i < n; i++) | ||
cout << arr[i] << " "; | ||
} | ||
int main() | ||
{ | ||
radixsort r; | ||
int i; | ||
int arr[10]; | ||
cout << "Input Elements: "; | ||
for ( i = 0; i < 10; i++) | ||
cin >> arr[i]; | ||
//int arr[] = {190, 3000, 68, 38, 980, 356, 1, 5, 66}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
r.radsort(arr, n); | ||
r.print(arr, n); | ||
} |