Created
April 12, 2021 21:25
-
-
Save ButlerFuqua/4e3751c1302bfd3fb0f9f6b650cc4ae7 to your computer and use it in GitHub Desktop.
This file contains 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 characters
BucketSort(numbers, numbersSize, bucketCount) { | |
if (numbersSize < 1) | |
return | |
buckets = Create list of bucketCount buckets | |
// Find the maximum value | |
maxValue = numbers[0] | |
for (i = 1; i < numbersSize; i++) { | |
if (numbers[i] > maxValue) | |
maxValue = numbers[i] | |
} | |
// Put each number in a bucket | |
for each (number in numbers) { | |
index = floor(number * (bucketCount - 1) / maxValue) | |
Append number to buckets[index] | |
} | |
// Sort each bucket | |
for each (bucket in buckets) | |
Sort(bucket) | |
// Combine all buckets back into numbers list | |
result = Concatenate all buckets together | |
Copy result to numbers | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment