-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathquick_sort_using_rec.cpp
More file actions
58 lines (49 loc) · 1003 Bytes
/
quick_sort_using_rec.cpp
File metadata and controls
58 lines (49 loc) · 1003 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
58
#include <bits/stdc++.h>
using namespace std;
int partiton(int arr[], int start, int end)
{
int i = start - 1;
int j = start;
int pivot = arr[end];
for(;j < end; j++)
{
if(arr[j] <= pivot)
{
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i+1], arr[end]);
return i+1;
}
void quick_sort(int arr[], int start, int end)
{
if (start >= end)
return;
int p = partiton(arr, start, end);
quick_sort(arr, start, p - 1);
quick_sort(arr, p + 1, end);
}
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int size;
cout << "Enter the size of array" << endl;
cin >> size;
int *arr = new int[size];
cout << "Enter array of size " << size << endl;
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
quick_sort(arr, 0, size - 1);
printArray(arr, size);
return 0;
}