From 9cd9b16beef5d29fe56e7ed3d79afb56a96cf239 Mon Sep 17 00:00:00 2001 From: KuLi Date: Tue, 16 Oct 2018 19:46:30 +0200 Subject: [PATCH] Implemented bucket sort algorithm --- Sorts/bucketSort.js | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Sorts/bucketSort.js diff --git a/Sorts/bucketSort.js b/Sorts/bucketSort.js new file mode 100644 index 0000000000..7bcb77c794 --- /dev/null +++ b/Sorts/bucketSort.js @@ -0,0 +1,62 @@ +/* +Wikipedia says: Bucket sort, or bin 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. It is a +distribution sort, and is a cousin of radix sort in the most to least significant digit flavour. +Bucket sort is a generalization of pigeonhole sort. Bucket sort can be implemented with comparisons +and therefore can also be considered a comparison sort algorithm. The computational complexity estimates +involve the number of buckets. + +Time Complexity of Solution: +Best Case O(n); Average Case O(n); Worst Case O(n) + +*/ +function bucketSort(list, size){ + + if(undefined === size){ + size = 5; + } + if(list.length === 0){ + return list; + } + let min = list[0]; + let max = list[0]; + // find min and max + for(let iList = 0; iList < list.length; iList++){ + + if(list[iList] < min){ + min = list[iList]; + } else if(list[iList] > max){ + max = list[iList]; + } + } + // how many buckets we need + let count = Math.floor((max - min) / size) + 1; + + // create buckets + let buckets = []; + for(let iCount = 0; iCount < count; iCount++){ + buckets.push([]); + } + + // bucket fill + for(let iBucket = 0; iBucket < list.length; iBucket++){ + let key = Math.floor((list[iBucket] - min) / size); + buckets[key].push(list[iBucket]) + } + let sorted = []; + // now sort every bucket and merge it to the sorted list + for(let iBucket = 0; iBucket < buckets.length; iBucket++){ + let arr = buckets[iBucket].sort(); + for(let iSorted = 0; iSorted < arr.length; iSorted++){ + sorted.push(arr[iSorted]); + } + } + return sorted; +} +let arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]; +//Array before Sort +console.log(arrOrignal); +arrSorted = bucketSort(arrOrignal); +//Array after sort +console.log(arrSorted); \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy