Upgrade to Pro — share decks privately, control downloads, hide ads and more …

冒泡排序

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for bin Chou bin Chou
December 18, 2024
10

 冒泡排序

Avatar for bin Chou

bin Chou

December 18, 2024

Transcript

  1. Bubble Sort • In bubble sort, the idea of the

    algorithm is to move higher valued elements generally towards the right and lower value elements generally towards the left. In pseudocode: • Set swap counter to a non-zero value • Repeat until the swap counter is 0: • Reset swap counter to 0 • Look at each adjacent pair • If two adjacent elements are not in order, swap them and add one to the swap counter
  2. Bubble Sort 5 2 1 3 6 4 In pseudocode:

    Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter Swap Counter -1
  3. Bubble Sort 5 2 1 3 6 4 Swap Counter

    0 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  4. Bubble Sort 5 2 1 3 6 4 Swap Counter

    0 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  5. Bubble Sort 2 5 1 3 6 4 Swap Counter

    1 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  6. Bubble Sort 2 5 1 3 6 4 Swap Counter

    1 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  7. Bubble Sort 2 1 5 3 6 4 Swap Counter

    2 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  8. Bubble Sort 2 1 5 3 6 4 Swap Counter

    2 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  9. Bubble Sort 2 1 3 5 6 4 Swap Counter

    3 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  10. Bubble Sort 2 1 3 5 6 4 Swap Counter

    3 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  11. Bubble Sort 2 1 3 5 6 4 Swap Counter

    3 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  12. Bubble Sort 2 1 3 5 4 6 Swap Counter

    4 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  13. Bubble Sort 2 1 3 5 4 6 Swap Counter

    4 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  14. Bubble Sort 2 1 3 5 4 6 Swap Counter

    0 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  15. Bubble Sort 2 1 3 5 4 6 Swap Counter

    0 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  16. Bubble Sort 1 2 3 5 4 6 Swap Counter

    1 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  17. Bubble Sort 1 2 3 5 4 6 Swap Counter

    1 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  18. Bubble Sort 1 2 3 5 4 6 Swap Counter

    1 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  19. Bubble Sort 1 2 3 5 4 6 Swap Counter

    1 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  20. Bubble Sort 1 2 3 4 5 6 Swap Counter

    2 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  21. Bubble Sort 1 2 3 4 5 6 Swap Counter

    2 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  22. Bubble Sort 1 2 3 4 5 6 Swap Counter

    0 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  23. Bubble Sort 1 2 3 4 5 6 Swap Counter

    0 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  24. Bubble Sort 1 2 3 4 5 6 Swap Counter

    0 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  25. Bubble Sort 1 2 3 4 5 6 Swap Counter

    0 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  26. Bubble Sort 1 2 3 4 5 6 Swap Counter

    0 In pseudocode: Set swap counter to a non-zero value Repeat until the swap counter is 0: Reset swap counter to 0 Look at each adjacent pair If two adjacent elements are not in order, swap them and add one to the swap counter
  27. Bubble Sort • Worst-case scenario: The array is in reverse

    order; we have to “bubble” each of the n elements all the way across the array, and since we can only fully bubble one element into position per pass, we must do this n times. • Best-case scenario: The array is already perfectly sorted, and we make no swaps on the first pass.