-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
50 lines (47 loc) · 1.09 KB
/
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
45
46
47
48
49
50
/**
* QuickSort
*/
import java.util.*;;
public class QuickSort {
public static int pvt(int arr[],int low,int high)
{
int pvt=arr[low];
int i=low,j=high;
while(i<j)
{
while(arr[i]<=pvt && i<=high-1)
i++;
while(arr[j]>pvt && j>=low+1)
j--;
if(i<j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
int temp=arr[low];
arr[low]=arr[j];
arr[j]=temp;
return j;
}
public static void qs(int arr[],int low,int high)
{
if(low<high)
{
int pvt=pvt(arr, low, high);
qs(arr, low, pvt-1);
qs(arr, pvt+1, high);
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
qs(arr, 0, arr.length-1);
System.out.println(Arrays.toString(arr));
sc.close();
}
}