1 / 11

泡沫排序法

泡沫排序法. Bubble Sort. 也稱之為冒泡排序法 就 像泡泡一樣,最大的數值會往尾端移動,最小的數值會往前端移動。. 35. 12. 77. 101. 5. 42. Sorting. Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6. 101. 12. 42. 35. 5. 77.

Download Presentation

泡沫排序法

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. 泡沫排序法

  2. Bubble Sort • 也稱之為冒泡排序法 • 就像泡泡一樣,最大的數值會往尾端移動,最小的數值會往前端移動。

  3. 35 12 77 101 5 42 Sorting • Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 101 12 42 35 5 77 1 2 3 4 5 6

  4. "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 101 12 42 35 5 77

  5. 42 77 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping Swap 1 2 3 4 5 6 101 12 42 35 5 77

  6. 35 77 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 101 12 77 35 5 42

  7. 12 77 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 101 12 35 77 5 42

  8. "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 101 77 35 12 5 42 No need to swap

  9. 5 101 "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 Swap 101 77 35 12 5 42

  10. "Bubbling Up" the Largest Element • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 101 5 77 35 12 42 Largest value correctly placed

  11. (1)比較相鄰的兩個元素,若前面的元素較大就進行交換。(1)比較相鄰的兩個元素,若前面的元素較大就進行交換。 • (2)重複進行(1)的動作直到最後面,最後一個元素將會是最大值。 • 重複進行(1),(2)的動作,每次比較到上一輪的最後一個元素。

More Related