1 / 31

Sorting

Sorting. What does sorting mean?. Sorting means to arrange the data in a certain order. How to interchange the content of two glasses of water ?. The previous movie clip shows the concept of swapping. In fact, swapping means to interchange of two items. VariableX. VariableY.

Download Presentation

Sorting

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. Sorting

  2. What does sorting mean? Sorting means to arrange the data in a certain order.

  3. How to interchange the content of two glasses of water ?

  4. The previous movie clip shows the concept of swapping. In fact, swapping means to interchange of two items.

  5. VariableX VariableY temporary To swap the content of variable X and Y, we need to employ a temporary variable. X Y

  6. VariableX VariableY temporary Assign the content in variableX to the temporary variable. Temporary := VariableX; X Y X

  7. VariableX VariableY temporary Assign the content in variableY to the variableX. VariableX := VariableY; Y Y X

  8. VariableX VariableY temporary Assign the content in the temporary variable to the variableY. VariableY := temporary; Y X X

  9. VariableX VariableY temporary The contents of variable X and Y are swapped. Y X X

  10. What is bubble Sort? Bubble sort is a sorting algorithm. The idea of bubble sort is to compare all adjacent (相鄰) elements in the array successively (遂一地) and swap them if they are not in right order.

  11. To illustrate the concept of bubble sort, we are going to sort a list of 4 items in ascending order step by step.

  12. 84 Score [1] 74 Score [2] 97 Score [3] 16 Score [4] Sorting in ascending order

  13. 84 Score [1] 74 Score [2] 97 Score [3] 16 Score [4] Sorting in ascending order Pass 1 84 > 74 Swap

  14. 84 74 Score [1] 74 84 Score [2] 97 97 Score [3] 16 16 Score [4] Sorting in ascending order Pass 1 84 > 74 Swap

  15. 84 74 Score [1] 74 84 Score [2] 97 97 Score [3] 16 16 Score [4] Sorting in ascending order Pass 1 84 > 74 Swap 84 < 97 No Swap

  16. 84 74 74 Score [1] 74 84 84 Score [2] 97 97 97 Score [3] 16 16 16 Score [4] Sorting in ascending order Pass 1 84 > 74 Swap 84 < 97 No Swap

  17. 84 74 74 Score [1] 84 84 74 Score [2] 97 97 97 Score [3] 16 16 16 Score [4] Sorting in ascending order Pass 1 84 > 74 Swap 84 < 97 No Swap 97 > 16 Swap

  18. 84 74 74 74 Score [1] 84 84 84 74 Score [2] 97 97 97 16 Score [3] 16 16 97 16 Score [4] Sorting in ascending order Pass 1 84 > 74 Swap 84 < 97 No Swap 97 > 16 Swap

  19. 84 74 74 Score [1] 84 84 74 Score [2] 97 97 97 Score [3] 16 16 16 Score [4] Sorting in ascending order Pass 1 74 84 16 97 97 is placed in the correct position

  20. 74 Score [1] 84 Score [2] 16 Score [3] 97 Score [4] Sorting in ascending order Pass 2

  21. 74 Score [1] 84 Score [2] 16 Score [3] 97 Score [4] Sorting in ascending order Pass 2

  22. 74 74 Score [1] 84 84 Score [2] 16 16 Score [3] 97 97 Score [4] Sorting in ascending order Pass 2 74 < 84 No Swap

  23. 74 74 Score [1] 84 84 Score [2] 16 16 Score [3] 97 97 Score [4] Sorting in ascending order Pass 2 74 < 84 No Swap

  24. 74 74 74 Score [1] 16 84 84 Score [2] 16 16 84 Score [3] 97 97 97 Score [4] Sorting in ascending order Pass 2 74 < 84 No Swap 84 > 16 Swap

  25. 74 74 Score [1] 84 84 Score [2] 16 16 Score [3] 97 97 Score [4] Sorting in ascending order Pass 2 74 16 84 97 84 is placed in the correct position

  26. 74 Score [1] 16 Score [2] 84 Score [3] 97 Score [4] Sorting in ascending order Pass 3

  27. 74 Score [1] 16 Score [2] 84 Score [3] 97 Score [4] Sorting in ascending order Pass 3

  28. 74 16 Score [1] 16 74 Score [2] 84 84 Score [3] 97 97 Score [4] Sorting in ascending order Pass 3 74 > 16 Swap

  29. 74 Score [1] 16 Score [2] 84 Score [3] 97 Score [4] Sorting in ascending order Pass 3 16 74 84 97 74 > 16 Swap

  30. 74 Score [1] 16 Score [2] 84 Score [3] 97 Score [4] Sorting in ascending order Pass 3 16 74 84 97 All elements are sorted.

  31. Advantage and Disadvantage of Bubble Sort: Advantage - simple to implement. Disadvantage - slow in run time.

More Related