-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
61 lines (54 loc) · 1.25 KB
/
QuickSort.java
File metadata and controls
61 lines (54 loc) · 1.25 KB
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
51
52
53
54
55
56
57
58
59
60
61
public class Solution {
public static void quickSort(int[] input) {
int s = 0;
int e = input.length-1;
quickS(input,s,e);
}
public static void quickS(int[] input, int si, int ei)
{
if(si>=ei)
{
return;
}
int pivotIndex = partition(input,si,ei);
quickS(input,pivotIndex+1,ei);
quickS(input,si,pivotIndex-1);
}
public static int partition(int a[],int si,int ei)
{
int pivotElement = a[si];
int smallnumcount = 0;
for(int i = si+1;i<=ei;i++)
{
if(a[i]<pivotElement)
{
smallnumcount++;
}
}
int temp = a[si + smallnumcount];
a[si+smallnumcount]= pivotElement;
a[si]=temp;
int i = si;
int j = ei;
while(i<j)
{
if(a[i]<pivotElement)
{
i++;
}
else if(a[j]>= pivotElement)
{
j--;
}
else
{
temp = a[i];
a[i]=a[j];
a[j] = temp;
i++;
j--;
}
}
return si + smallnumcount;
}
}