Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Algorithms.

Similar presentations


Presentation on theme: "Sorting Algorithms."— Presentation transcript:

1 Sorting Algorithms

2 Lesson Objectives Aims To understand the following SORTING algorithms:
Bubble Sort Merge Sort Insertion Sort To be able to construct a flow chart for each algorithm

3 Bubble Sort Bubble sort – moving through a list repeatedly, comparing pairs of elements, swapping elements that are in the wrong order.

4 Bubble Sort Algorithm SwapsMade = True Repeat until SwapsMade = False
Take the first element and second element from the list IF element 1 > element 2 THEN Swap them ELSE Do nothing IF no more elements are left: IF SwapsMade = True, then return to the start – GOTO Step 3 ELSE set SwapsMade = False End Repeat

5 Bubble Sort - Example Element Number Value 1 2 3 4 5 6 7 8 13 9 10 12
1) Compare elements 1 and 2 2) Is element 1 > element 2? 1 2 3 4 5 6 7 8 13 9 10 12 3) Yes: so swap them 1 2 3 4 5 6 7 8 13 9 10 12 4) Compare elements 2 and 3 5) Is element 2 > element 3? 1 2 3 4 5 6 7 8 13 9 10 12 6) Yes: so swap them

6 Bubble Sort - Example Talk through the next steps that are taken in the bubble sort: 1 2 3 4 5 6 7 8 13 9 10 12 1 2 3 4 5 6 7 8 13 9 10 12 1 2 3 4 5 6 7 8 13 9 10 12 1 2 3 4 5 6 7 8 9 13 10 12

7 Bubble Sort - Example 1 2 3 4 5 6 7 8 9 13 10 12 Compare elements 5 and 6 Is element 5 > element 6? 1 2 3 4 5 6 7 8 9 10 13 12 Yes, so swap them 1 2 3 4 5 6 7 8 9 10 13 12 Compare elements 6 and 7 Is element 6 > element 7? 1 2 3 4 5 6 7 8 9 10 12 13 Yes, so swap them

8 You have come to the end of the list.
Bubble Sort - Example 1 2 3 4 5 6 7 8 9 10 12 13 Compare elements 7 and 8 Is element 7 > element 8? 1 2 3 4 5 6 7 8 9 10 12 13 Yes, so swap them You have come to the end of the list. A change has been made. So you start again. Compare elements 1 and 2 1 2 3 4 5 6 7 8 9 10 12 13 Swap? No

9 Bubble Sort - Example Compare 2 and 3 1 2 3 4 5 6 7 8 9 10 12 13
Swap? No Compare 3 and 4 1 2 3 4 5 6 7 8 9 10 12 13 Swap? No Compare 4 and 5 1 2 3 4 5 6 7 8 9 10 12 13 Swap? No Compare 5 and 6 1 2 3 4 5 6 7 8 9 10 12 13 Swap? No

10 Bubble Sort - Example You’ve reached the end. Have you made any swaps?
Compare 6 and 7 1 2 3 4 5 6 7 8 9 10 12 13 Swap? Yes 1 2 3 4 5 6 7 8 9 10 12 13 Compare 7 and 8 1 2 3 4 5 6 7 8 9 10 12 13 Swap? No You’ve reached the end. Have you made any swaps? Yes, so start again.

11 Sample exam question

12

13 Merge Sort List – a set of data. Merge sort:
The list is split into individual elements These lists are then combined into a NEW list, 2 lists at a time.

14 Merge Sort Algorithm Split the list into individual elements
Get two lists Compare the first element in both lists. Put the smallest into a new list. Continue steps 3 and 4 with each remaining element in the two lists Get the next two lists and repeat 3-5 Repeat 2 – 6 until only 1 list remains

15 Merging just two lists List A List B Merged List 2 5 16 or 1 8 15 22 

16 Show each step of the process.
Task Merge the two lists: Show each step of the process. If you have two values the same, add one (it doesn’t matter from which list), and then add the second. 3 7 8 10 11 4 6 8 13

17 Merge Sort – Complete Example
22 13 5 2 8 15 Split the list into individual elements: 22 13 5 2 8 15 22 Merge individual lists 13 22 2 5 8 15 22 Merge sub-lists 2 5 13 22 8 15 22 Merge sub-lists 2 5 8 13 15 22

18 Use a merge sort to sort the list:
Task Use a merge sort to sort the list: 2 19 3 45 77 10 25 29 60 1

19 Answer 2 19 3 45 77 10 25 29 60 1 2 19 3 45 77 10 25 29 60 1 2 19 3 45 10 77 25 29 1 60 2 3 19 45 10 25 29 77 1 60 2 3 10 19 25 29 45 77 1 60 1 2 3 10 19 25 29 45 60 77

20 Insertion Sort Inserting each element in to the correct location! Uses the concept of a “sorted list” and an “unsorted list”

21 Insertion Sort - Algorithm
Take the first element 1 consider this to be in the right place - the ‘sorted’ list. The remaining elements are an ‘unsorted’ list. Get the next element from the ‘unsorted’ list. Compare this to the first element in the ‘sorted’ list. IF it is smaller, put it in front of that element (move the others along). ELSEIF it is larger, compare with the next. ELSEIF there are no more elements in the ‘sorted’ list put it in the final position. REPEAT UNTIL all element in the ‘unsorted’ list are in the ‘sorted’ list.

22 Insertion Sort Insertion sort this list 12 9 3 15 2 7 12 is the sorted list 12 9 3 15 2 7 Take element 1 of unsorted list 12 9 3 15 2 7 Compare to first element in sorted list 12 9 3 15 2 7 First element is greater: so put it on left 9 12 3 15 2 7 Take next element or unordered list 9 12 3 15 2 7 Compare to first element in sorted list 9 12 3 15 2 7 First element is greater: so put it on left 3 9 12 15 2 7

23 Insertion Sort Take element 1 of unsorted list 3 9 12 15 2 7 Compare to first element in sorted list 3 9 12 15 2 7 First element is smaller: so move to next element in ordered list 3 9 12 15 2 7 First element is smaller: so move to next element in ordered list 3 9 12 15 2 7 No more elements left, so insert at the end of the ordered list 3 9 12 15 2 7

24 Insertion Sort Can you talk through the next steps? 3 9 12 15 2 7 3 9

25 Take element 1 of unsorted list
2 3 7 9 12 15 Compare to element 1 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 2 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 3 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 4 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 5 in sorted list. It is larger. 2 3 7 9 12 15 Compare to element 6 in sorted list. 2 3 7 9 12 15 It is the same, so place it to the right. 2 3 7 9 12 15

26 Task Use an Insertion Sort to sort the list: 3 19 2 44 56 7 12

27 Review/Success Criteria


Download ppt "Sorting Algorithms."

Similar presentations


Ads by Google