-
Notifications
You must be signed in to change notification settings - Fork 3
/
BUCKET.CPP
108 lines (89 loc) · 1.42 KB
/
BUCKET.CPP
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <conio.h>
#include <iostream.h>
#include <stdlib.h>
class bucket {
public:
float data;
bucket* next;
bucket(float a, bucket* b)
{
data = a;
next = b;
}
};
void bucketSort(bucket* list[], float arr[], int s)
{
int pos;
float tempData, x;
bucket* temp = NULL;
for (int i = 0; i < s; i++)
{
tempData = arr[i];
pos = s*tempData;
temp = list[pos];
check:
if (temp == NULL)
{
list[pos] = new bucket(tempData, NULL);
}
else if (temp->data > tempData)
{
list[pos] = new bucket(tempData, list[pos]);
}
else{
list[pos] = list[pos]->next;
goto check;
}
}
}
void join(float arr[], bucket* list[], int s) {
clrscr();
int j=0;
for (int i = 0; i < s; i++)
{
if (list[i] != NULL)
{
for (bucket* temp = list[i]; temp != NULL; temp =temp->next)
{
arr[j] = temp->data;
j++;
}
}
}
}
void main()
{
clrscr();
int size;
float array[10];
cout<<"Size: ";
cin>>size;
cout<<"Array (of size "<<size<<"): \n";
for (int i = 0; i < size; i++)
{
cin>>array[i];
}
getch();
clrscr();
cout<<"\n\n Your array: \n";
for (i = 0; i < size; i++)
{
cout<<array[i]<<" ";
}
getch();
clrscr();
//create bucket
bucket* list[10];
for (i = 0; i < size; i++)
{
list[i] = NULL;
}
bucketSort(list, array, size);
join(array, list, size);
cout<<"\n\n Sorted array: \n";
for (i = 0; i < size; i++)
{
cout<<array[i]<<" ";
}
getch();
}