Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Sorting means to arrange the data in a certain order. What does sorting mean?"— Presentation transcript:

1

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

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 To swap the content of variable X and Y, we need to employ a temporary variable. VariableXVariableYtemporary XY

6 Assign the content in variableX to the temporary variable. Temporary := VariableX; VariableXVariableYtemporary XYX

7 Assign the content in variableY to the variableX. VariableX := VariableY; VariableXVariableYtemporary YYX

8 Assign the content in the temporary variable to the variableY. VariableY := temporary; VariableXVariableYtemporary YXX

9 The contents of variable X and Y are swapped. VariableXVariableYtemporary YXX

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 74 97 16 Score [1] Score [2] Score [3] Score [4] Sorting in ascending order

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

Similar presentations


Ads by Google