package practice;
import java.util.Arrays;
public class QuickSort {
public static <T extends Comparable<T>> void sort(T[] list) {
partition(list, 0, list.length - 1);
}
private static <T extends Comparable<T>> void partition(T[] list,
int start, int end) {
if (end <= start)
return;
// 만약 랜덤 피봇을 추출하려고 한다면
// int p = start + new
// java.util.Random().nextInt(end - start + 1);
// T pivot = list[p];
// list[p] = list[end];
T pivot = list[end];
T temp = list[start];
int s = start;
int e = end;
for (int i = start; i < end; i++) {
if (temp.compareTo(pivot) < 0) {
list[s] = temp;
s++;
temp = list[s];
} else {
list[e] = temp;
e--;
temp = list[e];
}
}
list[e] = pivot;
System.out.println(temp);
partition(list, start, e - 1);
partition(list, e + 1, end);
}
public static void main(String[] args) {
int size = 30;
java.util.Random rand = new java.util.Random();
Integer arr[] = new Integer[size];
for (int i = 0; i < size; i++) {
arr[i] = rand.nextInt(3000);
}
Integer t[] = Arrays.copyOf(arr, arr.length);
Arrays.sort(t);
QuickSort.sort(arr);
for (Integer i : arr) {
System.out.print(i + " ");
}
System.out.println();
for (Integer i : t) {
System.out.print(i + " ");
}
}
}