310 likes | 370 Views
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.
E N D
What does sorting mean? Sorting means to arrange the data in a certain order.
The previous movie clip shows the concept of swapping. In fact, swapping means to interchange of two items.
VariableX VariableY temporary To swap the content of variable X and Y, we need to employ a temporary variable. X Y
VariableX VariableY temporary Assign the content in variableX to the temporary variable. Temporary := VariableX; X Y X
VariableX VariableY temporary Assign the content in variableY to the variableX. VariableX := VariableY; Y Y X
VariableX VariableY temporary Assign the content in the temporary variable to the variableY. VariableY := temporary; Y X X
VariableX VariableY temporary The contents of variable X and Y are swapped. Y X X
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.
To illustrate the concept of bubble sort, we are going to sort a list of 4 items in ascending order step by step.
84 Score [1] 74 Score [2] 97 Score [3] 16 Score [4] Sorting in ascending order
84 Score [1] 74 Score [2] 97 Score [3] 16 Score [4] Sorting in ascending order Pass 1 84 > 74 Swap
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 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
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
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
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
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
74 Score [1] 84 Score [2] 16 Score [3] 97 Score [4] Sorting in ascending order Pass 2
74 Score [1] 84 Score [2] 16 Score [3] 97 Score [4] Sorting in ascending order Pass 2
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
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
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
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
74 Score [1] 16 Score [2] 84 Score [3] 97 Score [4] Sorting in ascending order Pass 3
74 Score [1] 16 Score [2] 84 Score [3] 97 Score [4] Sorting in ascending order Pass 3
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
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
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.
Advantage and Disadvantage of Bubble Sort: Advantage - simple to implement. Disadvantage - slow in run time.