Advanced Coding Patterns for Interviews

0% completed

Previous
Next
Bucket Sort Algorithm

Bucket Sort is a comparison-based sorting algorithm that distributes elements into several 'buckets'. Each bucket is then sorted individually, either using another sorting algorithm or recursively applying the bucket sort. Finally, the sorted buckets are concatenated to form the final sorted array. This algorithm is particularly effective when the input is uniformly distributed over a range.

Bucket Sort works in the following steps:

  1. Distribute Elements into Buckets: Divide the input array into a number of buckets.
  2. Sort Each Bucket: Sort each bucket individually.
  3. Concatenate Buckets: Concatenate the sorted buckets to form the final sorted array.

Bucket Sort can achieve a linear time complexity of O(n) in the best case scenario, where the elements are uniformly distributed. However, in the worst case, when all elements fall into the same bucket, the time complexity can degrade to O(n log n).

Step-by-Step Algorithm for Bucket Sort

  1. Initialize Buckets:

    • Determine the number of buckets: The number of buckets is typically the square root of the number of elements, numBuckets = sqrt(n).
    • Create an array of buckets: Each bucket is an empty list to hold the elements.
  2. Distribute Elements into Buckets:

    • Find the maximum value in the array: This helps in distributing elements uniformly.
    • Calculate the index for each element: For each element num in the array, calculate the bucket index as bucketIndex = (num * numBuckets) / (maxVal + 1).
    • Place each element into its corresponding bucket: Add the element to the list at buckets[bucketIndex].
  3. Sort Each Bucket:

    • For each bucket, sort the elements using a comparison-based sorting algorithm (e.g., Timsort provided by Collections.sort in Java).
  4. Concatenate Buckets:

    • Combine the sorted buckets into the original array.

Algorithm Walkthrough

Input: array1 = {29, 25, 3, 49, 9, 37, 21, 43, 45}

Image
  1. Initialize Buckets:

    • Determine the number of buckets: numBuckets = sqrt(9) ≈ 3.
    • Create an array of 3 empty buckets: buckets = [[], [], []].
  2. Distribute Elements into Buckets:

    • Find the maximum value: maxVal = 49.

    • Calculate the bucket index and place each element into its corresponding bucket:

      • For 29: bucketIndex = (29 * 3) / 50 ≈ 1. Place 29 into buckets[1].
      • For 25: bucketIndex = (25 * 3) / 50 ≈ 1. Place 25 into buckets[1].
      • For 3: bucketIndex = (3 * 3) / 50 ≈ 0. Place 3 into buckets[0].
      • For 49: bucketIndex = (49 * 3) / 50 ≈ 2. Place 49 into buckets[2].
      • For 9: bucketIndex = (9 * 3) / 50 ≈ 0. Place 9 into buckets[0].
      • For 37: bucketIndex = (37 * 3) / 50 ≈ 2. Place 37 into buckets[2].
      • For 21: bucketIndex = (21 * 3) / 50 ≈ 1. Place 21 into buckets[1].
      • For 43: bucketIndex = (43 * 3) / 50 ≈ 2. Place 43 into buckets[2].
      • For 45: bucketIndex = (45 * 3) / 50 ≈ 2. Place 45 into buckets[2].
    • After distribution, buckets look like:

      • buckets[0] = [3, 9]
      • buckets[1] = [29, 25, 21]
      • buckets[2] = [49, 37, 43, 45]
  3. Sort Each Bucket:

    • Sort each bucket individually:
      • buckets[0] becomes [3, 9].
      • buckets[1] becomes [21, 25, 29].
      • buckets[2] becomes [37, 43, 45, 49].
  4. Concatenate Buckets:

    • Combine the sorted buckets into the original array:
      • array = [3, 9, 21, 25, 29, 37, 43, 45, 49].

Code

Python3
Python3

. . . .

Complexity Analysis for Bucket Sort

Time Complexity

  1. Creating Buckets: Initializing buckets takes O(n) time.

  2. Distributing Elements into Buckets: This takes O(n) time.

  3. Sorting Each Bucket:

    • If the input elements are uniformly distributed, the number of elements in each bucket is approximately O(1).
    • Sorting each bucket using a comparison-based sort like Timsort (which is used by Collections.sort()) takes O(k log k) time, where k is the number of elements in a bucket.
    • Since there are O(√n) buckets, the total time for sorting all buckets is O(n * (1/√n) log (1/√n)) = O(n log n/√n) = O(n log n).
  4. Concatenating Buckets:

    • Combining the sorted buckets to form the final sorted array takes O(n) time.

Overall Time Complexity:

  • Best Case: O(n + k log k), where k is the number of elements in a bucket. If the elements are uniformly distributed and k is small, this simplifies to O(n).
  • Worst Case: O(n log n), considering a fairly uniform distribution of elements.

Space Complexity

  • The space complexity for Bucket Sort includes the space for the buckets and the additional space used by the sorting algorithm.
  • Buckets require O(n) space.
  • The sorting algorithm within each bucket can use O(k) additional space, but since k is small compared to n, this is negligible.

Overall Space Complexity: O(n), which includes the original array and the buckets.

Real-Time Applications of Bucket Sort

  1. Sorting Floating Point Numbers

    • Application: Bucket Sort is particularly effective for sorting floating point numbers that are uniformly distributed over a range.
    • Example: Sorting grades or percentages in educational institutions.
  2. Data Distribution Analysis

    • Application: When analyzing large datasets, Bucket Sort can be used to categorize and sort data points into different ranges or buckets.
    • Example: Sorting temperature readings or rainfall measurements for climate studies.
  3. Digital Signal Processing

    • Application: In digital signal processing, Bucket Sort can help in sorting and organizing signal data points.
    • Example: Sorting frequency components in a Fast Fourier Transform (FFT) algorithm.
  4. Parallel Processing

    • Application: Bucket Sort is well-suited for parallel processing, where different buckets can be processed and sorted independently.
    • Example: Sorting large datasets in distributed computing environments or multi-core processors.
  5. External Sorting

    • Application: Bucket Sort can be used in external sorting, where data that does not fit into memory is divided into chunks that are sorted independently.
    • Example: Sorting large log files or database records stored on disk.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next