빠른 정렬 시간 복잡도와 공간 복잡도
5232 단어 java 데이터 구조
import java.awt.List;
/**
* Created by shuxing on 2017/7/12.
*/
public class QuickSort {
public static int[] quickSort(int[] list) {
quickSort(list, 0, list.length - 1);
return list;
}
public static int[] quickSort(int[] list, int first, int last) {
if (first < last) {// (pivot)
int pivotIndex = partition(list, first, last);
quickSort(list, first, pivotIndex - 1);
quickSort(list, pivotIndex + 1, last);
}
return list;
}
public static int partition(int[] list, int first, int last) {//
int pivot = list[first], low = first + 1, high = last;
//
while (high > low) {
while (pivot >= list[low] && low <= high)
low++;
while (pivot < list[high] && low <= high)
high--;
//
if (low < high) {
int tmp = list[low];
list[low] = list[high];
list[high] = tmp;
}
}
//
while (list[high] >= pivot && high > first)
high--;
if (list[high] < pivot) {
list[first] = list[high];
list[high] = pivot;
return high;
}
else {
return first;
}
}
public static void main(String[] args) {
int[] list = {2,6,3,5,4,1,8,45,2};
list = quickSort(list);
for (int i = 0; i < list.length; i++) {
System.out.println(list[i]);
}
}
}
더욱 알기 쉬운 빠른 정렬 알고리즘을 볼 수 있습니다.http://blog.csdn.net/morewindows/article/details/6684558