作者dendrobium (石斛蘭)
看板C_and_CPP
標題Re: [問題] 利用指標完成quicksort
時間Mon Mar 22 11:15:18 2010
演算法本身就是錯的吧...
考慮 input[] = {3,1,2,3,4,5};
front last
| |
+---+---+---+---+---+---+
| 3 | 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+---+
meddle:3
因為 *last >= meddle 所以 last--
front last
| |
+---+---+---+---+---+---+
| 3 | 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+---+
meddle:3
again, *last >= meddle 所以 last--
front last
| |
+---+---+---+---+---+---+
| 3 | 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+---+
meddle:3
有等號的關係...again
front last
| |
+---+---+---+---+---+---+
| 3 | 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+---+
meddle:3
跳出while(), swap(*front,*last);
front last
| |
+---+---+---+---+---+---+
| 2 | 1 | 3 | 3 | 4 | 5 |
+---+---+---+---+---+---+
meddle:3
接著 *front <= meddle 所以 front++
front last
| |
+---+---+---+---+---+---+
| 2 | 1 | 3 | 3 | 4 | 5 |
+---+---+---+---+---+---+
meddle:3
直到
last front
| |
+---+---+---+---+---+---+
| 2 | 1 | 3 | 3 | 4 | 5 |
+---+---+---+---+---+---+
meddle:3
然後 swap(*last,*front)
last front
| |
+---+---+---+---+---+---+
| 2 | 1 | 4 | 3 | 3 | 5 |
+---+---+---+---+---+---+
meddle:3
你會發現, 這個演算法根本就不正確... ̄▽ ̄||
--
人家可不是為了你才這樣做的哦!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 60.198.35.85
推 nowar100:喔喔 有圖有推 XD 03/22 11:18
→ dendrobium:都是複製貼上啦XD 03/22 11:20
推 VictorTom:太熱心了, 推:) 03/22 11:23
推 mmmaaatttttt:推圖是一定要的啦XD 03/22 11:33
推 courant:有圖有推 03/22 11:39
推 loveme00835:有圖有推 03/22 15:26