public class QuickSort {
static int i = 0;
public static void QuickSort(int[] a,int begin, int end) {
if(begin < end){
int p = partition(a, begin, end);
QuickSort(a,begin,p-1);
QuickSort(a,p + 1, end);
}
}
public static int partition(int[] a, int begin, int end){
int pivot = (begin + end) / 2;
int L = begin;
int R = end;
int temp;
System.out.println("[퀵 정렬 " + ++i + "단계: pivot: " + a[pivot]);
while(L < R){
while(a[L] < a[pivot] && L < R) L++;
while(a[R] >= a[pivot] && L < R) R--;
if(L<R){
temp = a[L];
a[L] = a[R];
a[R] = temp;
}
}
temp = a[pivot];
a[pivot] = a[R];
a[R] = temp;
for(int i = 0; i<=7; i++){
System.out.print(a[i] + " ");
}
System.out.println();
return L;
}
public static void main(String[] args) {
int[] a = {69,10,30,2,16,8,31,22};
QuickSort.QuickSort(a,0,7);
}
}
참고싸이트 : http://palpit.tistory.com/126
'Java > 자료구조와 알고리즘' 카테고리의 다른 글
Bubble Sort 예제 (0) | 2017.09.08 |
---|---|
Selection Sort (0) | 2017.09.07 |
TreeMap 으로 순서정렬 알고리즘 (0) | 2017.09.07 |
Tree set 으로 순서나열하기 <2> (0) | 2017.09.07 |
Tree set으로 순서 나열하기 <1> (0) | 2017.09.07 |