-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick Sort.cpp
More file actions
41 lines (40 loc) · 868 Bytes
/
Quick Sort.cpp
File metadata and controls
41 lines (40 loc) · 868 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
class Solution
{
public:
//Function to sort an array using quick sort algorithm.
void quickSort(int arr[], int low, int high)
{
if(low<high)
{
int p=partition(arr, low, high);
quickSort(arr, low, p-1);
quickSort(arr, p+1, high);
}
// code here
}
public:
int partition (int arr[], int low, int high)
{
int pivot=arr[low];
int i=low;
int j=high;
while(i<j)
{
while(arr[i]<=pivot && i<=high-1)
{
i++;
}
while(arr[j]>pivot && j>=low+1)
{
j--;
}
if(i<j)
{
swap(arr[i],arr[j]);
}
}
swap(arr[low],arr[j]);
return j;
// Your code here
}
};