diff --git a/c-cpp/11_sorts/sorts.c b/c-cpp/11_sorts/sorts.c new file mode 100644 index 0000000..f759077 --- /dev/null +++ b/c-cpp/11_sorts/sorts.c @@ -0,0 +1,139 @@ +#include +#include +#include + +struct array { + int size; + int used; + int *arr; +}; + +void dump(struct array *array) +{ + int idx; + + for (idx = 0; idx < array->used; idx++) + printf("[%02d]: %08d\n", idx, array->arr[idx]); +} + +void alloc(struct array *array) +{ + array->arr = (int *)malloc(array->size * sizeof(int)); +} + +void bubble_sort(struct array *array) +{ + int i, j; + + if (array->used <= 1) + return; + + for (i = 0; i < array->used; i++) { + bool has_swap = false; + for (j = 0; j < array->used - i - 1; j++) { + if (array->arr[j] > array->arr[j+1]) { + int tmp; + tmp = array->arr[j]; + array->arr[j] = array->arr[j+1]; + array->arr[j+1] = tmp; + has_swap = true; + } + + } + if (!has_swap) + break; + } +} + +void bubble_sort_test() +{ + int idx; + struct array ten_int = {10, 0, NULL}; + + alloc(&ten_int); + for (idx = 0; idx < 10; idx++) + ten_int.arr[idx] = 30 - idx; + ten_int.used = 10; + dump(&ten_int); + bubble_sort(&ten_int); + dump(&ten_int); +} + +void insertion_sort(struct array *array) +{ + int i, j; + + if (array->used <= 1) + return; + + for (i = 1; i < array->used; i++) { + int val = array->arr[i]; + + for (j = i - 1; j >= 0; j--) { + if (val < array->arr[j]) + array->arr[j+1] = array->arr[j]; + else + break; + } + array->arr[j+1] = val; + } +} + +void insertion_sort_test() +{ + int idx; + struct array ten_int = {10, 0, NULL}; + + alloc(&ten_int); + for (idx = 0; idx < 10; idx++) + ten_int.arr[idx] = 30 - idx; + ten_int.used = 10; + dump(&ten_int); + insertion_sort(&ten_int); + dump(&ten_int); +} + +void selection_sort(struct array *array) +{ + int i, j; + + if (array->used <= 1) + return; + + for (i = 0; i < array->used - 1; i++) { + int tmp, idx = i; + + for (j = i + 1; j < array->used; j++) + if (array->arr[j] < array->arr[idx]) + idx = j; + + if (idx == i) + continue; + + tmp = array->arr[i]; + array->arr[i] = array->arr[idx]; + array->arr[idx] = tmp; + } +} + +void selection_sort_test() +{ + int idx; + struct array ten_int = {10, 0, NULL}; + + alloc(&ten_int); + for (idx = 0; idx < 10; idx++) + ten_int.arr[idx] = 30 - idx; + ten_int.used = 10; + dump(&ten_int); + selection_sort(&ten_int); + dump(&ten_int); +} + +int main() +{ + //bubble_sort_test(); + //selection_sort_test(); + insertion_sort_test(); + return 0; +} diff --git a/go/11_sorts/Sort.go b/go/11_sorts/Sort.go index 379c41b..005b4f0 100644 --- a/go/11_sorts/Sort.go +++ b/go/11_sorts/Sort.go @@ -1,54 +1,66 @@ package _1_sorts -func BubbleSort(a []int) { - arrLen := len(a) - if arrLen <= 1 { +/* +冒泡排序、插入排序、选择排序 + */ + +//冒泡排序,a是数组,n表示数组大小 +func BubbleSort(a []int, n int) { + if n <= 1 { return } - for i := arrLen - 1; i > 0; i-- { - for j := 0; j < i; j++ { + for i := 0; i < n; i++ { + // 提前退出标志 + flag := false + for j := 0; j < n-i-1; j++ { if a[j] > a[j+1] { - tmp := a[j+1] - a[j+1] = a[j] - a[j] = tmp + a[j], a[j+1] = a[j+1], a[j] + //此次冒泡有数据交换 + flag = true } } + // 如果没有交换数据,提前退出 + if !flag { + break + } } } -func InsertSort(a []int) { - arrLen := len(a) - if arrLen <= 1 { +// 插入排序,a表示数组,n表示数组大小 +func InsertionSort(a []int, n int) { + if n <= 1 { return } - for i := 1; i < arrLen; i++ { - v := a[i] + for i := 1; i < n; i++ { + value := a[i] j := i - 1 + //查找要插入的位置并移动数据 for ; j >= 0; j-- { - if a[j] > v { + if a[j] > value { a[j+1] = a[j] + } else { + break } } - a[j+1] = v + a[j+1] = value } } -func SelectionSort(a []int) { - arrLen := len(a) - if arrLen <= 1 { +// 选择排序,a表示数组,n表示数组大小 +func SelectionSort(a []int, n int) { + if n <= 1 { return } - for i := 0; i < arrLen; i++ { + for i := 0; i < n; i++ { + // 查找最小值 minIndex := i - for j := i + 1; j < arrLen; j++ { + for j := i + 1; j < n; j++ { if a[j] < a[minIndex] { minIndex = j } } - if minIndex != i { - tmp := a[minIndex] - a[minIndex] = a[i] - a[i] = tmp - } + // 交换 + a[i], a[minIndex] = a[minIndex],a[i] + } } diff --git a/java/11_sorts/Sorts.java b/java/11_sorts/Sorts.java index 2258106..aeda6f4 100644 --- a/java/11_sorts/Sorts.java +++ b/java/11_sorts/Sorts.java @@ -58,6 +58,9 @@ public class Sorts { } } + if (minIndex == i) + continue; + // 交换 int tmp = a[i]; a[i] = a[minIndex]; diff --git a/java/14_sorts/CountingSort.java b/java/14_sorts/CountingSort.java new file mode 100644 index 0000000..3cc5d58 --- /dev/null +++ b/java/14_sorts/CountingSort.java @@ -0,0 +1,53 @@ +package sorts; + +/** + * 计数排序 + * + * Author: ZHENG + */ +public class CountingSort { + + // 计数排序,a是数组,n是数组大小。假设数组中存储的都是非负整数。 + public static void countingSort(int[] a, int n) { + if (n <= 1) return; + + // 查找数组中数据的范围 + int max = a[0]; + for (int i = 1; i < n; ++i) { + if (max < a[i]) { + max = a[i]; + } + } + + // 申请一个计数数组c,下标大小[0,max] + int[] c = new int[max + 1]; + for (int i = 0; i < max + 1; ++i) { + c[i] = 0; + } + + // 计算每个元素的个数,放入c中 + for (int i = 0; i < n; ++i) { + c[a[i]]++; + } + + // 依次累加 + for (int i = 1; i < max + 1; ++i) { + c[i] = c[i-1] + c[i]; + } + + // 临时数组r,存储排序之后的结果 + int[] r = new int[n]; + // 计算排序的关键步骤了,有点难理解 + for (int i = n - 1; i >= 0; --i) { + int index = c[a[i]]-1; + r[index] = a[i]; + c[a[i]]--; + } + + // 将结果拷贝会a数组 + for (int i = 0; i < n; ++i) { + a[i] = r[i]; + } + } + +}