-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradprg.cpp
More file actions
70 lines (60 loc) · 1.42 KB
/
Copy pathradprg.cpp
File metadata and controls
70 lines (60 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;
#define MAX 10
class radixsort{
int arr[MAX],n;
public:
void getdata();
void showdata();
void sortLogic();
};
void radixsort :: getdata(){
cout<<"How many elements you require : ";
cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
}
void radixsort :: showdata(){
cout<<"\n--Display--\n";
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
}
void radixsort :: sortLogic(){
//for base 10int temp;
int bucket[10][20], buck_count[10], b[10];
int i,j,k,r,no_of_passes,divisor,largest,pass_no;
divisor = 1;
no_of_passes=0;
largest=arr[0];
for(i=1;i<n;i++) //Find the largest Number
{
if(arr[i] > largest)
largest=arr[i];
}
while(largest > 0) //Find number of digits in largest number
{
no_of_passes++;
largest /= 10;
}
for(pass_no=0; pass_no < no_of_passes; pass_no++){
for(k=0; k<10; k++)
buck_count[k]=0; //Initialize bucket count
for(i=0;i<n;i++){
r=(arr[i]/divisor) % 10;
bucket[r][buck_count[r]++]=arr[i];
}
i=0; //collect elements from bucket
for(k=0; k<10; k++){
for(j=0; j<buck_count[k]; j++)
arr[i++] = bucket[k][j];
}
divisor *= 10;
}
}
main(){
cout<<"\n*****Radix Sort*****\n";
radixsort obj;
obj.getdata();
obj.sortLogic();
obj.showdata();
}