GeezerVison is showing github.com — Implemented bucket sort · Womol/AlgorithmVisualizer@eb49d9e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: algorithm/category.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 69 | 69 | }, | 70 | 70 | "sorting": { | 71 | 71 | "list": { | | 72 | + "bucket": "Bucket Sort", | 72 | 73 | "bubble": "Bubble Sort", | 73 | 74 | "comb": "Comb Sort", | 74 | 75 | "counting": "Counting Sort", | |
Collapse file Copy file name to clipboard +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +//place numbers into appropriate buckets | | 2 | +for (let i = 0; i < array.length; i++) { | | 3 | + var bucketPos = Math.floor(numBuckets * (array[i] / maxValue)); | | 4 | + buckets[bucketPos].push(array[i]); | | 5 | + bucketsCount[bucketPos]++; | | 6 | + tracer._select(0, i)._wait(); | | 7 | + tracer._notify(1, bucketPos, D[1][bucketPos])._wait(); | | 8 | + tracer._deselect(0, i); | | 9 | + tracer._denotify(1, bucketPos, D[1][bucketPos]); | | 10 | +} | | 11 | + | | 12 | +var sortLocation = 0; | | 13 | +for (let k = 0; k < buckets.length; k++) { | | 14 | + //do insertion sort | | 15 | + for (let i = 1; i < buckets[k].length; i++) { | | 16 | + var key = buckets[k][i]; | | 17 | + var j; | | 18 | + for (j = i - 1; (j >= 0) && (buckets[k][j] > key); j--) { | | 19 | + buckets[k][j + 1] = buckets[k][j]; | | 20 | + } | | 21 | + buckets[k][j + 1] = key; | | 22 | + } | | 23 | + | | 24 | + //place ordered buckets into sorted array | | 25 | + for (let i = 0; i < buckets[k].length; i++) { | | 26 | + sortedArray[sortLocation] = buckets[k][i]; | | 27 | + bucketsCount[k]--; | | 28 | + tracer._notify(1, k, D[1][k]); | | 29 | + tracer._notify(2, sortLocation, D[2][sortLocation])._wait(); | | 30 | + tracer._denotify(1, k, D[1][k]); | | 31 | + tracer._denotify(2, sortLocation, D[2][sortLocation]); | | 32 | + sortLocation++; | | 33 | + } | | 34 | +} |
Collapse file Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +var maxValue = 100; | | 2 | +var arraySize = 10; | | 3 | +var numBuckets = 5; | | 4 | + | | 5 | +//initialize array values | | 6 | +var array = Array1D.random(arraySize, 0, maxValue - 1); | | 7 | +var buckets = []; | | 8 | +var bucketsCount = []; | | 9 | +var sortedArray = []; | | 10 | +for (let i = 0; i < arraySize; i++) { | | 11 | + if (i < numBuckets) { | | 12 | + buckets[i] = []; | | 13 | + bucketsCount[i] = 0; | | 14 | + } | | 15 | + sortedArray[i] = 0; | | 16 | +} | | 17 | +var D = [ | | 18 | + array, | | 19 | + bucketsCount, | | 20 | + sortedArray | | 21 | +]; | | 22 | + | | 23 | +var tracer = new Array2DTracer(); | | 24 | +tracer._setData(D); |
Collapse file Copy file name to clipboard +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + "Bucket Sort": "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.", | | 3 | + "Complexity": { | | 4 | + "time": "worst O(n<sup>2</sup>), best O(n+k), average O(n+k) where n is the number of buckets and k is the range of the input", | | 5 | + "space": "worst O(n*k)" | | 6 | + }, | | 7 | + "References": [ | | 8 | + "<a href='https://en.wikipedia.org/wiki/Bucket_sort'>Wikipedia</a>" | | 9 | + ], | | 10 | + "files": { | | 11 | + "basic": "Bucket sort" | | 12 | + } | | 13 | +} |
|