Implementing Bucket Sort in JavaScript

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the bucket sort algorithm in JavaScript. Bucket sort 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. This lab will provide you with an opportunity to implement this algorithm and gain a deeper understanding of how it works.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28181{{"`Implementing Bucket Sort in JavaScript`"}} javascript/data_types -.-> lab-28181{{"`Implementing Bucket Sort in JavaScript`"}} javascript/arith_ops -.-> lab-28181{{"`Implementing Bucket Sort in JavaScript`"}} javascript/comp_ops -.-> lab-28181{{"`Implementing Bucket Sort in JavaScript`"}} javascript/higher_funcs -.-> lab-28181{{"`Implementing Bucket Sort in JavaScript`"}} javascript/spread_rest -.-> lab-28181{{"`Implementing Bucket Sort in JavaScript`"}} end

Bucket Sort Algorithm

To use the bucket sort algorithm and sort an array of numbers, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Find the minimum and maximum values of the given array using Math.min(), Math.max() and the spread operator (...).
  3. Create the appropriate number of buckets (empty arrays) using Array.from() and Math.floor().
  4. Populate each bucket with the appropriate elements from the array using Array.prototype.forEach().
  5. Sort each bucket and append it to the result using Array.prototype.reduce(), the spread operator (...) and Array.prototype.sort().

Here is an example implementation of the bucket sort algorithm in JavaScript:

const bucketSort = (arr, size = 5) => {
  const min = Math.min(...arr);
  const max = Math.max(...arr);
  const buckets = Array.from(
    { length: Math.floor((max - min) / size) + 1 },
    () => []
  );
  arr.forEach((val) => {
    buckets[Math.floor((val - min) / size)].push(val);
  });
  return buckets.reduce((acc, b) => [...acc, ...b.sort((a, b) => a - b)], []);
};

To test the algorithm, run the following code:

bucketSort([6, 3, 4, 1]); // [1, 3, 4, 6]

Summary

Congratulations! You have completed the Bucket Sort lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like