-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort.java
42 lines (41 loc) · 1.17 KB
/
Quick_Sort.java
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
public class Quick_Sort {
public static void printArr(int arr[]) {
for (int i = 0; i <= arr.length; i++) {
System.out.println(arr[i] + " ");
}
System.out.println();
}
public static void quicksort(int arr[],int start, int end){
if(start>=end){
return;
}
int index= partition(arr,start,end);
quicksort(arr,start,index-1);
quicksort(arr,index+1, end);
}
public static int partition(int arr[],int start,int end){
int pivot = arr[end];
int i= start-1;
for(int j=start; j<end; j++){
if(arr[j]<=pivot){
i++;
int temp =arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
i++;
int temp =pivot;
arr[end]=arr[i];
arr[i]=temp;
return i;
}
public static void main(String args[]) {
int arr[] = { 0, 9, 8, 7, 1, 2, 5, 7 };
quicksort(arr, 0, arr.length - 1);
printArr(arr);
}
}
//Time complexity = O(nlogn)
//No extra space required
//Worst time complexity= O(n2) when the probelm is divided into 1 and (n-1) parts.