-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub_sort.c
113 lines (84 loc) · 1.79 KB
/
sub_sort.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
109
110
111
112
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#include "lib/helpers.h"
int FindEndOfLeft(int *nums, int num_size)
{
int i = 0;
for (i = 1; i < num_size; ++i)
{
if (nums[i - 1] > nums[i])
break;
}
return i - 1;
}
int FindStartOfRight(int *nums, int num_size)
{
int i = 0;
for (i = num_size - 2; i >= 0; --i)
{
if (nums[i] > nums[i + 1])
break;
}
return i + 1;
}
int BinarySearch(int *nums, int start, int end, int target)
{
int last_index = -1;
while (start <= end)
{
int mid = ((start + end) >> 1);
if (nums[mid] == target)
return mid;
last_index = mid;
if (nums[mid] > target)
end = mid - 1;
else
start = mid + 1;
}
return last_index;
}
struct Range
{
int start;
int end; // included
};
void RangeDump(struct Range *range)
{
printf("start %d end %d\n", range->start, range->end);
}
void RangeInit(struct Range *range, int start, int end)
{
range->start = start;
range->end = end;
}
struct Range* FindRange(int *nums, int num_size)
{
int end_of_left = -1, start_of_right = -1;
int min = INT_MAX, max = INT_MAX;
int i = 0;
struct Range *range = calloc(1, sizeof(struct Range));
if ((end_of_left = FindEndOfLeft(nums, num_size)) == num_size - 1)
return NULL;
if ((start_of_right = FindStartOfRight(nums, num_size)) == 0)
return NULL;
min = nums[start_of_right];
max = nums[end_of_left];
for (i = end_of_left; i <= start_of_right; ++i)
{
min = Min(min, nums[i]);
max = Max(max, nums[i]);
}
RangeInit(range
, BinarySearch(nums, 0, end_of_left, min)
, BinarySearch(nums, start_of_right, num_size - 1, max));
RangeDump(range);
return range;
}
int main(int argc, char *argv[])
{
int nums[] = {1, 2, 4, 7, 10, 11, 7, 12, 6, 7, 16, 18, 19};
FindRange(nums, sizeof(nums)/sizeof(nums[0]));
return 0;
}