내 quickSort가 왜 그렇게 느린지 궁금합니다. 다음 배열을 정렬하는 데 10-20 초가 걸립니다. Bubblesort (아래 그림 참조)가 자동으로 수행됩니다.QuickSort가 BubbleSort보다 속도가 훨씬 느린 이유는 무엇입니까?
public static void quickSort(int[] tab, int lowIndex, int highIndex) {
if (tab == null || tab.length == 0) {
return;
}
int i = lowIndex;
int j = highIndex;
int pivot = tab[lowIndex + (highIndex - lowIndex)/2];
while (i <= j) {
while (tab[i] < pivot) {
i++;
}
while (tab[j] > pivot) {
j--;
}
if (i <= j) {
int c = tab[i];
tab[i] = tab[j];
tab[j] = c;
i++;
j--;
}
if (lowIndex < j) {
quickSort(tab, lowIndex, j);
}
if (i < highIndex) {
quickSort(tab, i, highIndex);
}
}
}
public static void bubbleSort(int[] arr) {
int n = arr.length - 1;
while (n >= 1) {
for (int i = 0; i < n; i++) {
if (arr[i] > arr[i + 1]) {
int c = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = c;
}
}
n--;
}
}
public static void main(String[] args) {
int[] t = new int[] { 5, 1, 33, 13, 21, 2, 12, 4,
2, 3, 53, 2, 125, 23, 53, 523, 5, 235, 235, 235, 23, 523, 1,
2, 41, 2412, 412, 4, 124, 1, 241, 24, 1, 43, 6, 346, 457, 56,
74, 5, 3, 5, 1, 33, 13, 21, 2, 12, 4,
2, 3, 53, 2, 125, 23, 53, 523, 5, 235, 235, 235, 23, 523, 1,
2, 41, 2412, 412, 4, 124, 1, 241, 24, 1, 43, 6, 346, 457, 56,
74, 5, 3, 74, 5, 3, 5, 1, 33, 13, 21, 2, 12, 4,
2, 3, 53, 2, 125, 23, 53, 523, 5, 235, 235, 235, 23, 523, 1,
2, 41, 2412, 412, 4, 124, 1, 241, 24, 1, 43, 6, 346, 457, 56,
74, 5, 3 };
quicksort 구현에 재귀 호출이 포함되어 있으므로 작은 데이터 세트의 경우 Quicksort가 느리게 보일 수 있습니다. – wns349
거품 껍질의 최악의 경우의 강약은 n^2입니다. quicksort의 wortcase 복잡도는 n * ln (n)입니다. 이것은 최악의 경우이며, 개별적인 경우에는 아무 것도 말하지 않습니다. 데이터가 얼마나 무작위이며 얼마나 큰가? '자동'은 무엇을 의미합니까? –
내 코드의 퀵 소트 시간은 ~ 15 초이며 bubblesort는 1 초 미만입니다. – Stuart