-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSortP1.java
46 lines (43 loc) · 968 Bytes
/
QuickSortP1.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
import java.util.*;
public class QuickSortP1
{
static void partition(int[] ar)
{
int temp=0;
int pivot=ar[0];
int pIndex=ar.length-1;
for(int i=ar.length-1;i>=1;i--)
{
if(ar[i]>=pivot)
{
temp=ar[i];
ar[i]=ar[pIndex];
ar[pIndex]=temp;
pIndex-=1;
}
}
temp=ar[pIndex];
ar[pIndex]=ar[0];
ar[0]=temp;
printArray(ar);
}
static void printArray(int[] ar)
{
for(int n: ar)
{
System.out.print(n+" ");
}
System.out.println("");
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] ar = new int[n];
for(int i=0;i<n;i++)
{
ar[i]=in.nextInt();
}
partition(ar);
}
}