CMSC201 Computer Science I for Majors Lecture 23 – Sorting

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Analysis And Algorithms CMSC 201. Search Sometimes, we use the location of a piece of information in a list to store information. If I have the list [4,
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
CHAPTER 11 Sorting.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Topic 9B – Array Sorting and Searching
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Adapted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All.
Centroids part 2 Getting rid of outliers and sorting.
Chapter 12 Binary Search and QuickSort Fundamentals of Java.
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
3 – SIMPLE SORTING ALGORITHMS
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Parallel Sorting Algorithm. 2 Bitonic Sequence A bitonic sequence is defined as a list with no more than one LOCAL MAXIMUM and no more than one LOCAL.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Lists and Sorting Algorithms
CPS120: Introduction to Computer Science Sorting.
CMSC201 Computer Science I for Majors Lecture 23 – Algorithms and Analysis Prof. Katherine Gibson Based on slides from previous iterations.
Searching and Sorting Algorithms
Sorting Mr. Jacobs.
Last Class We Covered Sorting algorithms Searching algorithms
Last Class We Covered Sorting algorithms “Run” time Selection Sort
CMSC201 Computer Science I for Majors Lecture 22 – Searching
Recitation 13 Searching and Sorting.
Warmup What is an abstract class?
Lesson Objectives Aims Understand the following “standard algorithms”:
Quicksort
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Teach A level Computing: Algorithms and Data Structures
Quicksort 1.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Data Structures and Analysis (COMP 410)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Last Class We Covered Data representation Binary numbers ASCII values
Data Structures and Organization (p.2 – Arrays)
Selection Sort Sorted Unsorted Swap
Shuttle Sort Example 1st pass Comparisons: 1
Last Class We Covered Sorting algorithms Searching algorithms
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
8/04/2009 Many thanks to David Sun for some of the included slides!
IT 4043 Data Structures and Algorithms
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Chapter 4.
RECURSION Haskell.
Searching and Sorting Arrays
Quicksort.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Last Class We Covered Recursion Stacks Parts of a recursive function:
Quicksort.
Unit 2: Computational Thinking, Algorithms & Programming
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Last Class We Covered Sorting algorithms Searching algorithms
Quicksort.
Presentation transcript:

CMSC201 Computer Science I for Majors Lecture 23 – Sorting

Last Class We Covered Searching Linear search Binary search Recursion

Any Questions from Last Time?

Today’s Objectives To learn about some sorting algorithms Selection Sort Bubble Sort Quicksort To start examining which of these algorithms is best for different sorting situations “Run” time

“Run” Time

“Run” Time An algorithm’s run time is the amount of time it takes for that algorithm to run Run time is shown as an expression, which updates based on how large the problem is Run time shows how an algorithm scales, or changes with the size of the problem

Example: Fibonacci Recursion Ideally, we want an algorithm that runs in a reasonable amount of time, no matter how large the problem Remember the recursive Fibonacci program? It runs within one second for smaller numbers But the larger the number we ask for, the longer and longer it takes

Fibonacci Recursion python fibEx.py (with num < 30): < 1 second 2 seconds python fibEx.py (with num = 35): 8 seconds python fibEx.py (with num = 40): 76 seconds

Fibonacci Recursion python fibEx.py (with num = 50): Guess! 9,493 seconds 2 hours, 38 minutes, 13 seconds!!!

Run Time for Linear Search Say we have a list that does not contain what we’re looking for. How many things in the list does linear search have to look at for it to figure out the item’s not there for a list of 8 things? 16 things? 32 things?

Run Time for Binary Search Say we have a list that does not contain what we’re looking for. What about for binary search? How many things does it have to look at to figure out the item’s not there for a list of 8 things? 16 things? 32 things? Notice anything different?

Different Run Times These algorithms scale differently! Linear search does work equal to the number of items in the list Binary search does work equal to the log2 of the numbers in the list! A log2(x) is basically asking “2 to what power equals x?” This is the same as saying, “how many times must we divide x in half before we hit 1?”

Sorting

Sorting Algorithms Sorting algorithms put the elements of a list in a specific order A sorted list is necessary to be able to use certain other algorithms Like binary search! If sorted once, we can search many, many times

Sorting Algorithms There are many different ways to sort a list What method would you use? Now imagine you can only look at at most two elements at a time What method would you use now? Computer science has a number of commonly used sorting algorithms

Selection Sort

Selection Sort Algorithm Here is a simple way of sorting a list: Find the smallest number in a list Move that to the end of a new list Repeat until the original list is empty

Selection Sort Run Time What is the run time of finding the lowest number in a list? For a list of size N, what is the worst case number of elements you’d have to look through to find the min? N

Selection Sort Run Time For a list of size N, how many times would we have to find the min to sort the list? N What is the run time of this sorting algorithm? N2

Selection Sort Video Video from https://www.youtube.com/watch?v=Ns4TPTC8whw

Bubble Sort

Bubble Sort Algorithm Let’s take a look at another sorting method! We look at the first pair of items in the list, and if the first one is bigger than the second one, we swap them Then we look at the second and third one and put them in order, and so on Once we hit the end of the list, we start over at the beginning Repeat until the list is sorted!

Bubble Sort Example [ 4, 8, 1, 10, 13, 14, 6] First pass: 4 and 8 are in order 8 and 1 should be swapped: [ 4, 1, 8, 10, 13, 14, 6] 8 and 10 are in order 10 and 13 are in order 13 and 14 are in order 6 and 14 should be swapped: [ 4, 1, 8, 10, 13, 6, 14]

Bubble Sort Example (Cont) [ 4, 1, 8, 10, 13, 6, 14] Second pass: 4 and 1 should be swapped: [ 1, 4, 8, 10, 13, 6, 14] 4 and 8 are in order 8 and 10 are in order 10 and 13 are in order 13 and 6 should be swapped: [ 1, 4, 8, 10, 6, 13, 14] 13 and 14 are in order

Bubble Sort Example (Cont) [ 1, 4, 8, 10, 6, 13, 14] Third pass: 10 and 6 should be swapped: [ 1, 4, 8, 6, 10, 13, 14] Fourth pass: 8 and 6 should be swapped: [ 1, 4, 6, 8, 10, 13, 14]

Bubble Sort Run Time For a list of size N, how much work do we do for a single pass? N How many passes will we have to do? What is the run time of Bubble Sort? N2

Bubble Sort Video Video from https://www.youtube.com/watch?v=lyZQPjUT5B4

Quicksort

Quicksort Algorithm Here’s another method: Start with the number on the far right Put everything less than that number on the left of it and everything greater than it on the right of it Quicksort the left side and the right side Does this method remind you of anything?

Quicksort Run Time For a list of size N, how many steps does it take to move everything less than the last number to the left and everything greater than the last number to the right? N

Quicksort Run Time How many times will the algorithm divide the list in half? lg(N) What is the run time of Quicksort? N * lg(N)

Quicksort Video Video from https://www.youtube.com/watch?v=ywWBy6J5gz8

Thursday, December 15th (3:30 – 5:30) Announcements Final is when? “Hour of code” volunteer opportunity http://www.signupgenius.com/go/10c0f45aaab2aabfc1-hour Project 2 out now Due on Tuesday, December 13th Survey #3 also out – follow link in announcement Thursday, December 15th (3:30 – 5:30)