-
Notifications
You must be signed in to change notification settings - Fork 0
/
1051 Height Checker.c
65 lines (55 loc) · 1.32 KB
/
1051 Height Checker.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
void Merge(int arr[], int low, int mid, int high)
{
int * aux_arr = (int *)malloc(sizeof(int) * (high-low+1));
int aux_arr_index = 0;
int i=low;
int j=mid+1;
for(i=low, j=mid+1; i<=mid && j<=high; )
{
if(arr[i] <= arr[j])
{
aux_arr[aux_arr_index++] = arr[i];
i++;
}
else
{
aux_arr[aux_arr_index++] = arr[j];
j++;
}
}
while(i<=mid)
{
aux_arr[aux_arr_index++] = arr[i++];
}
while(j<=high)
{
aux_arr[aux_arr_index++] = arr[j++];
}
memcpy(&arr[low], aux_arr, sizeof(int) * (high-low+1));
free(aux_arr);
}
void merge_sort(int arr[], int low, int high)
{
if(low < high)
{
int mid = (low+high)/2;
merge_sort(arr, low, mid);
merge_sort(arr, mid+1, high);
Merge(arr, low, mid, high);
}
}
int heightChecker(int* heights, int heightsSize)
{
//Applying merge sort to sort the height
int * sort_arr = (int *)malloc(sizeof(int) * heightsSize);
memcpy(sort_arr, heights, sizeof(int)*heightsSize);
merge_sort(sort_arr, 0, heightsSize-1);
int i=0;
int count = 0;
for(i=0; i<heightsSize; i++)
{
if(heights[i] != sort_arr[i])
count++;
}
return count;
}