Intro to Computer Science CS1510 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
Advertisements

Quicksort Quicksort     29  9.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Algorithm An algorithm is a step-by-step set of operations to be performed. Real-life example: a recipe Computer science example: determining the mode.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
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.
Wednesday, 11/25/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/25/02  QUESTIONS??  Today:  More on sorting. Advanced sorting algorithms.  Complexity:
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Searching Arrays Linear search Binary search small arrays
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Computer Science Searching & Sorting.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
Order Statistics David Kauchak cs302 Spring 2012.
Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Centroids part 2 Getting rid of outliers and sorting.
Chapter 12 Binary Search and QuickSort Fundamentals of Java.
Sorts Tonga Institute of Higher Education. Introduction - 1 Sorting – The act of ordering data Often, we need to order data.  Example: Order a list of.
3.3 Complexity of Algorithms
COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting.
Searching Topics Sequential Search Binary Search.
Sorting Algorithms Written by J.J. Shepherd. Sorting Review For each one of these sorting problems we are assuming ascending order so smallest to largest.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 16: Searching, Sorting, and the vector Type.
Lists and Sorting Algorithms
Searching Arrays Linear search Binary search small arrays
Week 13: Searching and Sorting
CSCI 104 Sorting Algorithms
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Warmup What is an abstract class?
Lesson Objectives Aims Understand the following “standard algorithms”:
Sorting by Tammy Bailey
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Design and Analysis of Algorithms
Teach A level Computing: Algorithms and Data Structures
Divide and Conquer.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Sorting Algorithms Written by J.J. Shepherd.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithm design and Analysis
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Bubble Sort The basics of a popular sorting algorithm.
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Complexity Present sorting methods. Binary search. Other measures.
Binary Search and Intro to Sorting
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithmic Complexity
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.
Topic 24 sorting and searching arrays
Chapter 4.
Searching.
Applied Combinatorics, 4th Ed. Alan Tucker
Searching and Sorting Arrays
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Mod 3 Lesson 2 Me First! Sorting
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Presentation transcript:

Intro to Computer Science CS1510 Dr. Sarah Diesburg Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

Last Time We looked at two basic algorithms for searching Linear search Binary search Linear search was the easiest to write But perhaps not the best from a complexity standpoint

Last Time Big “O” measures how badly the problem grows as the data set grows Study of complexity of algorithms Worst case of linear search was N, where N is the number of comparisons that we need to perform Double the number of items in list, double the amount of time needed to complete the search in the worst case

Last Time The binary search was another solution that incurred less comparisons in the worst case Only works on sorted list

Binary Search Binary search algorithm Try the guess at middle index of the range If the value we are searching for is higher than number at the index, then adjust your low range bound to be your guess+1 If the value we are searching for is lower than number at the index, then adjust your high range bound to be your guess-1 Repeat

Binary Search What is the worst-case scenario of the binary search? Thinking of a number between 1 and 100 7 guesses in total – why? 1 guesses – cut down to 50 possibilities 2 guesses – cut down to 25 3 guesses – cut down to 12 4 guesses – cut down to 6 5 guesses – cut down to 3 6 guesses – cut down to 1 7 guesses – to figure out if last guess is right

Binary Search What is the complexity of a binary search? log2(100) = x Big O value of log2 N This is “log base 2” log2(100) = x What is this saying?

Binary Search What is the complexity of a binary search? log2(100) = x Big O value of log2 N This is “log base 2” log2(100) = x What is this saying? 2x = 100 Go “to the next power” when not exact

Binary Search How does that relate to our binary search? Let’s say there are 16 items in our list. What is the worst case number of guesses? 32? 34? 64? One million?

Binary Search How does that relate to our binary search? Let’s say there are 16 items in our list. What is the worst case number of guesses? 32? 34? 64? One million? One million is about 20 guesses 2^10 = 1024 One million is 1000 squared, so twice as much

Searching So which kind of search would amazon.com use to search their databases?

Searching So which kind of search would amazon.com use to search their databases? Binary search!!! The downside is that the list needs to be sorted first…

Group Time! Let’s get into 4 big groups Put the cards in order You can only look at two cards at a time

Sorting Methods Insertion Sort Two chunks of data (sorted and unsorted) Go through unsorted data and insert it in order into sorted pile As humans, if we could look at all cards at once, we would probably perform an insertion sort

Sorting Methods Bubble Sort Higher cards “bubble” to the top Compare two cards Move the higher card to the top Pick out another card Repeat Higher cards “bubble” to the top After each run, one more high card is in order Lower cards slowly “bubble” to the bottom

Sorting Methods Selection Sort Find smallest card by Comparing two cards at a time Saving out the current smallest card Repeat until reach end of pile Put smallest card in sorted pile Repeat

Sorting Methods Quicksort Choose a random card (called the “partition”) Compare every card against the partition Moving cards < partition to one side and cards > partition to the other Now, within each partition….over and over again… Compare every card against the partition, moving the cards on either side based on them being bigger or smaller Sort the small amount of cards within each partition using a simple sort method, like insertion or selection sort