Skip to content

Commit

Permalink
插入排序
Browse files Browse the repository at this point in the history
  • Loading branch information
wenjun1055 committed Dec 6, 2013
1 parent 34c2878 commit 03a61fb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
15 changes: 15 additions & 0 deletions heap.c
Original file line number Diff line number Diff line change
@@ -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)
43 changes: 43 additions & 0 deletions insertion_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}

0 comments on commit 03a61fb

Please sign in to comment.