Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Algorithms 2.1 – Algorithms.

Similar presentations


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

1 Sorting Algorithms 2.1 – Algorithms

2 Thursday, 09 May 2019 Sorting Algorithms
Unit 2: Computational Thinking, Algorithms & Programming Thursday, 09 May 2019 Sorting Algorithms Learning Objective: To be able to demonstrate an understanding of standard sorting algorithms. Success Criteria: I can describe how items are sorted using a bubble sort algorithm. I can write a bubble sort algorithm. I can describe how items are sorted using an insertion sort algorithm. I can write an insertion sort algorithm.

3 Unit 2: Computational Thinking, Algorithms & Programming
Sorting Algorithms You need to know how to complete the following sort algorithms: Bubble sort Insertion sort Merge sort

4 Unit 2: Computational Thinking, Algorithms & Programming
Bubble Sort A bubble sort works by comparing each item in the list to the item to the right of it. The item is then moved based upon whether it is bigger/smaller. Step 1: 10 2 6 5 11 Compare the first two numbers. Are they in order? Adjust them if not. Step 2: 2 10 6 5 11 Move to the next two numbers. Are they in order? Adjust them if not.

5 Unit 2: Computational Thinking, Algorithms & Programming
Bubble Sort … Step 3: Move to the next two numbers. Are they in order? Adjust them if not. 2 6 10 5 11 Step 4: 2 6 5 10 11 Move to the next two numbers. Are they in order? Adjust them if not Step 5: Once you have been through the entire list, check that they are in order. If they are, great! If not, undertake the same process again until they are in order.

6 Unit 2: Computational Thinking, Algorithms & Programming
Bubble Sort: Activity A 15 9 22 27 14 B 44 30 16 51 27 C Pear Banana Apple Blueberry Pineapple D Rain Lolly Freedom Aeroplane Zebra Task: Sort the above using the bubble sort algorithm. Show your steps. Show the order of the list after each adjustment.

7 Insertion Sort Algorithm
Unit 2: Computational Thinking, Algorithms & Programming Insertion Sort Algorithm The insertion sort works by looking at each value in turn and inserting the value into its correct place in the list. 2 Step 1: Compare the first two items. 9 > 2 so 2 moves position. 9 5 8 7

8 Insertion Sort Algorithm
Unit 2: Computational Thinking, Algorithms & Programming Insertion Sort Algorithm Step 2: Insert 5 into its correct position. 5 > 2 and 5 < 8 so 5 moves position. 5 2 9 8 7 8 Step 3: Insert 8 into its correct position. 8 > 5 and < 9 so 8 moves position. 2 5 9 7

9 Insertion Sort Algorithm
Unit 2: Computational Thinking, Algorithms & Programming Insertion Sort Algorithm Step 4: Insert 7 into its correct position. 7 > 5 and 7 < 8 so 7 moves position. 7 2 5 8 9 2 5 7 8 9 Completed Insertion Sort:

10 Insertion Sort: Activity
Unit 2: Computational Thinking, Algorithms & Programming Insertion Sort: Activity A 4 2 8 9 7 B 19 5 8 7 15 C 40 9 32 27 20 Task: Sort the above using the Insertion sort algorithm. Show your steps. Show the order of the list after each adjustment.

11 Unit 2: Computational Thinking, Algorithms & Programming
Merge Sort Algorithm The merge sort algorithm is an example of a divide-and-conquer algorithm and takes advantage of two facts: Small lists are easier to sort than large lists. It’s easier to merge two-ordered lists than two unordered lists. Split the list in half (the smaller lists are called sub-lists) – the second sub-list should start at the middle item. Keep repeating step 1 on each sub-list until all the lists only contain one item. Merge pairs of sub-lists so that each sub-list has twice as many items. Each time you merge sub-lists, sort the items into the right order. Repeat step 3 until you’ve merged all the sub-lists together.

12 Unit 2: Computational Thinking, Algorithms & Programming
Merge Sort: Example F P A L T D K H F P A L T D K H F P A L T D K H F P A L T D K H F P A L D T H K A F L P D H K T A D F H K L P T

13 Unit 2: Computational Thinking, Algorithms & Programming
Merge Sort Pros & Cons The pros and cons of merge sort algorithms are as follows: Pros Cons In general, it is much more efficient and quicker than the bubble sort and insertion sort algorithms for large lists. It has a very consistent running time regardless of how ordered the items in the original list are. It’s slower than other algorithms for small lists. Even if the list is already sorted it still goes through the whole splitting and merging process. It uses more memory than the other sorting algorithms in order to create the separate lists.

14 Unit 2: Computational Thinking, Algorithms & Programming
Merge Sort: Activity Task: Sort these items using the merge sort method. S A P H J Z E R 19 5 29 1 87 14 61 33 Goat Dog Bird Turtle Horse Snake Lion Bear


Download ppt "Sorting Algorithms 2.1 – Algorithms."

Similar presentations


Ads by Google