-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
44 lines (42 loc) · 913 Bytes
/
QuickSort.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
43
44
package Module1;
import java.util.*;
public class QuickSort {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++)
a[i] = sc.nextInt();
quick_sort(a, 0 , n-1);
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
sc.close();
}
static void quick_sort(int a[], int low, int high) {
if(low<high) {
int j = partion(a, low, high);
quick_sort(a, low, j-1);
quick_sort(a, j+1, high);
}
}
static int partion(int a[], int low, int high) {
int pivot = a[low];
int i=low+1, j=high;
while(i<=j) {
if(a[i]<pivot)
i++;
else if(a[j]>pivot)
j--;
if(i<j)
interchange(a,i,j);
}
a[low] = a[j];
a[j] = pivot;
return j;
}
static void interchange(int a[], int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}