Class BucketSort<T>

BucketSort is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm or by recursively applying the bucket sorting algorithm.

Bucket sort is mainly useful when input is uniformly distributed over a range. The algorithm has a time complexity of O(n + k) in the best and average case, where n is the number of elements and k is the number of buckets. The worst-case time complexity is O(n^2) when all elements are placed in the same bucket.

Note: This implementation is optimized for numeric values. For non-numeric types, consider using other sorting algorithms.

Example

const bucketSort = new BucketSort([0.42, 0.32, 0.33, 0.52, 0.37, 0.47, 0.51]);
bucketSort.sort(); // returns new array with [0.32, 0.33, 0.37, 0.42, 0.47, 0.51, 0.52]
// or
BucketSort.sort([0.42, 0.32, 0.33, 0.52, 0.37, 0.47, 0.51]); // returns new array with [0.32, 0.33, 0.37, 0.42, 0.47, 0.51, 0.52]

Example

const bucketSort = new BucketSort(
[5, 3, 8, 6, 2, 7, 1, 4],
(a : number, b : number) => a - b,
10
);
bucketSort.sort(); // returns new array with [1, 2, 3, 4, 5, 6, 7, 8]

Type Parameters

  • T

Hierarchy

  • BucketSort

Constructors

  • Type Parameters

    • T

    Parameters

    • array: T[] = []
    • comparatorFn: ((a, b) => number) = BucketSort.defaultComparator
        • (a, b): number
        • Default comparator function used for sorting

          Parameters

          • a: any

            first element to compare

          • b: any

            second element to compare

          Returns number

          a negative number if a < b, 0 if a = b, a positive number if a > b

    • bucketCount: number = 10

    Returns BucketSort<T>

Properties

bucketCount: number
defaultComparator: ((a, b) => number)

Type declaration

    • (a, b): number
    • Parameters

      • a: T
      • b: T

      Returns number

size: number
values: T[]

Methods

  • Sorts the array using the BucketSort algorithm.

    Returns T[]

    a new array with the elements sorted

    Example

    const bucketSort = new BucketSort([0.42, 0.32, 0.33, 0.52, 0.37, 0.47, 0.51]);
    bucketSort.sort(); // returns new array with [0.32, 0.33, 0.37, 0.42, 0.47, 0.51, 0.52]
  • Default comparator function used for sorting

    Parameters

    • a: any

      first element to compare

    • b: any

      second element to compare

    Returns number

    a negative number if a < b, 0 if a = b, a positive number if a > b

  • Sorts the array using the BucketSort algorithm. Static method, no need to instantiate class

    Parameters

    • array: any[]

      the array to be sorted

    • comparatorFn: ((a, b) => number) = ...

      the comparator function used for sorting, defaults to numerical comparison

        • (a, b): number
        • Default comparator function used for sorting

          Parameters

          • a: any

            first element to compare

          • b: any

            second element to compare

          Returns number

          a negative number if a < b, 0 if a = b, a positive number if a > b

    • bucketCount: number = 10

      the number of buckets to use, defaults to 10

    Returns any[]

    a new array with the elements sorted

Generated using TypeDoc