Sorting Algorithms 2 1 Algorithms Unit 2 Computational

  • Slides: 14
Download presentation
Sorting Algorithms 2. 1 – Algorithms

Sorting Algorithms 2. 1 – Algorithms

Unit 2: Computational Thinking, Algorithms & Programming 07 December 2020 Sorting Algorithms Learning Objective:

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

Unit 2: Computational Thinking, Algorithms & Programming Sorting Algorithms You need to know how

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

Unit 2: Computational Thinking, Algorithms & Programming Bubble Sort A bubble sort works by

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: Compare the first two numbers. Are they in order? Adjust them if not. 10 2 6 5 11 2 10 6 5 11 Step 2: Move to the next two numbers. Are they in order? Adjust them if not.

Unit 2: Computational Thinking, Algorithms & Programming Bubble Sort … Step 3: Move to

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 2 6 5 10 11 Step 4: 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.

Unit 2: Computational Thinking, Algorithms & Programming Bubble Sort: Activity A 15 9 22

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.

Unit 2: Computational Thinking, Algorithms & Programming Insertion Sort Algorithm The insertion sort works

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. Step 1: Compare the first two items. 9 > 2 so 2 moves position. 2 9 5 8 7

Unit 2: Computational Thinking, Algorithms & Programming Insertion Sort Algorithm Step 2: Insert 5

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. Step 3: Insert 8 into its correct position. 8 > 5 and < 9 so 8 moves position. 5 2 9 8 7 8 2 5 9 7

Unit 2: Computational Thinking, Algorithms & Programming Insertion Sort Algorithm Step 4: Insert 7

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. 2 5 8 9 Completed Insertion Sort: 2 5 7 8 7 9

Unit 2: Computational Thinking, Algorithms & Programming Insertion Sort: Activity A 4 2 8

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.

Unit 2: Computational Thinking, Algorithms & Programming Merge Sort Algorithm The merge sort algorithm

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. 1) Split the list in half (the smaller lists are called sub-lists) – the second sub-list should start at the middle item. 2) Keep repeating step 1 on each sub-list until all the lists only contain one item. 3) 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. 4) Repeat step 3 until you’ve merged all the sub -lists together.

Unit 2: Computational Thinking, Algorithms & Programming Merge Sort: Example F F F T

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

Unit 2: Computational Thinking, Algorithms & Programming Merge Sort Pros & Cons The pros

Unit 2: Computational Thinking, Algorithms & Programming Merge Sort Pros & Cons The pros and cons of merge sort algorithms are as follows: Pros • 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. Cons • 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.

Unit 2: Computational Thinking, Algorithms & Programming Merge Sort: Activity Task: Sort these items

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