-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAA_Bucket_Sort.cpp
116 lines (96 loc) · 1.78 KB
/
DAA_Bucket_Sort.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
109
110
111
112
113
114
115
116
#include <iostream>
using namespace std;
struct Node
{
int value;
struct Node* next;
};
struct Bucket
{
struct Node *head;
};
struct BucketList
{
int V;
struct Bucket * array;
};
struct Node* newNode(int value)
{
struct Node* newnode = new Node;
newnode->value = value;
newnode->next = NULL;
return newnode;
}
struct BucketList* createBucket(int V)
{
int i;
struct BucketList* bl = new BucketList;
bl->V = V;
bl->array = new Bucket[V];
for(i = 0; i < V; i++)
bl->array[i].head = NULL;
return bl;
}
void addNode(struct BucketList* bl, int bckt, int value)
{
struct Node *newnode = newNode(value);
struct Node *temp = new Node;
if(bl->array[bckt].head != NULL)
{
temp = bl->array[bckt].head;
if(temp->value > newnode->value)
{
newnode->next = bl->array[bckt].head;
bl->array[bckt].head = newnode;
}
else
{
while(temp->next != NULL)
{
if((temp->next)->value > newnode->value)
break;
temp = temp->next;
}
newnode->next = temp->next;
temp->next = newnode;
}
}
else
{
bl->array[bckt].head = newnode;
}
}
void printBuckets(struct BucketList *bl)
{
int v;
struct Node* pCrawl = new Node;
for(v = 0; v < bl->V; v++)
{
pCrawl = bl->array[v].head;
while (pCrawl != NULL)
{
cout<<"->"<< pCrawl->value;
pCrawl = pCrawl->next;
}
}
}
int main()
{
int V = 10, range, NOE, i;
struct BucketList* mybucket = createBucket(V);
cout<<"Enter the upper limit in the power of 10 (10 or 100 or 1000 ..) to create Bucket: ";
cin>>range;
range = range/10;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>NOE;
int arr[100];
for(i = 0; i < NOE; i++)
{
cout<<"Enter element "<<i+1<<" : ";
cin>>arr[i];
addNode(mybucket, arr[i]/range, arr[i]);
}
cout<<"\nSorted Data ";
printBuckets(mybucket);
return 0;
}