Bucket and Heap
Bucket and Heap
Algorithm
Data Structures and Algorithms
Bucket sort
is a sorting technique that involves dividing
elements into various groups, or buckets.
These buckets are formed by uniformly
distributing the elements. Once the elements
are divided into buckets, they can be sorted
using any other sorting algorithm. Finally, the
sorted elements are gathered together in an
ordered fashion.
Bucket Sort Algorithm:
Create n empty buckets (Or lists) and
do the following for every array
element arr[i].
• Insert arr[i] into bucket[n*array[i]]
• Sort individual buckets using
insertion sort.
• Concatenate all sorted buckets.
How does Bucket Sort work?
To apply bucket sort on the input array [0.78, 0.17, 0.39, 0.26,
0.72, 0.94, 0.21, 0.12, 0.23, 0.68], we follow these steps:
• Step 1: Create an array of size 10, where each slot represents
a bucket.
Bucket Sort Algorithm:
Step 2: Insert elements into the buckets from the
input array based on their range.
Inserting elements into the buckets:
• Take each element from the input array.
• Multiply the element by the size of the bucket
array (10 in this case). For example, for element
0.23, we get 0.23 * 10 = 2.3.
• Convert the result to an integer, which gives us
the bucket index. In this case, 2.3 is converted to
the integer 2.
• Insert the element into the bucket corresponding
to the calculated index.
• Repeat these steps for all elements in the input
array.
Bucket Sort Algorithm:
Bucket Sort Algorithm:
Step 3: Sort the elements within each bucket. In this example, we use quicksort (or
any stable sorting algorithm) to sort the elements within each bucket.
Sorting the elements within each bucket:
• Apply a stable sorting algorithm (e.g., Bubble Sort, Merge Sort) to sort the
elements within each bucket.
• The elements within each bucket are now sorted.
Bucket Sort Algorithm:
Step 4: Gather the elements from each bucket and put them back into the original array.
Gathering elements from each bucket:
• Iterate through each bucket in order.
• Insert each individual element from the bucket into the original array.
• Once an element is copied, it is removed from the bucket.
• Repeat this process for all buckets until all elements have been gathered.
Bucket Sort Algorithm:
Step 5: The original array now contains the sorted elements.
The final sorted array using bucket sort for the given input is [0.12, 0.17,
0.21, 0.23, 0.26, 0.39, 0.68, 0.72, 0.78, 0.94].
Heap Data
Structure
Data Structures and Algorithms
Heap
is a complete binary tree data structure that
satisfies the heap property: for every node, the
value of its children is greater than or equal to
its own value. Heaps are usually used to
implement priority queues, where the smallest
(or largest) element is always at the root of the
tree.
Heap Data Structure
Step 1: Treat the Array as a Complete Binary Tree
We first need to visualize the array as a complete binary
tree. For an array of size n, the root is at index 0, the left
child of an element at index i is at 2i + 1, and the right
child is at 2i + 2.
Heap Data Structure
Step 2: Build a Max Heap
Heap Data Structure
Step 3: Sort the array by placing largest element at end
of unsorted array.
Heap Data Structure
Heap Data Structure
Heap Data Structure
T h a n k
y o u !