-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTri_Fusion.c
More file actions
90 lines (81 loc) · 2.19 KB
/
Tri_Fusion.c
File metadata and controls
90 lines (81 loc) · 2.19 KB
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
#include <stdio.h>
#include <stdlib.h>
/**
* Merges two subarrays of array[].
* First subarray is array[low..mid]
* Second subarray is array[mid+1..high]
*
* int array[] :: array to sort
* int low :: lowest index of array : usually 0
* int mid :: middle / mediane index of array
* int high :: highest index of array : usually size of array
**/
void merge(int array[], int low, int mid, int high)
{
int i, j, k;
int n1 = mid - low + 1;
int n2 = high - mid;
// create temp arrays
int *left = malloc(n1 * sizeof(int));
int *right = malloc(n2 * sizeof(int));
// Copy data to temp arrays left[] and right[]
for (i = 0; i < n1; i++)
left[i] = array[low + i];
for (j = 0; j < n2; j++)
right[j] = array[mid + 1+ j];
// Merge the temp arrays back into array[low..high]
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = low; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (left[i] <= right[j])
{
array[k] = left[i];
i++;
}
else
{
array[k] = right[j];
j++;
}
k++;
}
// Copy the remaining elements of left[], if there are any
while (i < n1)
{
array[k] = left[i];
i++;
k++;
}
// Copy the remaining elements of right[], if there are any
while (j < n2)
{
array[k] = right[j];
j++;
k++;
}
free(left);
free(right);
}
/**
* low is for left index and high is right index of the
* sub-array of array to be sorted
*
* int array[] :: array to sort
* int low :: lowest index of array : usually 0
* int high :: highest index of array : usually size of array
**/
void mergeSort(int array[], int low, int high)
{
if (low < high)
{
// Same as (low+high)/2, but avoids overflow for
// large low and high
int mid = low+(high-low)/2;
// Sort first and second halves
mergeSort(array, low, mid);
mergeSort(array, mid+1, high);
merge(array, low, mid, high);
}
}