Wah Engineering College University of Wah
Wah Engineering College University of Wah
Section:
4th (B)
Department Of Electrical Engineering
Wah Engineering College
1
Wah Engineering College University of Wah
Table of Content
Introduction
What is Bucket Sort and how is it associated with
Algorithms?
Method
Algorithm
Assumption
Time Complexity
Example
Proof of Correctness
Problem Description
Problem Solution
Program/Source Code
Program Explanation
References
2
Wah Engineering College University of Wah
Introduction:
The bucket sort is a non-comparison-based sorting algorithm .
Allocate one storage location for each item to be sorted.
Assigning each item into its corresponding bucket.
In order to bucket sort n unique items in the range of 1 through m, allocate m buckets and
then iterate over the n items assigning each one to the proper bucket. Finally loop through
the buckets and collect the items putting them into final order.
Bucket sort work well for data sets where the possible key values are known and
relatively small and there are on average just a few elements per bucket.
We can perform bucket sort on any array of (non-negative) integers but number of
buckets will depend on the maximum integer value.
Method:
Throws the numbers in their right buckets.
Sort each bucket with regular insertion sort.
Concatenate the buckets.
Algorithm:
BucketSort(A)
1 n = length [A]
2 for i = 1 to n
3 do insert A [i] into list B [n A [i]]
4 for i = 0 to n - 1
5 do sort list B [i] with insertion sort
6 concatenate the lists B[0], B[1] ... B[n-1] together in order
3
Wah Engineering College University of Wah
Assumption:
Keys to be sorted are uniformly distributed over a known range (say 1 to m).
Time Complexity:
4
Wah Engineering College University of Wah
After Sorting:
Proof of Correctness:
If two items are in the same bucket then they are in proper relative order.
If two items are in two different buckets, even then they are in right order.
5
Wah Engineering College University of Wah
Problem Description:
1. We should implement Bucket Sort on uniformly distributed data over a range by splitting the range into equal parts.
2. Assign those parts as buckets and each bucket ‘i’ will be having ‘Ni’ number of the elements.
3. Selecting these Bucket for inserting will cost time complexity of O(N) where N is a total number of elements.
4. Sort them separately. We have used insertion sort which has a time complexity of summation of O(Ni^2).
5. Complexity for bucket sort is O(N + summation of(Ni^2)).
6. It is better than the other sorting algorithms (insertion sort, bubble sort, etc) with complexities O(N^2).
Problem Solution:
1. Divide the range into equal parts and assign a bucket to each part.
2. Split the data and insert them into the corresponding bucket using insertion sort.
3. Merge all the buckets into one.
4. Display the result.
5. Exit.
Program/Source Code:
1. A C++ Program to demonstrate Bucket Sort using adjacency list.
This program is successfully run on Dev-C++ using TDM-GCC 4.9.2 MinGW compiler on a Windows
system.
2. #include <iostream>
3.
4. using namespace std;
5.
6. // A structure to represent a node.
7. struct Node
8. {
9. int value;
10. struct Node* next;
11. };
12.
13. // A structure to represent a Head Bucket Node of the bucket list.
14. struct Bucket
15. {
16. // Pointer to head node of Bucket.
17. struct Node *head;
18. };
19.
20. struct BucketList
21. {
22. int V;
23. struct Bucket * array;
6
Wah Engineering College University of Wah
24. };
25.
26.
27. // A utility function to create a new node for a particular entry in a bucket.
28. struct Node* newNode(int value)
29. {
30. struct Node* newnode = new Node;
31. newnode->value = value;
32. newnode->next = NULL;
33. return newnode;
34. }
35.
36. // A utility function that creates a list of the bucket over the range of input data.
37. struct BucketList* createBucket(int V)
38. {
39. int i;
40. struct BucketList* bl = new BucketList;
41.
42. bl->V = V;
43. bl->array = new Bucket[V];
44.
45.
46. // Initialize each Bucket list as empty by making head as NULL.
47. for(i = 0; i < V; i++)
48. bl->array[i].head = NULL;
49.
50. return bl;
51. }
52.
53. // A function to Insert the nodes to corresponding Buckets.
54. void addNode(struct BucketList* bl, int bckt, int value)
55. {
56. // Creating new data node.
57. struct Node *newnode = newNode(value);
58. struct Node *temp = new Node;
59.
60. if(bl->array[bckt].head != NULL)
61. {
62. temp = bl->array[bckt].head;
63.
64. // Sorting.
65. // If the head node value is lesser than the newnode value, then add node
at beginning.
66. if(temp->value > newnode->value)
67. {
68. newnode->next = bl->array[bckt].head;
69. bl->array[bckt].head = newnode;
70. }
71. else
72. {
73. // Search for the node whose value is more than the newnode
value.
74. while(temp->next != NULL)
75. {
76. if((temp->next)->value > newnode->value)
77. break;
7
Wah Engineering College University of Wah
78.
79. temp = temp->next;
80. }
81.
82. // Insert newnode after temp node.
83. newnode->next = temp->next;
84. temp->next = newnode;
85. }
86. }
87. else
88. {
89. // Assign head of the Bucket as newnode since bucket head is NULL.
90. bl->array[bckt].head = newnode;
91. }
92. }
93.
94. // A function to print the result as sorted Data.
95. void printBuckets(struct BucketList *bl)
96. {
97. int v;
98. struct Node* pCrawl = new Node;
99.
100. for(v = 0; v < bl->V; v++)
101. {
102. // To view the data in individual bucket remove next line from
comment.
103. // cout<<"\n\t bucket "<<v+1;
104.
105. pCrawl = bl->array[v].head;
106. while (pCrawl != NULL)
107. {
108. cout<<"->"<< pCrawl->value;
109. pCrawl = pCrawl->next;
110. }
111. }
112. }
113.
114.
115. int main()
116. {
117. // Create the BucketLists for the data and set 10 as default number of
Buckets.
118. int V = 10, range, NOE, i;
119. struct BucketList* mybucket = createBucket(V);
120.
121.
122. cout<<"\n\nEnter the upper limit in the power of 10 (10 or 100 or
1000 ..) to create Bucket: ";
123. cin>>range;
124.
125. // Dividing range into 10 parts so it will have 10 buckets as default.
126. range = range/10;
127.
128. cout<<"\nEnter the number of data element to be sorted: ";
129. cin>>NOE;
130. int arr[NOE];
8
Wah Engineering College University of Wah
131.
132. for(i = 0; i < NOE; i++)
133. {
134. cout<<"Enter element "<<i+1<<" : ";
135. cin>>arr[i];
136. addNode(mybucket, arr[i]/range, arr[i]);
137. }
138.
139. // Print the adjacency list representation of the BucketList i.e the
sorted Output.
140. cout<<"\nSorted Data ";
141. printBuckets(mybucket);
142.
143. return 0;
144.}
Program Explanation:
References:
https://www.sanfoundry.com/cpluscplus-program-implement-bucket-sort/
https://en.wikipedia.org/wiki/Bucket_sort
https://www.geeksforgeeks.org/bucket-sort-2/
https://www.tutorialspoint.com/Bucket-Sort
9
Wah Engineering College University of Wah
10