-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedian.cpp
86 lines (73 loc) · 1.6 KB
/
median.cpp
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
#include<iostream>
using namespace std;
void swapay(int arr[], int a, int b)
{
int t = arr[a];
arr[a] = arr[b];
arr[b] = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++)
{
// If current element is smaller than the pivot
if (arr[j] < pivot)
{
i++; // increment index of smaller element
swapay(arr, i, j);
}
}
swapay(arr, i+1, high);
return (i + 1);
}
int findmax(int arr[], int end){
int max = INT_MIN;
for (int i = 0; i < end; i++){
if (arr[i] > max){
max = arr[i];
}
}
return max;
}
int findmin(int arr[], int start, int size){
int mins = INT_MAX;
for (int i = start; i < size; i++){
if (arr[i] < mins){
mins = arr[i];
}
}
return mins;
}
int quickSort(int arr[], int low, int high, int n)
{
if (low < high)
{
int pi = partition(arr, low, high);
if(abs(pi-(n-pi-1))<=1){
if (abs(pi-(n-pi-1))==0){
return arr[pi];
}
else if (pi > n-pi-1){
return (findmax(arr, pi)+arr[pi])/2;
}
else{
return (findmin(arr, pi+1, n))/2;
}
}
else if (pi > n-pi-1){
return quickSort(arr, low, pi - 1, n);
}
else {
return quickSort(arr, pi + 1, high, n);
}
}
}
int main()
{
int foo [5] = { 16, 2, 77, 40, 12071 };
cout << quickSort(foo, 0, 4, 5);
cout << "\n";
return 0;
}