mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	feat(bucket_sort): add bucket_sort code in go/c (#443)
* feat(bucket_sort): add bucket_sort code in go/c * feat(go): add bucket_sort * feat(c): add bucket_sort in c * Update bucket_sort_test.go --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
		@ -1,4 +1,5 @@
 | 
				
			|||||||
add_executable(bubble_sort bubble_sort.c)
 | 
					add_executable(bubble_sort bubble_sort.c)
 | 
				
			||||||
 | 
					add_executable(bucket_sort bucket_sort.c)
 | 
				
			||||||
add_executable(insertion_sort insertion_sort.c)
 | 
					add_executable(insertion_sort insertion_sort.c)
 | 
				
			||||||
add_executable(quick_sort quick_sort.c)
 | 
					add_executable(quick_sort quick_sort.c)
 | 
				
			||||||
add_executable(counting_sort counting_sort.c)
 | 
					add_executable(counting_sort counting_sort.c)
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										38
									
								
								codes/c/chapter_sorting/bucket_sort.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								codes/c/chapter_sorting/bucket_sort.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * File: bucket_sort.c
 | 
				
			||||||
 | 
					 * Created Time: 2023-03-27
 | 
				
			||||||
 | 
					 * Author: Reanon (793584285@qq.com)
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "../include/include.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 冒泡排序 */
 | 
				
			||||||
 | 
					void bucketSort(double nums[], int size) {
 | 
				
			||||||
 | 
					    // 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
 | 
				
			||||||
 | 
					    int k = size / 2;
 | 
				
			||||||
 | 
						// 1. 将数组元素分配到各个桶中
 | 
				
			||||||
 | 
					    // 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
 | 
				
			||||||
 | 
					    // 将 num 添加进桶 i
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 2. 对各个桶执行排序
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 使用内置切片排序函数,也可以替换成其它排序算法
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 3. 遍历桶合并结果
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* Driver Code */
 | 
				
			||||||
 | 
					int main() {
 | 
				
			||||||
 | 
					    // 设输入数据为浮点数,范围为 [0, 1)
 | 
				
			||||||
 | 
					    double nums[] = {0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37};
 | 
				
			||||||
 | 
					    int size = sizeof(nums) / sizeof(double);
 | 
				
			||||||
 | 
					    bucketSort(nums, size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    printf("桶排序完成后 nums = ");
 | 
				
			||||||
 | 
					    printf("[");
 | 
				
			||||||
 | 
					    for (int i = 0; i < size - 1; i++) {
 | 
				
			||||||
 | 
					        printf("%g, ", nums[i]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    printf("]");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -28,20 +28,20 @@ extern "C" {
 | 
				
			|||||||
static void printArray(int arr[], int size) {
 | 
					static void printArray(int arr[], int size) {
 | 
				
			||||||
    printf("[");
 | 
					    printf("[");
 | 
				
			||||||
    if (arr != NULL && size != 0) {
 | 
					    if (arr != NULL && size != 0) {
 | 
				
			||||||
      for (int i = 0; i < size - 1; i++) {
 | 
					        for (int i = 0; i < size - 1; i++) {
 | 
				
			||||||
          if (arr[i] != NIL) {
 | 
					            if (arr[i] != NIL) {
 | 
				
			||||||
              printf("%d, ", arr[i]);
 | 
					                printf("%d, ", arr[i]);
 | 
				
			||||||
          } else {
 | 
					            } else {
 | 
				
			||||||
              printf("NULL, ");
 | 
					                printf("NULL, ");
 | 
				
			||||||
          }
 | 
					            }
 | 
				
			||||||
      }
 | 
					        }
 | 
				
			||||||
      if (arr[size - 1] != NIL) {
 | 
					        if (arr[size - 1] != NIL) {
 | 
				
			||||||
          printf("%d]\n", arr[size - 1]);
 | 
					            printf("%d]\n", arr[size - 1]);
 | 
				
			||||||
      } else {
 | 
					        } else {
 | 
				
			||||||
          printf("NULL]\n");
 | 
					            printf("NULL]\n");
 | 
				
			||||||
      }
 | 
					        }
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      printf("]");
 | 
					        printf("]");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -138,7 +138,7 @@ static void printTree(TreeNode *root) {
 | 
				
			|||||||
 * @param size
 | 
					 * @param size
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
static void printHeap(int arr[], int size) {
 | 
					static void printHeap(int arr[], int size) {
 | 
				
			||||||
    TreeNode * root;
 | 
					    TreeNode *root;
 | 
				
			||||||
    printf("堆的数组表示:");
 | 
					    printf("堆的数组表示:");
 | 
				
			||||||
    printArray(arr, size);
 | 
					    printArray(arr, size);
 | 
				
			||||||
    printf("堆的树状表示:\n");
 | 
					    printf("堆的树状表示:\n");
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										37
									
								
								codes/go/chapter_sorting/bucket_sort.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								codes/go/chapter_sorting/bucket_sort.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,37 @@
 | 
				
			|||||||
 | 
					// File: bucket_sort.go
 | 
				
			||||||
 | 
					// Created Time: 2023-03-27
 | 
				
			||||||
 | 
					// Author: Reanon (793584285@qq.com)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package chapter_sorting
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "sort"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* 桶排序 */
 | 
				
			||||||
 | 
					func bucketSort(nums []float64) {
 | 
				
			||||||
 | 
						// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
 | 
				
			||||||
 | 
						k := len(nums) / 2
 | 
				
			||||||
 | 
						buckets := make([][]float64, k)
 | 
				
			||||||
 | 
						for i := 0; i < k; i++ {
 | 
				
			||||||
 | 
							buckets[i] = make([]float64, 0)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// 1. 将数组元素分配到各个桶中
 | 
				
			||||||
 | 
						for _, num := range nums {
 | 
				
			||||||
 | 
							// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
 | 
				
			||||||
 | 
							i := int(num) * k
 | 
				
			||||||
 | 
							// 将 num 添加进桶 i
 | 
				
			||||||
 | 
							buckets[i] = append(buckets[i], num)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// 2. 对各个桶执行排序
 | 
				
			||||||
 | 
						for i := 0; i < k; i++ {
 | 
				
			||||||
 | 
							// 使用内置切片排序函数,也可以替换成其它排序算法
 | 
				
			||||||
 | 
							sort.Float64s(buckets[i])
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// 3. 遍历桶合并结果
 | 
				
			||||||
 | 
						i := 0
 | 
				
			||||||
 | 
						for _, bucket := range buckets {
 | 
				
			||||||
 | 
							for _, num := range bucket {
 | 
				
			||||||
 | 
								nums[i] = num
 | 
				
			||||||
 | 
								i++
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										17
									
								
								codes/go/chapter_sorting/bucket_sort_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								codes/go/chapter_sorting/bucket_sort_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					// File: bucket_sort_test.go
 | 
				
			||||||
 | 
					// Created Time: 2023-03-27
 | 
				
			||||||
 | 
					// Author: Reanon (793584285@qq.com)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package chapter_sorting
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestBucketSort(t *testing.T) {
 | 
				
			||||||
 | 
						// 设输入数据为浮点数,范围为 [0, 1)
 | 
				
			||||||
 | 
						nums := []float64{0.49, 0.96, 0.82, 0.09, 0.57, 0.43, 0.91, 0.75, 0.15, 0.37}
 | 
				
			||||||
 | 
						bucketSort(nums)
 | 
				
			||||||
 | 
						fmt.Println("桶排序完成后 nums = ", nums)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user