Topic 1: Problem Solving

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Garfield AP Computer Science
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Recursion, Complexity, and Sorting By Andrew Zeng.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
1 Lecture 16: Lists and vectors Binary search, Sorting.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
Chapter 4, Part II Sorting Algorithms. 2 Heap Details A heap is a tree structure where for each subtree the value stored at the root is larger than all.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Searching and Sorting Searching algorithms with simple arrays
Merge Sort.
3.3 Fundamentals of data representation
Lecture 25: Searching and Sorting
COP 3503 FALL 2012 Shayan Javed Lecture 16
Searching Given a collection and an element (key) to find… Output
Lecture 14 Searching and Sorting Richard Gesick.
Sorting.
Recitation 13 Searching and Sorting.
Section 10.3a Merge Sort.
Sorting Algorithms.
Simple Sorting Algorithms
David Kauchak cs201 Spring 2014
Algorithm Analysis CSE 2011 Winter September 2018.
Divide and Conquer.
slides created by Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Algorithm design and Analysis
Building Java Programs
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
CS Two Basic Sorting Algorithms Review Exchange Sorting Merge Sorting
Sort Techniques.
Sorting Algorithms Ellysa N. Kosinaya.
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
CSE 154 Sorting reading: 13.3, 13.4
Standard Version of Starting Out with C++, 4th Edition
Adapted from slides by Marty Stepp and Stuart Reges
Topic 1: Problem Solving
Search,Sort,Recursion.
slides created by Marty Stepp
Yan Shi CS/SE 2630 Lecture Notes
Lecture 16 Bubble Sort Merge Sort.
slides created by Marty Stepp and Ethan Apter
slides created by Marty Stepp
Basics of Recursion Programming with Recursion
Searching.
slides created by Marty Stepp
CSE 143 Sorting reading: 13.3, 13.4.
Search,Sort,Recursion.
slides created by Marty Stepp and Hélène Martin
slides adapted from Marty Stepp
Module 8 – Searching & Sorting Algorithms
Stacks, Queues, ListNodes
Presentation transcript:

Topic 1: Problem Solving Sorting Algorithms

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

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

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

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

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

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

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

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

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

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

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