forked from fnplus/Algorithms-Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_sort.java
45 lines (36 loc) · 855 Bytes
/
quick_sort.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
// add your code here :
public class QuickSort {
private static int tab[];
private static int n;
public QuickSort(int n, int[] tab) {
this.n = n;
this.tab = tab;
quicksort(tab,0,n);
}
private static void quicksort(int tab[], int x, int y) {
int i, j, v, temp;
i = x;
j = y;
v = tab[(x + y) / 2];
do {
while (tab[i] < v) {
i++;
}
while (v < tab[j]) {
j--;
}
if (i <= j) {
temp = tab[i];
tab[i] = tab[j];
tab[j] = temp;
i++;
j--;
}
}
while (i <= j);
if (x < j)
quicksort(tab, x, j);
if (i < y)
quicksort(tab, i, y);
}
}