Skip to content

Commit 03a61fb

Browse files
committedDec 6, 2013
插入排序
1 parent 34c2878 commit 03a61fb

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
 

‎heap.c

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct Heap_
2+
{
3+
int size;
4+
int (*compare)(const void *key1, const void *key2);
5+
void (*destory)(void *data);
6+
void **tree;
7+
};
8+
typedef struct Heap_ Heap;
9+
10+
void heap_init(Heap *heap, int (*compare)(const void *key1, const void *key2), void (*destory)(void *data));
11+
void heap_destory(Heap *heap);
12+
int heap_insert(Heap *heap, const void *data);
13+
int heap_extract(Heap *heap, void **data);
14+
15+
#define HEAP_SIZE(heap) ((heap)->size)

‎insertion_sort.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
int issort(void *data, int size, int esize, int (*compare)(const void *key1, const void *key2))
6+
{
7+
char *a = data;
8+
void *key;
9+
int i,j;
10+
11+
if (NULL == (key = (char *)malloc(esize))) {
12+
return -1;
13+
}
14+
15+
for (j = 1; j < size; j++) {
16+
memcpy(key, &a[j * esize], esize);
17+
i = j - 1;
18+
19+
while (i >=0 && compare(&a[i * esize], key) > 0) {
20+
memcpy(&a[(i + 1) * esize], &a[i * esize], esize);
21+
i--;
22+
}
23+
24+
memcpy(&a[(i + 1) * esize], key, esize);
25+
}
26+
27+
free(key);
28+
29+
return 0;
30+
}
31+
32+
int main()
33+
{
34+
char *str = "zyxkjhaop";
35+
36+
printf("original:str = %s", str);
37+
38+
issort(str, 9, sizeof(char), strcmp);
39+
40+
printf("changed:str = %s", str);
41+
42+
return 0;
43+
}

0 commit comments

Comments
 (0)
Please sign in to comment.