Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 1: Problem Solving

Similar presentations


Presentation on theme: "Topic 1: Problem Solving"— Presentation transcript:

1 Topic 1: Problem Solving
Sorting Algorithms

2 Problem Solving: Sorting Algorithms
Algorithms themselves can cover a wide range of problems How do we get a number from a user? How do we calculate the distance between two points? How do we simulate a game of chess? Sometimes one particular problem can have a wide range of solutions So much so, in fact, that they become a topic of research and learning Problem Solving: Sorting Algorithms

3 Problem Solving: Sorting Algorithms
We’re looking at two of these topics Sorting Algorithms Searching Algorithms This presentation is aimed at the former Both topics include many possible algorithms we can use Each working in a different way And each taking different amounts of time to complete Problem Solving: Sorting Algorithms

4 Sorting Algorithms Sorting algorithms are aimed exclusively at one problem How do we order this collection of values? These values can be of any data-type that is ‘orderable’ Numbers and text, for example Each specific algorithm in this topic will end up with the same result, but will achieve that in different ways A short animation on some different sorting algorithms Problem Solving: Sorting Algorithms

5 Problem Solving: Sorting Algorithms
We are going to examine two different sorting algorithms in this presentation Bubble Sort Merge Sort Bubble Sort is considered the simpler of the two, but also the slower Merge Sort has some complexities to it, but solves the problem faster than Bubble Sort Problem Solving: Sorting Algorithms

6 Problem Solving: Sorting Algorithms
Bubble Sort The Bubble Sort algorithm works by looking at all the values in a collection in pairs If the smaller value in the pair is first, it swaps the values around It moves along the whole collection, one pair at a time Once it gets to the end, it starts from the beginning and does another sweep It stops sweeping when it doesn’t make any swaps Problem Solving: Sorting Algorithms

7 Problem Solving: Sorting Algorithms
Bubble Sort Here is the algorithm in pseudocode BEGIN BubbleSort(nums) sorted  False WHILE NOT sorted sorted  True FOR index FROM 0 TO SIZE(nums) – 2 IF nums[index] > nums[index + 1] temp  nums[index] nums[index]  nums[index + 1] nums[index + 1]  temp END IF END FOR END WHILE END BubbleSort Problem Solving: Sorting Algorithms

8 Problem Solving: Sorting Algorithms
Have a go at implementing the Bubble Sort algorithm To test, you will need to make an array of numbers Use this array for testing: [1, 5, 3, 9, 7, 8, 2, 4, 6, 0] Output the array after it has been sorted BEGIN BubbleSort(nums) sorted  False WHILE NOT sorted sorted  True FOR index FROM 0 TO SIZE(nums) – 2 IF nums[index] > nums[index + 1] temp  nums[index] nums[index]  nums[index + 1] nums[index + 1]  temp END IF END FOR END WHILE END BubbleSort Problem Solving: Sorting Algorithms

9 Merge Sort The Merge Sort algorithm takes a different approach to sorting the values Rather than look through the values in pairs, it continuously splits the collection in half It does this until there are only two values left Once split down all the way, it builds it back up Sorting the values as it does so Click on this shape to go to a good sorting visualiser online Problem Solving: Sorting Algorithms

10 Problem Solving: Sorting Algorithms
Merge Sort This algorithm is split into two major components Splitting the collection into two parts Merging the parts together in the correct order The easiest is splitting the collection into two This function acts recursively We start by passing in the whole collection It then runs itself, passing in the left half Which then passes in the left-left-half And so on… BEGIN MergeSort(nums) IF SIZE(nums) = 1 RETURN nums END IF left  nums[0:SIZE(nums)/2] right  nums[SIZE(nums)/2:SIZE(nums)] MergeSort(left) MergeSort(right) RETURN Combine(left, right) END MergeSort Problem Solving: Sorting Algorithms

11 Problem Solving: Sorting Algorithms
Merge Sort The combination is the trickiest part When recombining the left and right parts, we need to go through them one-by-one If the current left value is smaller than the current right value, we put it first Then move on to the next left value Otherwise we do the reverse When we run out of lefts or rights, we add the rest of the other BEGIN Combine(left, right) lcounter  0 rcounter  0 nums  ARRAY(SIZE(left) + SIZE(right)) WHILE lcounter < SIZE(left) AND rcounter < SIZE(right) IF left[lcounter] < right[rcounter] nums[lcounter + rcounter]  left[lcounter] lcounter  lcounter + 1 ELSE nums[lcounter + rcounter]  right[rcounter] rcounter  rcounter + 1 END IF END WHILE WHILE lcounter < SIZE(left) WHILE rcounter < SIZE(right) END Combine Problem Solving: Sorting Algorithms

12 Problem Solving: Sorting Algorithms
Use these two pieces of pseudocode to implement the Merge Sort algorithm Use the same array as before for testing BEGIN Combine(left, right) lcounter  0 rcounter  0 nums  ARRAY(SIZE(left) + SIZE(right)) WHILE lcounter < SIZE(left) AND rcounter < SIZE(right) IF left[lcounter] < right[rcounter] nums[lcounter + rcounter]  left[lcounter] lcounter  lcounter + 1 ELSE nums[lcounter + rcounter]  right[rcounter] rcounter  rcounter + 1 END IF END WHILE WHILE lcounter < SIZE(left) WHILE rcounter < SIZE(right) END Combine BEGIN MergeSort(nums) IF SIZE(nums) = 1 RETURN nums END IF left  nums[0:SIZE(nums)/2] right  nums[SIZE(nums)/2:SIZE(nums)] MergeSort(left) MergeSort(right) RETURN Combine(left, right) END MergeSort Problem Solving: Sorting Algorithms

13


Download ppt "Topic 1: Problem Solving"

Similar presentations


Ads by Google