Last active
April 11, 2020 02:38
-
-
Save mitrakmt/615e05975737f198d176ab23d844e0c1 to your computer and use it in GitHub Desktop.
Revisions
-
mitrakmt revised this gist
Oct 1, 2016 . 1 changed file with 3 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,10 +2,9 @@ function insertionSort(array) { var length = array.length; for(var i = 1; i < length; i++) { var temp = array[i]; for(var j = i - 1; j >= 0 && array[j] > temp; j--) { array[j+1] = array[j]; } array[j+1] = temp; @@ -59,4 +58,4 @@ function bucketSort(array, bucketSize) { }); return array; } -
mitrakmt revised this gist
Oct 1, 2016 . 1 changed file with 21 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,18 +20,20 @@ function bucketSort(array, bucketSize) { return array; } // Declaring vars var i, minValue = array[0], maxValue = array[0], bucketSize = bucketSize || 5; // Setting min and max values array.forEach(function (currentVal) { if (currentVal < minValue) { minValue = currentVal; } else if (currentVal > maxValue) { maxValue = currentVal; } }) // Initializing buckets var bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1; @@ -40,20 +42,21 @@ function bucketSort(array, bucketSize) { for (i = 0; i < allBuckets.length; i++) { allBuckets[i] = []; } // Pushing values to buckets array.forEach(function (currentVal) { allBuckets[Math.floor((currentVal - minValue) / bucketSize)].push(currentVal); }); // Sorting buckets array.length = 0; allBuckets.forEach(function(bucket) { insertionSort(bucket); bucket.forEach(function (element) { array.push(element) }); }); return array; } -
mitrakmt revised this gist
Oct 1, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -15,7 +15,7 @@ function insertionSort(array) { } // Implement bucket sort function bucketSort(array, bucketSize) { if (array.length === 0) { return array; } -
mitrakmt revised this gist
Oct 1, 2016 . 1 changed file with 20 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,7 @@ // InsertionSort to be used within bucket sort function insertionSort(array) { var length = array.length; for(var i = 1; i < length; ++i) { var temp = array[i]; var j = i - 1; @@ -8,18 +10,21 @@ function insertionSort(array) { } array[j+1] = temp; } return array; } // Implement bucket sort function sort(array, bucketSize) { if (array.length === 0) { return array; } var i, minValue = array[0], maxValue = array[0], bucketSize = bucketSize || 5; for (i = 1; i < array.length; i++) { if (array[i] < minValue) { minValue = array[i]; @@ -28,26 +33,25 @@ function sort(array, bucketSize) { } } // Initializing buckets var bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1; var allBuckets = new Array(bucketCount); for (i = 0; i < allBuckets.length; i++) { allBuckets[i] = []; } for (i = 0; i < array.length; i++) { allBuckets[Math.floor((array[i] - minValue) / bucketSize)].push(array[i]); } // Sorting buckets array.length = 0; for (i = 0; i < allBuckets.length; i++) { // Using helper insertion sort function to sort buckets insertionSort(buckets[i]); for (var j = 0; j < allBuckets[i].length; j++) { array.push(allBuckets[i][j]); } } -
mitrakmt revised this gist
Oct 1, 2016 . 1 changed file with 13 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,16 @@ function insertionSort(array) { var length = array.length; for(var i = 1; i < length; ++i) { var temp = array[i]; var j = i - 1; for(; j >= 0 && array[j] > temp; --j) { array[j+1] = array[j]; } array[j+1] = temp; } return array; } function sort(array, bucketSize) { if (array.length === 0) { return array; -
mitrakmt created this gist
Oct 1, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ function sort(array, bucketSize) { if (array.length === 0) { return array; } // Determine minimum and maximum values var i; var minValue = array[0]; var maxValue = array[0]; for (i = 1; i < array.length; i++) { if (array[i] < minValue) { minValue = array[i]; } else if (array[i] > maxValue) { maxValue = array[i]; } } // Initialise buckets var DEFAULT_BUCKET_SIZE = 5; bucketSize = bucketSize || DEFAULT_BUCKET_SIZE; var bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1; var buckets = new Array(bucketCount); for (i = 0; i < buckets.length; i++) { buckets[i] = []; } // Distribute input array values into buckets for (i = 0; i < array.length; i++) { buckets[Math.floor((array[i] - minValue) / bucketSize)].push(array[i]); } // Sort buckets and place back into input array array.length = 0; for (i = 0; i < buckets.length; i++) { insertionSort(buckets[i]); for (var j = 0; j < buckets[i].length; j++) { array.push(buckets[i][j]); } } return array; }