-
Notifications
You must be signed in to change notification settings - Fork 4k
/
QuickSort
35 lines (34 loc) · 853 Bytes
/
QuickSort
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
class Solution
{
//Function to sort an array using quick sort algorithm.
static void quickSort( int arr[],int l, int h)
{
// code here
if(l<h) {
int pivot = partition(arr,l,h);
quickSort(arr,l,pivot-1);
quickSort(arr,pivot +1 , h);
}
}
static int partition(int arr[], int l, int h)
{
// your code here
int pivot = arr[l];
int i = l;
int j = h;
while(i<j) {
while(arr[i]<=pivot && i<j) i++;
while(arr[j]>pivot) j--;
if(i<j) {
swap(arr,i,j);
}
}
swap(arr,j,l);
return j;
}
static void swap(int arr[], int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}