Merge pull request #102 from RichardWeiYang/master

counting sort in c
This commit is contained in:
wangzheng0822 2018-10-26 10:37:19 +08:00 committed by GitHub
commit d8d7806a01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 219 additions and 0 deletions

View File

@ -0,0 +1,84 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
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;
}

View File

@ -0,0 +1,69 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
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;
}

View File

@ -0,0 +1,66 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
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;
}