From 78dd867ad1cb42a6059277d57b933f2d0be7f425 Mon Sep 17 00:00:00 2001 From: WalterMarch Date: Wed, 28 Feb 2024 09:17:33 +0000 Subject: [PATCH 1/4] BucketSort readme from PR 687 --- Algorithms/Sorting/BucketSort/readme.md | 403 ++++++++++++++++++++++++ 1 file changed, 403 insertions(+) create mode 100644 Algorithms/Sorting/BucketSort/readme.md diff --git a/Algorithms/Sorting/BucketSort/readme.md b/Algorithms/Sorting/BucketSort/readme.md new file mode 100644 index 00000000..e0c70914 --- /dev/null +++ b/Algorithms/Sorting/BucketSort/readme.md @@ -0,0 +1,403 @@ +# Bucket Sort +Bucket 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 sort algorithm. After all the individual buckets are sorted, the elements are concatenated together to form the sorted array. + +## Complexity +| Best | Average | Worst | Memory | Stable | +|------|---------|-------|--------|--------| +| log(n+k) | log(n+k) | log(n^2) | n | Yes | + + +## Pseudo Code +``` +BucketSort(arr, n): + # Find the minimum and maximum values in the array + min_val = arr[0] + max_val = arr[0] + for i from 1 to n-1: + if arr[i] < min_val: + min_val = arr[i] + if arr[i] > max_val: + max_val = arr[i] + # Determine the range and the number of buckets + range = max_val - min_val + num_buckets = n + bucket_size = range / num_buckets + # Create an array of empty buckets + buckets = array of empty lists, one for each bucket + # Place each element in the appropriate bucket + for i from 0 to n-1: + index = (arr[i] - min_val) / bucket_size + insert arr[i] into buckets[index] + # Sort each bucket and concatenate them to get the sorted array + index = 0 + for i from 0 to num_buckets-1: + sort(buckets[i]) # You can use any sorting algorithm, e.g., insertion sort + for each element in buckets[i]: + arr[index] = element + index = index + 1 +``` + +## Implementations +* [Python](#python)! +* [C++](#cpp)! +* [C](#c)! +* [Java](#java)! +* [JavaScript](#javascript) +* [Go](#go)! +* [Ruby](#ruby)! + + +### Python +```python +def bucket_sort(arr): + # Find the minimum and maximum values in the input array + min_val, max_val = min(arr), max(arr) + + # Determine the range of each bucket + bucket_range = (max_val - min_val) / len(arr) + + # Create empty buckets + num_buckets = len(arr) + buckets = [[] for _ in range(num_buckets)] + + # Place each element in the appropriate bucket + for num in arr: + index = int((num - min_val) / bucket_range) + if index == num_buckets: + index -= 1 # Adjust for values equal to max_val + buckets[index].append(num) + + # Sort each bucket and concatenate them to get the sorted array + sorted_arr = [] + for bucket in buckets: + sorted_arr.extend(sorted(bucket)) + + return sorted_arr + +# Example usage: +arr = [3.2, 0.4, 2.8, 4.5, 1.1, 0.9] +sorted_arr = bucket_sort(arr) +print(sorted_arr) +``` + +### CPP +```cpp +#include +#include +#include + +std::vector bucketSort(std::vector arr) { + // Find the minimum and maximum values in the input vector + double minVal = *min_element(arr.begin(), arr.end()); + double maxVal = *max_element(arr.begin(), arr.end()); + + // Create empty buckets + int numBuckets = arr.size(); + double bucketRange = (maxVal - minVal) / numBuckets; + std::vector> buckets(numBuckets); + + // Place each element in the appropriate bucket + for (double num : arr) { + int index = static_cast((num - minVal) / bucketRange); + if (index == numBuckets) { + index--; // Adjust for values equal to maxVal + } + buckets[index].push_back(num); + } + + // Sort each bucket and concatenate them to get the sorted vector + std::vector sortedArr; + for (auto& bucket : buckets) { + std::sort(bucket.begin(), bucket.end()); + sortedArr.insert(sortedArr.end(), bucket.begin(), bucket.end()); + } + + return sortedArr; +} + +int main() { + std::vector arr = {3.2, 0.4, 2.8, 4.5, 1.1, 0.9}; + std::vector sortedArr = bucketSort(arr); + + for (double num : sortedArr) { + std::cout << num << " "; + } + std::cout << std::endl; + + return 0; +} + +``` +### C +```c +#include +#include +// Define a structure for a linked list node +struct Node { + double data; + struct Node* next; +}; +// Function to insert a value into a bucket (linked list) +void insert(struct Node** bucket, double value) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = value; + newNode->next = NULL; + // Insert the new node at the beginning of the bucket + if (*bucket == NULL) { + *bucket = newNode; + } else { + newNode->next = *bucket; + *bucket = newNode; + } +} +// Function to sort a bucket (linked list) +void sortBucket(struct Node** bucket) { + if (*bucket == NULL) return; // Empty bucket + // Perform an insertion sort on the bucket + struct Node* sorted = NULL; + struct Node* current = *bucket; + while (current != NULL) { + struct Node* next = current->next; + // Insert current node into the sorted list + if (sorted == NULL || current->data <= sorted->data) { + current->next = sorted; + sorted = current; + } else { + struct Node* search = sorted; + while (search->next != NULL && search->next->data < current->data) { + search = search->next; + } + current->next = search->next; + search->next = current; + } + current = next; + } + *bucket = sorted; +} +// Function to perform bucket sort +void bucketSort(double arr[], int size) { + // Find the minimum and maximum values in the input array + double minVal = arr[0]; + double maxVal = arr[0]; + for (int i = 1; i < size; i++) { + if (arr[i] < minVal) minVal = arr[i]; + if (arr[i] > maxVal) maxVal = arr[i]; + } + // Determine the range of values and the number of buckets + double range = maxVal - minVal; + int numBuckets = size; + double bucketSize = range / numBuckets; + // Create an array of linked lists (buckets) + struct Node* buckets[numBuckets]; + for (int i = 0; i < numBuckets; i++) { + buckets[i] = NULL; + } + // Place each element in the appropriate bucket + for (int i = 0; i < size; i++) { + int index = (int)((arr[i] - minVal) / bucketSize); + insert(&buckets[index], arr[i]); + } + // Sort each bucket and concatenate them to get the sorted array + int index = 0; + for (int i = 0; i < numBuckets; i++) { + sortBucket(&buckets[i]); + struct Node* current = buckets[i]; + while (current != NULL) { + arr[index++] = current->data; + current = current->next; + } + } +} +int main() { + double arr[] = {3.2, 0.4, 2.8, 4.5, 1.1, 0.9}; + int size = sizeof(arr) / sizeof(arr[0]); + bucketSort(arr, size); + printf("Sorted array: "); + for (int i = 0; i < size; i++) { + printf("%.1lf ", arr[i]); + } + printf("\n"); + return 0; +} +``` + +### Java +```java +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class BucketSort { + public static List bucketSort(List arr) { + // Find the minimum and maximum values in the input list + double minVal = Collections.min(arr); + double maxVal = Collections.max(arr); + + // Determine the number of buckets based on the range of values + int numBuckets = arr.size(); // Or choose an appropriate number of buckets + List > buckets = new ArrayList<>(numBuckets); + + for (int i = 0; i < numBuckets; i++) { + buckets.add(new ArrayList<>()); + } + + // Place each element in the appropriate bucket + double bucketRange = (maxVal - minVal) / numBuckets; + for (Double num : arr) { + int index = (int) ((num - minVal) / bucketRange); + if (index == numBuckets) { + index--; // Adjust for values equal to maxVal + } + buckets.get(index).add(num); + } + + // Sort each bucket and concatenate them to get the sorted list + List sortedArr = new ArrayList<>(); + for (List bucket : buckets) { + Collections.sort(bucket); + sortedArr.addAll(bucket); + } + + return sortedArr; + } + + public static void main(String[] args) { + List arr = new ArrayList<>(List.of(3.2, 0.4, 2.8, 4.5, 1.1, 0.9)); + List sortedArr = bucketSort(arr); + System.out.println(sortedArr); + } +} + +``` + +### JavaScript +```javascript +function bucketSort(arr) { + // Find the minimum and maximum values in the array + let minVal = arr[0]; + let maxVal = arr[0]; + for (let i = 1; i < arr.length; i++) { + if (arr[i] < minVal) { + minVal = arr[i]; + } + if (arr[i] > maxVal) { + maxVal = arr[i]; + } + } + + // Determine the range and the number of buckets + const range = maxVal - minVal; + const numBuckets = arr.length; + const bucketSize = range / numBuckets; + + // Create an array of empty buckets + const buckets = []; + for (let i = 0; i < numBuckets; i++) { + buckets.push([]); + } + + // Place each element in the appropriate bucket + for (let i = 0; i < arr.length; i++) { + let index = Math.floor((arr[i] - minVal) / bucketSize); + + // Ensure that the index is within the valid range of bucket indices + if (index === numBuckets) { + index = numBuckets - 1; + } + buckets[index].push(arr[i]); + } + + // Sort each bucket and concatenate them to get the sorted array + let sortedArr = []; + for (let i = 0; i < numBuckets; i++) { + sortBucket(buckets[i]); + sortedArr = sortedArr.concat(buckets[i]); + } + + return sortedArr; +} + +function sortBucket(bucket) { + // Use any sorting algorithm for the bucket, e.g., insertion sort + for (let i = 1; i < bucket.length; i++) { + const key = bucket[i]; + let j = i - 1; + while (j >= 0 && bucket[j] > key) { + bucket[j + 1] = bucket[j]; + j--; + } + bucket[j + 1] = key; + } +} + +// Example usage: +const array = [3.2, 0.4, 2.8, 4.5, 1.1, 0.9]; +const sortedArray = bucketSort(array); +console.log(sortedArray); + +``` + +### Go +```go +package main + +import ( + "fmt" + "sort" +) + +func bucketSort(arr []float64) []float64 { + if len(arr) == 0 { + return arr + } + + // Find the minimum and maximum values in the array + minVal, maxVal := arr[0], arr[0] + for _, num := range arr { + if num < minVal { + minVal = num + } + if num > maxVal { + maxVal = num + } + } + + // Determine the range and the number of buckets + rangeVal := maxVal - minVal + numBuckets := len(arr) + bucketSize := rangeVal / float64(numBuckets) + + // Create an array of empty buckets + buckets := make([][]float64, numBuckets) + for i := range buckets { + buckets[i] = make([]float64, 0) + } + + // Place each element in the appropriate bucket + for _, num := range arr { + index := int((num - minVal) / bucketSize) + + // Ensure that the index remains within a valid range + if index == numBuckets { + index-- + } + buckets[index] = append(buckets[index], num) + } + + // Sort each bucket and concatenate them to get the sorted array + sortedArr := make([]float64, 0, len(arr)) + for _, bucket := range buckets { + sort.Float64s(bucket) + sortedArr = append(sortedArr, bucket...) + } + + return sortedArr +} + +func main() { + arr := []float64{3.2, 0.4, 2.8, 4.5, 1.1, 0.9} + sortedArr := bucketSort(arr) + fmt.Println(sortedArr) +} + +``` \ No newline at end of file From ec44c05839cb3db317228638845d7d980089f366 Mon Sep 17 00:00:00 2001 From: WalterMarch Date: Fri, 1 Mar 2024 00:09:48 +0000 Subject: [PATCH 2/4] corrected issues --- Algorithms/Sorting/BucketSort/readme.md | 128 +++++++++++++----------- 1 file changed, 68 insertions(+), 60 deletions(-) diff --git a/Algorithms/Sorting/BucketSort/readme.md b/Algorithms/Sorting/BucketSort/readme.md index e0c70914..087ed754 100644 --- a/Algorithms/Sorting/BucketSort/readme.md +++ b/Algorithms/Sorting/BucketSort/readme.md @@ -1,13 +1,15 @@ # Bucket Sort + Bucket 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 sort algorithm. After all the individual buckets are sorted, the elements are concatenated together to form the sorted array. ## Complexity + | Best | Average | Worst | Memory | Stable | |------|---------|-------|--------|--------| | log(n+k) | log(n+k) | log(n^2) | n | Yes | - ## Pseudo Code + ``` BucketSort(arr, n): # Find the minimum and maximum values in the array @@ -38,16 +40,16 @@ BucketSort(arr, n): ``` ## Implementations -* [Python](#python)! -* [C++](#cpp)! -* [C](#c)! -* [Java](#java)! -* [JavaScript](#javascript) -* [Go](#go)! -* [Ruby](#ruby)! +* [Python](#python) +* [C++](#cpp) +* [C](#c) +* [Java](#java) +* [JavaScript](#javascript) +* [Go](#go) ### Python + ```python def bucket_sort(arr): # Find the minimum and maximum values in the input array @@ -81,6 +83,7 @@ print(sorted_arr) ``` ### CPP + ```cpp #include #include @@ -128,7 +131,9 @@ int main() { } ``` + ### C + ```c #include #include @@ -193,7 +198,7 @@ void bucketSort(double arr[], int size) { buckets[i] = NULL; } // Place each element in the appropriate bucket - for (int i = 0; i < size; i++) { + for (int i = 0; i < numBuckets + 1; i++) { int index = (int)((arr[i] - minVal) / bucketSize); insert(&buckets[index], arr[i]); } @@ -222,6 +227,7 @@ int main() { ``` ### Java + ```java import java.util.ArrayList; import java.util.Collections; @@ -271,6 +277,7 @@ public class BucketSort { ``` ### JavaScript + ```javascript function bucketSort(arr) { // Find the minimum and maximum values in the array @@ -338,66 +345,67 @@ console.log(sortedArray); ``` ### Go + ```go package main import ( - "fmt" - "sort" + "fmt" + "sort" ) func bucketSort(arr []float64) []float64 { - if len(arr) == 0 { - return arr - } - - // Find the minimum and maximum values in the array - minVal, maxVal := arr[0], arr[0] - for _, num := range arr { - if num < minVal { - minVal = num - } - if num > maxVal { - maxVal = num - } - } - - // Determine the range and the number of buckets - rangeVal := maxVal - minVal - numBuckets := len(arr) - bucketSize := rangeVal / float64(numBuckets) - - // Create an array of empty buckets - buckets := make([][]float64, numBuckets) - for i := range buckets { - buckets[i] = make([]float64, 0) - } - - // Place each element in the appropriate bucket - for _, num := range arr { - index := int((num - minVal) / bucketSize) - - // Ensure that the index remains within a valid range - if index == numBuckets { - index-- - } - buckets[index] = append(buckets[index], num) - } - - // Sort each bucket and concatenate them to get the sorted array - sortedArr := make([]float64, 0, len(arr)) - for _, bucket := range buckets { - sort.Float64s(bucket) - sortedArr = append(sortedArr, bucket...) - } - - return sortedArr + if len(arr) == 0 { + return arr + } + + // Find the minimum and maximum values in the array + minVal, maxVal := arr[0], arr[0] + for _, num := range arr { + if num < minVal { + minVal = num + } + if num > maxVal { + maxVal = num + } + } + + // Determine the range and the number of buckets + rangeVal := maxVal - minVal + numBuckets := len(arr) + bucketSize := rangeVal / float64(numBuckets) + + // Create an array of empty buckets + buckets := make([][]float64, numBuckets) + for i := range buckets { + buckets[i] = make([]float64, 0) + } + + // Place each element in the appropriate bucket + for _, num := range arr { + index := int((num - minVal) / bucketSize) + + // Ensure that the index remains within a valid range + if index == numBuckets { + index-- + } + buckets[index] = append(buckets[index], num) + } + + // Sort each bucket and concatenate them to get the sorted array + sortedArr := make([]float64, 0, len(arr)) + for _, bucket := range buckets { + sort.Float64s(bucket) + sortedArr = append(sortedArr, bucket...) + } + + return sortedArr } func main() { - arr := []float64{3.2, 0.4, 2.8, 4.5, 1.1, 0.9} - sortedArr := bucketSort(arr) - fmt.Println(sortedArr) + arr := []float64{3.2, 0.4, 2.8, 4.5, 1.1, 0.9} + sortedArr := bucketSort(arr) + fmt.Println(sortedArr) } -``` \ No newline at end of file +``` From 7d363f997ef20f85dbc86ce649fa1d7eaa4f0b1a Mon Sep 17 00:00:00 2001 From: WalterMarch Date: Fri, 1 Mar 2024 00:10:09 +0000 Subject: [PATCH 3/4] added bucket sort --- Algorithms/Sorting/readme.md | 2 ++ Algorithms/readme.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Algorithms/Sorting/readme.md b/Algorithms/Sorting/readme.md index 6e26eb6d..b18fa880 100644 --- a/Algorithms/Sorting/readme.md +++ b/Algorithms/Sorting/readme.md @@ -30,7 +30,9 @@ memory hierarchy (caches and virtual memory) of the host computer, and the software environment. Many of these issues are best dealt with at the algorithmic level, rather than by “tweaking” the code. ### Popular Sorting Algorithms + - [Bubble Sort](BubbleSort/readme.md) +- [Bucket Sort](BucketSort/readme.md) - [Insertion Sort](InsertionSort/readme.md) - [Selection Sort](SelectionSort/readme.md) - [Merge Sort](MergeSort/readme.md) diff --git a/Algorithms/readme.md b/Algorithms/readme.md index 713f206d..9a7fce29 100644 --- a/Algorithms/readme.md +++ b/Algorithms/readme.md @@ -16,7 +16,9 @@ When creating algorithms there are a few techniques that can be used to reduce t ## [Sorting](Sorting/readme.md) Sorting is the process of arranging a list of items in a particular order. For example, if you had a list of names, you might want to sort them alphabetically. Or if you had a list of numbers, you might want to put them in order from smallest to largest. Sorting is a common task, and it’s one that we can do in many different ways. ### Popular Sorting Algorithms + * [Bubble Sort](Sorting/BubbleSort/readme.md) +* [Bucket Sort](Sorting/BucketSort/readme.md) * [Insertion Sort](Sorting/InsertionSort/readme.md) * [Selection Sort](Sorting/SelectionSort/readme.md) * [Merge Sort](Sorting/MergeSort/readme.md) From 479a3746e07b30de36ecf1fb618a6dfbf6247d97 Mon Sep 17 00:00:00 2001 From: WalterMarch Date: Sat, 2 Mar 2024 21:47:32 +0000 Subject: [PATCH 4/4] added fresh temp --- temp/computer_science | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/temp/computer_science b/temp/computer_science index c8aaab7f..c845bbc2 160000 --- a/temp/computer_science +++ b/temp/computer_science @@ -1 +1 @@ -Subproject commit c8aaab7fd7f56b25813980cd4673005eaedfa778 +Subproject commit c845bbc2b67ef54aa76f882784af6cc1fb50db60