-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindSum.c
93 lines (77 loc) · 1.74 KB
/
FindSum.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
#include <stdio.h>
void swap(int a[], int idxA, int idxB)
{
int temp = a[idxA];
a[idxA] = a[idxB];
a[idxB] = temp;
}
int partitionOnPivot(int a[], int startIdx, int endIdx)
{
int pivotIdx = startIdx;
int pivotValue = a[endIdx];
for (int i = startIdx; i < endIdx; i++)
{
if (a[i] <= pivotValue)
{
swap(a, i, pivotIdx);
pivotIdx++;
}
}
// Move the pivot into proper position.
swap(a, pivotIdx, endIdx);
return pivotIdx;
}
void quickSort(int a[], int startIdx, int endIdx)
{
if (startIdx >= endIdx)
{
// All done!
return;
}
int pivotIdx = partitionOnPivot(a, startIdx, endIdx);
// Sort the values smaller than the pivot.
quickSort(a, startIdx, pivotIdx - 1);
// Sort the values larger than the pivot.
quickSort(a, pivotIdx + 1, endIdx);
}
void findPair(int a[], int n, int sum)
{
int i = 0;
int j = n-1;
while (i < j)
{
int pairSum = a[i] + a[j];
if (pairSum == sum)
{
// Found it!
printf("Found pair at idx a[%d]=%d and a[%d]=%d\n",i,a[i],j,a[j]);
return;
}
else if (pairSum > sum)
{
j--;
}
else
{
i++;
}
}
printf("No pair exists");
}
int main()
{
int a[] = {8, 7, 2, 5, 3, 1};
int sum = 10;
int n = sizeof(a) / sizeof(int);
printf("Starting quick sort on array of size %u\n",n);
int startIdx = 0;
int endIdx = n-1;
quickSort(a, startIdx, endIdx);
printf("Sorted array:\n");
for(int i=0; i < n; i++)
{
printf("%d\n",a[i]);
}
findPair(a, n, sum);
return 0;
}