請問這題Quick Sort步驟是什麼

2009-04-18 10:35 pm
(1, 8, 2, 7, 3, 5, 4, 6)
show the process step-by-step in increasing order

回答 (2)

2009-04-20 7:18 am
✔ 最佳答案
Can I answer this in English?

2009-04-19 23:18:01 補充:
function quicksort(array)
var list less, greater
if length(array) ≤ 1
return array
select and remove a pivot value pivot from array
for each x in array
if x ≤ pivot then append x to less
else append x to greater
return concatenate(quicksort(less), pivot, quicksort(greater))

To find the pivot, use median of 3. Median of median is the best but will be complicated. median = first element, last element and
(int) (first + last) / 2 or first element if array < 3

1 pivot is median( 1, 7, 6 ) = 6
2 1, 2, 3, 5, 4 goes to less and 8, 7 goes to grea
3 quicksort(less: 1, 2, 3, 5, 4):
4 pivot is median(1, 3, 4) = 3
5 1, 2 goes to less and 5, 4, 6 goes to greater
6 quicksort(less: 1, 2):
7 pivot is median(1, 2) = 1
8 NULL goes to less and 2 goes to greater
9 quicksort(less: NULL):
10 return NULL
11 quicksort(greater: 2):
12 return 2
13 return concatenate(NULL, 1, 2)
14 quicksort(greater: 5, 4, 6):
15 pivot is median(5, 4, 6) = 5
16 4 goes to less and 6 goes to greater
17 quicksort(less: 4):
18 return 4
19 quicksort(greater: 6):
20 return 6
21 concatenate(4, 5, 6)
22 concatenate(1, 2, 3, 4, 5, 6)
23 quicksort(greater: 8, 7):
24 pivot is median(8, 7) = 8
25 7 goes to less, NULL goes to greater
26 quicksort(less: 7):
27 return 7
28 quicksort(greater: NULL)
29 return NULL
30 concatenate(7, 8, NULL)
31 concatenate(1, 2, 3, 4, 5, 6, 7, 8)

The quicksort algorithm is from wiki. I have added median of 3 to it.
Note that the algorithm uses about 31 steps to sort in this case. You can try some other arrays to convince yourself the sort is O(nlogn).
2009-04-20 2:19 am
yes you can
please tell me the answer, THX


收錄日期: 2021-04-30 12:55:11
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090418000010KK05469

檢視 Wayback Machine 備份