-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuicksort.java
44 lines (35 loc) · 885 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 com.sai;
public class Quicksort {
public static void main(String[] args) {
int[] inputArr = { 45, 23, 11, 89, 77, 98, 4, 28, 65, 43 };
Quicksort qs = new Quicksort();
int[] temp = qs.Qsort(inputArr, 0, 9);
for (int i : temp) {
System.out.println(i);
}
}
public int[] Qsort(int[] temp, int start, int end) {
if (start < end) {
int pIndex = partition(temp, start, end);
Qsort(temp, start, pIndex - 1);
Qsort(temp, pIndex + 1, end);
}
return temp;
}
public int partition(int[] temp, int start, int end) {
int pivot = temp[end];
int pIndex = start;
for (int i = start; i < end; i++) {
if (temp[i] <= pivot) {
int tempPindex = temp[pIndex];
temp[pIndex] = temp[i];
temp[i] = tempPindex;
pIndex++;
}
}
int tempPindex = temp[pIndex];
temp[pIndex] = temp[end];
temp[end] = tempPindex;
return pIndex;
}
}