-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
78 lines (63 loc) · 1.41 KB
/
Copy pathQuickSort.java
File metadata and controls
78 lines (63 loc) · 1.41 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.*;
public class QuickSort {
public static void swap(int a[], int i, int j){
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
return;
}
public static int partition_car_hoare(int a[], int p, int r){
int i, j, x;
x = a[p];
i = p - 1;
j = r + 1;
while(true){
while(x >= a[++i]);
while(x <= a[--j]);
if(i < j)
swap(a, i, j);
else{
swap(a, j, p);
return(j);
}
}
}
public static int partition_n_lomuto(int a[], int p, int r){
int i, j, x;
i = p - 1;
x = r;
for(j = p;j <= r;j++){
if(a[x] >= a[j]){
i++;
swap(a, j, i);
}
}
return(i);
}
public static void quick_sort(int a[], int p, int r){
int q, i;
if(p < r){
q = partition_n_lomuto(a, p, r);
quick_sort(a, p, q - 1);
quick_sort(a, q + 1, r);
}
return;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = Integer.parseInt(in.nextLine());
for (int i = 1; i <= t; ++i) {
int n = Integer.parseInt(in.nextLine());
int[] a = new int[n];
for(int j = 0;j < n;j++){
a[j] = in.nextInt();
}
quick_sort(a, 0, n-1);
for(int j = 0;j < n;j++){
System.out.println(a[j]);
}
}
in.close();
}
}