-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort-Quick.cpp
More file actions
57 lines (48 loc) · 970 Bytes
/
Sort-Quick.cpp
File metadata and controls
57 lines (48 loc) · 970 Bytes
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
#include <iostream>
using namespace std;
int partition(int *arr, int start, int end)
{
int pivot = arr[end];
int index = start;
for(int i=start ; i<end ; i++)
{
if(arr[i] <= pivot)
{
swap(arr[i], arr[index]);
index++;
}
}
swap(arr[index],arr[end]);
return index;
}
void sortArray(int *arr, int start, int end)
{
if(start < end) //to manage unwanted segments or write it as (start>= end) then return
{
int index = partition(arr,start,end);
sortArray(arr,start,index-1);
sortArray(arr,index+1,end);
}
}
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void printArray(int *arr, int size)
{
for(int i=0 ; i<size ; i++)
{
cout << *arr << " ";
*arr++;
}
cout << endl;
}
int main()
{
int arr[8] = {7,2,1,6,8,5,3,4};
printArray(arr,8);
sortArray(arr,0,8);
printArray(arr,8);
}