-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathbucket-sort.cpp
More file actions
67 lines (62 loc) · 1.15 KB
/
bucket-sort.cpp
File metadata and controls
67 lines (62 loc) · 1.15 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
#include<iostream>
using namespace std;
int findMax(int arr[], int n)
{
int i,max=arr[0],cnt=0;
for(i=1;i<n;i++)
{
if(arr[i]>max)
max=arr[i];
}
while(max>0)
{
cnt++;
max=max/10;
}
return cnt;
}
void bucketSort(int arr[],int *bucket[],int n)
{
static int i,j[10],k,l,d=1;
int c;
c=findMax(arr,n);
for(int m=0;m<c;m++)
{
for(i=0;i<10;i++)
j[i]=0;
for(i=0;i<n;i++)
{
k=(arr[i]/d)%10;
bucket[k][j[k]]=arr[i];
j[k]++;
}
l=0;
for(i=0;i<10;i++)
{
for(k=0;k<j[i];k++)
{
arr[l]=bucket[i][k];
l++;
}
}
d*=10;
}
}
int main()
{
int n,*arr,i;
int *bucket[20];
cout<<"Enter no of element : ";
cin>>n;
arr=new int[n+1];
for(i=0;i<10;i++)
bucket[i]=new int[n];
cout<<"Enter array element : ";
for(i=0;i<n;i++)
cin>>arr[i];
bucketSort(arr,bucket,n);
cout<<"Sorted array : ";
for(i=0;i<n;i++)
cout<<arr[i]<<" ";
return 0;
}