-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_sorting.cpp
64 lines (59 loc) · 1.3 KB
/
quick_sorting.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
#include <iostream>
#include <vector>
using namespace std;
int partition(int arr[], int start, int end)
{ int pivot_element_location = start ;
while (start < end)
{
for (int i = start; i < 9; i++)
{
if (arr[i] > arr[pivot_element_location])
{
start = i;
break;
}
}
for (int j = end; j > 0; j--)
{
if (arr[j] < arr[pivot_element_location])
{
end = j;
break;
}
}
if (start < end)
{
int temp_1 = arr[start];
arr[start] = arr[end];
arr[end] = temp_1;
}
}
int temp_2 = arr[pivot_element_location];
arr[pivot_element_location] = arr[end];
arr[end] = temp_2;
return end ;
}
void QuickSort(int arr[], int lb, int ub)
{
int location;
if (lb < ub)
{
location = partition(arr, lb, ub);
QuickSort(arr, location + 1, ub);
QuickSort(arr, lb, location - 1);
}
}
int main()
{
int arr[] = {7, 6, 10, 5, 9, 2, 1, 15, 7};
int start, end;
start = 0;
end = 9 ;
QuickSort(arr, start , end );
//displaying Sorted array
for (int i = 0; i < 9; i++)
{
cout << arr[i] << "||";
}
return 0;
}