diff --git a/heap.c b/heap.c new file mode 100644 index 0000000..7dfb0d7 --- /dev/null +++ b/heap.c @@ -0,0 +1,15 @@ +struct Heap_ +{ + int size; + int (*compare)(const void *key1, const void *key2); + void (*destory)(void *data); + void **tree; +}; +typedef struct Heap_ Heap; + +void heap_init(Heap *heap, int (*compare)(const void *key1, const void *key2), void (*destory)(void *data)); +void heap_destory(Heap *heap); +int heap_insert(Heap *heap, const void *data); +int heap_extract(Heap *heap, void **data); + +#define HEAP_SIZE(heap) ((heap)->size) diff --git a/insertion_sort.c b/insertion_sort.c new file mode 100644 index 0000000..963a1de --- /dev/null +++ b/insertion_sort.c @@ -0,0 +1,43 @@ +#include +#include +#include + +int issort(void *data, int size, int esize, int (*compare)(const void *key1, const void *key2)) +{ + char *a = data; + void *key; + int i,j; + + if (NULL == (key = (char *)malloc(esize))) { + return -1; + } + + for (j = 1; j < size; j++) { + memcpy(key, &a[j * esize], esize); + i = j - 1; + + while (i >=0 && compare(&a[i * esize], key) > 0) { + memcpy(&a[(i + 1) * esize], &a[i * esize], esize); + i--; + } + + memcpy(&a[(i + 1) * esize], key, esize); + } + + free(key); + + return 0; +} + +int main() +{ + char *str = "zyxkjhaop"; + + printf("original:str = %s", str); + + issort(str, 9, sizeof(char), strcmp); + + printf("changed:str = %s", str); + + return 0; +}