Back to Course


0% Complete
0/82 Steps
  1. Getting Started with Algorithm
    What is an Algorithm?
  2. Characteristics of Algorithm
    1 Topic
  3. Analysis Framework
  4. Performance Analysis
    3 Topics
  5. Mathematical Analysis
    2 Topics
  6. Sorting Algorithm
    Sorting Algorithm
    10 Topics
  7. Searching Algorithm
    6 Topics
  8. Fundamental of Data Structures
  9. Queues
  10. Graphs
  11. Trees
  12. Sets
  13. Dictionaries
  14. Divide and Conquer
    General Method
  15. Binary Search
  16. Recurrence Equation for Divide and Conquer
  17. Finding the Maximum and Minimum
  18. Merge Sort
  19. Quick Sort
  20. Stassen’s Matrix Multiplication
  21. Advantages and Disadvantages of Divide and Conquer
  22. Decrease and Conquer
    Insertion Sort
  23. Topological Sort
  24. Greedy Method
    General Method
  25. Coin Change Problem
  26. Knapsack Problem
  27. Job Sequencing with Deadlines
  28. Minimum Cost Spanning Trees
    2 Topics
  29. Single Source Shortest Paths
    1 Topic
  30. Optimal Tree Problem
    1 Topic
  31. Transform and Conquer Approach
    1 Topic
  32. Dynamic Programming
    General Method with Examples
  33. Multistage Graphs
  34. Transitive Closure
    1 Topic
  35. All Pairs Shortest Paths
    6 Topics
  36. Backtracking
    General Method
  37. N-Queens Problem
  38. Sum of Subsets problem
  39. Graph Coloring
  40. Hamiltonian Cycles
  41. Branch and Bound
    2 Topics
  42. 0/1 Knapsack problem
    2 Topics
  43. NP-Complete and NP-Hard Problems
    1 Topic
Lesson 6, Topic 9
In Progress

Bucket Sort

Lesson Progress
0% Complete

Bucket Sort is a sorting technique that sorts the elements by first dividing the elements into several groups called buckets. The elements inside each bucket are sorted using any of the suitable sorting algorithms or recursively calling the same algorithm.

A number of buckets are created. Each bucket is filled with a specific range of elements. The elements inside the bucket are sorted using any other algorithm. Finally, the elements of the bucket are gathered to get the sorted array.

KodNest 1 9

The process of bucket sort can be understood as scatter-gather approach. The elements are first scattered into buckets then the elements of buckets are sorted. Finally, the elements are gathered in order.

How Bucket Sort Works?

  1. Suppose, the input array is:
KodNest 2 6

Create an array of size 10. Each slot of this array is used as a bucket for storing elements.

KodNest 3 5

2. Insert elements into the buckets from the array. The elements are inserted according to the range of the bucket.

In our example code, we have buckets each of ranges from 0 to 1, 1 to 2, 2 to 3,…… (n-1) to n.
Suppose, an input element is .23 is taken. It is multiplied by size = 10 (ie. .23*10=2.3). Then, it is converted into an integer (ie. 2.3≈2). Finally, .23 is inserted into bucket-2.

KodNest 4 4

Similarly, .25 is also inserted into the same bucket. Everytime, the floor value of thr floating point number is taken.

In a similar way, other elements are inserted into their respective buckets.

KodNest 5 3

3. The elements of each bucket are sorted using any of the stable sorting algorithms. Here, we have used quicksort (inbuilt function).

KodNest 6 2

4.The elements from each bucket are gathered.

It is done by iterating through the bucket and inserting an individual element into the original array in each cycle. The element from the bucket is erased once it is copied into the original array.

KodNest 7 1

Bucket Sort Algorithm

  create N buckets each of which can hold a range of values
  for all the buckets
    initialize each bucket with 0 values
  for all the buckets
    put elements into buckets matching the range
  for all the buckets
    sort elements in each bucket
  gather elements from each bucket
end bucketSort

Bucket Sort Program in C

// Bucket Sort in C programming
#include <stdio.h>
int getMax(int array[], int size)
  int max = array[0];
  for (int i = 1; i < size; i++)
    if (array[i] > max)
      max = array[i];
  return max;
void bucketSort(int array[], int size)
  // The size of bucket must be at least the (max+1) but
  // we cannot assign declare it as int bucket(max+1) in C as
  // it does not support dynamic memory allocation.
  // So, its size is provided statically.
  int bucket[10];
  const int max = getMax(array, size);
  for (int i = 0; i <= max; i++)
    bucket[i] = 0;
  for (int i = 0; i < size; i++)
  for (int i = 0, j = 0; i <= max; i++)
    while (bucket[i] > 0)
      array[j++] = i;
void printArray(int array[], int size)
  for (int i = 0; i < size; ++i)
    printf("%d  ", array[i]);
int main()
  int data[] = {4, 3, 4, 5, 6, 0, 9, 5};
  int size = sizeof(data) / sizeof(data[0]);
  bucketSort(data, size);
  printf("Sorted array in ascending order: \n");
  printArray(data, size);


  • Worst Case Complexity: O(n2)When there are elements of close range in the array, they are likely to be placed in the same bucket. This may result in some buckets having more number of elements than others.

    It makes the complexity depend on the sorting algorithm used to sort the elements of the bucket.

    The copmlexity becomes even worse when the elements are in reverse order. If insertion sort is used to sort elements of the bucket, then the time complexity becomes O(n2).
  • Best Case Complexity: O(n+k)It occurs when the elements are uniformly distributed in the buckets with a nearly equal number of elements in each bucket.

    The complexity becomes even better if the elements inside the buckets are already sorted.

    If insertion sort is used to sort elements of a bucket then the overall complexity in the best case will be linear ie. O(n+k)O(n) is the complexity for making the buckets and O(k) is the complexity for sorting the elements of the bucket using algorithm having linear time complexity at best case.
  • Average Case Complexity: O(n)It occurs when the elements are distributed randomly in the array. Even if the elements are not distributed uniformly, bucket sort runs in linear time. It holds true until the sum of the squares of the bucket sizes is linear in the total number of elements.

Bucket Sort Applications

Bucket sort is used when:

  • input is uniformly distributed over a range.
  • there are floating point values

New Report