-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.c
109 lines (78 loc) · 1.77 KB
/
mergesort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include "sortworks.h"
void** merge_sort_merge(void** arr, void** buf, int a, int b, int c, compfunc comp)
{
int bind = 0;
int r = 0;
int ia = a;
int ib = b;
while (ia < b && ib < c)
{
r = comp(arr[ia],arr[ib]);
if (r < 0)
buf[bind++] = arr[ia++];
else if (r > 0)
buf[bind++] = arr[ib++];
else
{
buf[bind++] = arr[ia++];
buf[bind++] = arr[ib++];
}
}
while (ia < b)
buf[bind++] = arr[ia++];
while (ib < c)
buf[bind++] = arr[ib++];
bind = 0;
for (r = a; r < c; ++r)
arr[r] = buf[bind++];
return arr;
}
void** merge_sort_rec(void** arr, void** buf, int a, int b, compfunc comp)
{
//printf("merge_sort_rec: %x, %x, %i, %i, %x\n",arr,buf,a,b,comp);
int m,span;
void** left;
void** right;
span = b-a;
if (span < 2)
return arr;
m = a + span/2;
left = merge_sort_rec(arr,buf,a,m,comp);
right = merge_sort_rec(arr,buf,m,b,comp);
return merge_sort_merge(arr,buf,a,m,b,comp);
}
void** merge_sort(void** arr, int len, compfunc comp)
{
/* arr is presumed an array of void pointers */
void** buf = 0;
void** result;
buf = malloc(sizeof(void*)*len);
result = merge_sort_rec(arr,buf,0,len,comp);
free(buf);
return result;
}
//#define mergesort_active
#ifdef mergesort_active
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv)
{
int i;
int len = 8;
int arr[] = {5,3,6,4,7,2,8,1};
int** brr = malloc(sizeof(int*)*len);
for (i=0; i<len; ++i)
brr[i] = &arr[i];
printf("unsorted\n");
for (i=0; i<len; ++i)
printf("brr[%i]: %i\n",i,*brr[i]);
merge_sort(brr,len,comp_int);
printf("sorted\n");
for (i=0; i<len; ++i)
printf("brr[%i]: %i\n",i,*brr[i]);
free(brr);
return 0;
}
#endif