-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge.h
101 lines (89 loc) · 1.92 KB
/
merge.h
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1000000
#ifndef MERGE_H
#define MERGE_H
int result[MAX];
void mergeInt(int number[], int head, int mid, int tail)
{
int i, j, k;
k = 0;
i = head;
j = mid + 1;
while(i <= mid && j <= tail)
{
if(number[i] < number[j])
{
result[k++] = number[i++];
}
else
{
result[k++] = number[j++];
}
}
while(i <= mid)
{
result[k++] = number[i++];
}
while(j <= tail)
{
result[k++] = number[j++];
}
for(i=tail; i >= head; i--)
{
number[i] = result[--k]; // copying back the sorted list to a[]
}
}
char resultstring[MAX][101];
void mergeString(char **str, int head, int mid, int tail)
{
int i, j, k;
k = 0;
i = head;
j = mid + 1;
while(i <= mid && j <= tail)
{
if(strcmp(str[i],str[j])<0)
{
strcpy(resultstring[k++],str[i++]);
}
else
{
strcpy(resultstring[k++],str[j++]);
}
}
while(i <= mid)
{
strcpy(resultstring[k++],str[i++]);
}
while(j <= tail)
{
strcpy(resultstring[k++],str[j++]);
}
for(i=tail; i >= mid; i--)
{
strcpy(str[i],resultstring[--k]);
}
}
void mergeSortInt(int *number, int head, int tail)
{
if(head < tail)
{
int mid = (head + tail) / 2;
mergeSortInt(number, head, mid);
mergeSortInt(number, mid+1, tail);
mergeInt(number, head, mid, tail);
}
}
void mergeSortString(char **str, int head, int tail)
{
if(head < tail)
{
int mid = (head + tail) / 2;
mergeSortString(str, head, mid);
mergeSortString(str, mid+1, tail);
mergeString(str, head, mid, tail);
}
}
#endif