1 / 18

Sort & Search

Sort & Search. 關於 Sort. What it is? When will we use it? Where can we take advantage of it? Why do we need it? How to implement it?. Sort 的演算法. Bubble sort Insertion sort Quick sort. Bubble sort. 每次迴圈都將最大的元素矩陣底部 實作時以互換讓元素被 “ 擠 ” 到最後面. 20 50 90 40 10 30  20 50 40 10 30 90

sian
Download Presentation

Sort & Search

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sort & Search

  2. 關於Sort • What it is? • When will we use it? • Where can we take advantage of it? • Why do we need it? • How to implement it?

  3. Sort的演算法 • Bubble sort • Insertion sort • Quick sort

  4. Bubble sort • 每次迴圈都將最大的元素矩陣底部 • 實作時以互換讓元素被“擠”到最後面 • 205090401030 • 205040103090 • 204010305090 • 201030405090 • 102030405090 • 102030405090

  5. Bubble sort • 屬於實作及概念都是最簡易的演算法 • 時間複雜度 • 平均

  6. Insertion sort • 每次迴圈將特定元素放到該放的位置 • 以矩陣實作將導致大量元素位移 • 205090401030 • 205090401030 • 205090401030 • 205090401030 • 2040 50901030 • 10 2040 509030 • 10 2030 40 5090

  7. Insertion sort • 應用上鏈結串列較矩陣合適 • 時間複雜度 • 平均

  8. Quick Sort • 每次的遞迴都會決定特定元素(pivot)的位置,以及其餘元素的相對位置 • 簡而言之,小的去前面,大的到後面 R Pivot L 50 20 60 10 30 70 90 40 1.L:找到比pivot大的元素為止往後搜尋 2.R:找到比pivot小的元素為止往前搜尋 3.LR都找到元素時互換,回到1. 4.當R撞上L時停止迴圈,pivot和R互換 5.將序列分為pivot左邊和pivot右邊再分別做quick sort

  9. Quick Sort • 目前速度最快的演算法 • 時間複雜度 • 平均

  10. Sort:總結 • 穩定性 • 穩定:Bubble sort、Insertion sort • 不穩定: Quick sort • 仍有許多其他特色各異的演算法 • Merge sort • Radix sort

  11. 關於Search • 在指定空間內搜尋特定元素 • 搜尋的種類:依母空間區分 • 字串搜尋 • 最大子字串、KMP演算法 • 圖搜尋 • DFS、BFS • 陣列搜尋

  12. Search的演算法 • Linearsearch • Binary search

  13. Linearsearch • 簡稱暴力搜尋法 • 從頭依序比較直到找到指定元素為止 • 時間複雜度 • 平均

  14. Binary search • 從中間開始,依據元素的相對關係查找 例:於升冪的序列中搜尋元素32 18 21 26 32 42 47 48 49 54 54 67 68 75 80 84 89 93 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 INDEX: *若該元素大於目標元素,查看該元素左邊序列的中間元素 *若該元素小於目標元素,查看該元素右邊序列的中間元素

  15. Binary search • 僅適用於已排序之序列 • 時間複雜度 • 平均

  16. Search總結 • 根據序列排序與否,搜尋會有不同的選擇 • 排序+BinarySearch可用於最佳化執行時間

  17. 習題 • 基礎 • 299Train Swapping • 10062 Tell me the frequencies! • 進階 • 755 487-3279 • 10008 What's Cryptanalysis?

  18. Sort & Search • 參考資料: • 2010寒訓講義 • 微基Wiki

More Related