forked from IOSD/Algo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquick_sort.java
49 lines (38 loc) · 825 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
46
47
48
49
//Quick_Sort
import java.util.Scanner;
public class quick_sort {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = s.nextInt();
Quick_Sort(a, 0, a.length - 1);
for (int i = 0; i < n; i++)
System.out.print(a[i] + " ");
}
public static void Quick_Sort(int a[], int low, int high) {
if (low >= high)
return;
int left = low;
int right = high;
int mid = (left + high) / 2;
int pivot = a[mid];
while (left <= right) {
while (a[left] < pivot) {
left++;
}
while (a[right] > pivot) {
right--;
}
if (left <= right) {
int temp = a[right];
a[right] = a[left];
a[left] = temp;
}
left++;
right--;
}
}
}