|
| 1 | +var array = [0.21, 0.44, 0.221, 0.01, 0.88]; |
| 2 | + |
| 3 | +/** |
| 4 | + * Bucketsort. This algorithm has complexity O(n) but it's not |
| 5 | + * correct for every input. |
| 6 | + * |
| 7 | + * @public |
| 8 | + */ |
| 9 | +var bucketSort = (function () { |
| 10 | + |
| 11 | + /** |
| 12 | + * Insertionsort. |
| 13 | + * |
| 14 | + * @private |
| 15 | + * @param {array} array Input array |
| 16 | + * @returns {array} array Sorted input array |
| 17 | + */ |
| 18 | + function insertionSort(array) { |
| 19 | + var current, |
| 20 | + j; |
| 21 | + for (var i = 1; i < array.length; i += 1) { |
| 22 | + current = array[i]; |
| 23 | + j = i - 1; |
| 24 | + while (j >= 0 && current < array[j]) { |
| 25 | + array[j + 1] = array[j]; |
| 26 | + j -= 1; |
| 27 | + } |
| 28 | + array[j + 1] = current; |
| 29 | + } |
| 30 | + return array; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Creates buckets for given array |
| 35 | + * |
| 36 | + * @private |
| 37 | + * @param {array} array Input array |
| 38 | + * @returns {array} buckets Array whith array for each bucket. |
| 39 | + * Each bucket contains an array with all elements from the input which are with suitable size. |
| 40 | + */ |
| 41 | + function createBuckets(array) { |
| 42 | + var buckets = [], |
| 43 | + currentBucket, |
| 44 | + current, |
| 45 | + sectorSize = 1 / array.length; |
| 46 | + for (var i = 0; i < array.length; i += 1) { |
| 47 | + current = array[i]; |
| 48 | + console.log(current / sectorSize); |
| 49 | + currentBucket = Math.floor(current / sectorSize); |
| 50 | + if (buckets[currentBucket] === undefined) { |
| 51 | + buckets[currentBucket] = []; |
| 52 | + } |
| 53 | + buckets[currentBucket].push(current); |
| 54 | + } |
| 55 | + return buckets; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Sorts the arrays from each bucket. |
| 60 | + * |
| 61 | + * @private |
| 62 | + * @param {array} buckets Given buckets |
| 63 | + * @returns {array} buckets Buckets with sorted arrays for each bucket |
| 64 | + */ |
| 65 | + function sortBuckets(buckets) { |
| 66 | + for (var i = 0; i < buckets.length; i += 1) { |
| 67 | + if (buckets[i] !== undefined) |
| 68 | + insertionSort(buckets[i]); |
| 69 | + } |
| 70 | + return buckets; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Unions all buckets' arrays |
| 75 | + * |
| 76 | + * @private |
| 77 | + * @param {array} buckets Input buckets |
| 78 | + * @returns {array} result Sorted array which contains all elements form each bucket |
| 79 | + */ |
| 80 | + function unionBuckets(buckets) { |
| 81 | + var result = [], |
| 82 | + currentBucket; |
| 83 | + for (var i = 0; i < buckets.length; i += 1) { |
| 84 | + currentBucket = buckets[i]; |
| 85 | + if (currentBucket !== undefined) { |
| 86 | + for (var j = 0; j < currentBucket.length; j += 1) { |
| 87 | + result.push(currentBucket[j]); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return result; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Sorts given array with bucketsort |
| 96 | + * |
| 97 | + * @public |
| 98 | + * @param {array} array Input array which should be sorted |
| 99 | + * @returns {array} Sorted array |
| 100 | + */ |
| 101 | + return function (array) { |
| 102 | + var buckets = createBuckets(array); |
| 103 | + sortBuckets(buckets); |
| 104 | + return unionBuckets(buckets); |
| 105 | + }; |
| 106 | +}()); |
| 107 | + |
| 108 | +console.log(bucketSort(array)); |
0 commit comments