-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.c
More file actions
74 lines (67 loc) · 1.9 KB
/
sorting.c
File metadata and controls
74 lines (67 loc) · 1.9 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
// #include <stdlib.h>
// #include <limits.h>
// #include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "sorting.h"
static void merge(int *arr, int leftIndex, int middleIndex, int rightIndex, int recurseLevel) {
int i, j, k;
int n1 = middleIndex - leftIndex + 1;
int n2 = rightIndex - middleIndex;
int tempLeftArray[n1];
int tempRightArray[n2];
printf("left Index: %d middleIndex: %d rightIndex: %d recurseLevel: %d \n", leftIndex, middleIndex, rightIndex, recurseLevel);
/* Copy data to temp arrays */
for (i = 0; i < n1; i++) {
tempLeftArray[i] = *(arr + leftIndex + i);
}
for (j = 0; j < n2; j++) {
tempRightArray[j] = *(arr + middleIndex + j + 1);
}
/* Maintain current index of sub-arrays */
i=0;
j=0;
k=leftIndex;
/* Until we reach either end of either temporary array, pick larger among
elements in the temp arrays and place them in the correct position in arr */
while (i < n1 && j < n2) {
if (tempLeftArray[i] <= tempRightArray[j]) {
*(arr + k) = tempLeftArray[i];
i++;
}
else {
*(arr + k) = tempRightArray[j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1) {
*(arr + k) = tempLeftArray[i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2) {
*(arr + k) = tempRightArray[j];
j++;
k++;
}
}
void mergeSort(int *arr, int leftIndex, int rightIndex, int recurseLevel) {
int i;
if (leftIndex < rightIndex) {
/* Same as (leftIndex + r)/2, but avoids overflow for large leftIndex and rightIndex */
int middleIndex = leftIndex + (rightIndex - leftIndex) / 2;
printf("Array: ");
for(i = leftIndex; i < rightIndex + 1; i++) {
printf("%d ", *(arr + i));
}
printf("\n");
mergeSort(arr, leftIndex, middleIndex, recurseLevel + 1);
mergeSort(arr, middleIndex + 1, rightIndex, recurseLevel + 1);
merge(arr, leftIndex, middleIndex, rightIndex, recurseLevel);
}
}