-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.c
49 lines (41 loc) · 798 Bytes
/
bucket.c
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
#include <stdio.h>
int main( void )
{
int unsorted[50] , bucket[10][50]={{0}} , sorted[50] ;
int j , k , m , p , flag = 0, num, N;
printf("\nEnter the number of elements to be sorted :");
scanf("%d",&N);
printf("\nEnter the elements to be sorted :\n");
for(k=0 ; k < N ; k++){
scanf("\n%d",&num);
sorted[k] = unsorted[k] = num;
}
for(p=1; flag != N ; p*=10){
flag = 0;
for(k=0;k<N;k++){
bucket[(sorted[k]/p)%10][k] = sorted[k];
if ( (sorted[k]/p)%10 == 0 ){
flag++;
}
}
if (flag == N){
printf("\nSorted List: \n");
for(j=0 ; j < N ; j++){
printf("%d\t", sorted[j]);
}
printf("\n");
getch();
return 0;
}
for(j=0,m=0;j<10;j++){
for(k=0;k<N;k++){
if( bucket[j][k] > 0 ){
sorted[m] = bucket[j][k];
bucket[j][k] = 0 ;
m++;
}
}
}
}
return 0 ;
}