From 275752f9f5bf28c80d20750972d3c51a92e285cd Mon Sep 17 00:00:00 2001 From: Wei Yang Date: Mon, 22 Oct 2018 13:58:50 +0800 Subject: [PATCH 1/2] counting sort in c --- c-cpp/14_sorts/counting_sort.c | 66 ++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 c-cpp/14_sorts/counting_sort.c diff --git a/c-cpp/14_sorts/counting_sort.c b/c-cpp/14_sorts/counting_sort.c new file mode 100644 index 0000000..55e9608 --- /dev/null +++ b/c-cpp/14_sorts/counting_sort.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +void dump(int *arr, int size) +{ + int i; + + for (i = 0; i < size; i++) + printf("%08d\n", arr[i]); +} + +// content in arr must be positive integer +void counting_sort(int *arr, int size) +{ + int max, i; + int *count, *tmp; + + if (size <= 1) + return; + + max = 0; + // find the biggest integer + for (i = 0; i < size; i++) { + if (max < arr[i]) + max = arr[i]; + } + + // init count to 0 + count = (int*)malloc((max+1) * sizeof(int)); + tmp = (int*)malloc(size * sizeof(int)); + if (!count || !tmp) + return; + memset(count, 0, (max + 1) * sizeof(int)); + + // counting + for (i = 0; i < size; i++) + count[arr[i]]++; + for (i = 1; i < max + 1; i++) + count[i] = count[i-1] + count[i]; + + // iterate arr and put it to the correct index in tmp + for (i = 0; i < size; i++){ + int index = count[arr[i]] - 1; + tmp[index] = arr[i]; + count[arr[i]]--; + } + + // move back to arr + memcpy(arr, tmp, size * sizeof(int)); +} + +void counting_sort_test() +{ + int test_data[10] = {3, 23, 98, 1, 27, 36, 52, 89, 76, 44}; + + counting_sort(test_data, 10); + dump(test_data, 10); +} + +int main() +{ + counting_sort_test(); + return 0; +} From 52a13834fec63148aa86468bd9b666acbffcbd8d Mon Sep 17 00:00:00 2001 From: Wei Yang Date: Thu, 25 Oct 2018 08:40:40 +0800 Subject: [PATCH 2/2] quick/merge sort in c --- c-cpp/12_sorts/merge_sort.c | 84 +++++++++++++++++++++++++++++++++++++ c-cpp/12_sorts/quick_sort.c | 69 ++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 c-cpp/12_sorts/merge_sort.c create mode 100644 c-cpp/12_sorts/quick_sort.c diff --git a/c-cpp/12_sorts/merge_sort.c b/c-cpp/12_sorts/merge_sort.c new file mode 100644 index 0000000..9e62666 --- /dev/null +++ b/c-cpp/12_sorts/merge_sort.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include + +void dump(int *arr, int size) +{ + int idx; + + for (idx = 0; idx < size; idx++) + printf("%08d\n", arr[idx]); +} + +void __merge(int *arr, int p, int q, int r) +{ + int *tmp; + int i, j, k; + + tmp = (int*)malloc((r - p + 1) * sizeof(int)); + + if (!tmp) + abort(); + + for (i = p, j = q + 1, k = 0; i <= q && j <= r;) { + if (arr[i] <= arr[j]) + tmp[k++] = arr[i++]; + else + tmp[k++] = arr[j++]; + } + + if (i == q + 1) { + for (; j <= r;) + tmp[k++] = arr[j++]; + } else { + for (; i <= q;) + tmp[k++] = arr[i++]; + } + + memcpy(arr + p, tmp, (r - p + 1) * sizeof(int)); + free(tmp); +} + +void __merge_sort(int *arr, int p, int r) +{ + int q; + + if (p >= r) + return; + + q = (p + r) / 2; + __merge_sort(arr, p, q); + __merge_sort(arr, q + 1, r); + __merge(arr, p, q, r); +} + +void merge_sort(int *arr, int size) +{ + __merge_sort(arr, 0, size - 1); +} + +void merge_verify() +{ + int test[10] = {5, 8, 9, 23, 67, 1, 3, 7, 31, 56}; + + __merge(test, 0, 4, 9); + + dump(test, 10); +} + +void merge_sort_test() +{ + int test[10] = {5, 8, 9, 23, 67, 1, 3, 7, 31, 56}; + + merge_sort(test, 10); + + dump(test, 10); +} + +int main() +{ + //merge_verify(); + merge_sort_test(); + return 0; +} diff --git a/c-cpp/12_sorts/quick_sort.c b/c-cpp/12_sorts/quick_sort.c new file mode 100644 index 0000000..7bdc318 --- /dev/null +++ b/c-cpp/12_sorts/quick_sort.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include + +void dump(int *arr, int size) +{ + int idx; + + for (idx = 0; idx < size; idx++) + printf("%08d\n", arr[idx]); +} + +void swap(int *a, int *b) +{ + int tmp = *a; + *a = *b; + *b = tmp; +} + +int partition(int *arr, int p, int r) +{ + //int pivot = arr[r]; + int i, j; + + i = j = p; + + for (; j < r; j++) { + if (arr[j] < arr[r]) { + swap(arr + i, arr + j); + i++; + } + } + + swap(arr + i, arr + r); + return i; +} + +void __quick_sort(int *arr, int p, int r) +{ + int q; + + if (p >= r) + return; + + q = partition(arr, p, r); + __quick_sort(arr, p, q-1); + __quick_sort(arr, q+1, r); +} + +void quick_sort(int *arr, int size) +{ + __quick_sort(arr, 0, size - 1); +} + +void quick_sort_test() +{ + int test[10] = {5, 8, 9, 23, 67, 1, 3, 7, 31, 56}; + + quick_sort(test, 10); + + dump(test, 10); +} + +int main() +{ + quick_sort_test(); + return 0; +}