Intro to Computer Science CS1510 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
CSE Lecture 3 – Algorithms I
Advertisements

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Visual C++ Programming: Concepts and Projects
SEARCHING, SORTING, TOPOLOGICAL SORTS Most real world computer applications deal with vast amounts of data. Searching for a particular data item can take.
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.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
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
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19: Searching and Sorting Algorithms
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Sorting List is rearranged into sorted order How is the sorted order determined? – The ItemType is responsible for determining the key to be used in comparison.
CSCI 51 Introduction to Programming March 12, 2009.
3.3 Complexity of Algorithms
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Searching Topics Sequential Search Binary Search.
Searching & Sorting. Algorithms Step by step recipe to do a task…
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.
Chapter 16: Searching, Sorting, and the vector Type.
CMSC 104, Version 8/061L24Searching&Sorting.ppt Searching and Sorting Topics Sequential Search on an Unordered File Sequential Search on an Ordered File.
Searching Arrays Linear search Binary search small arrays
Growth of Functions & Algorithms
Week 9 - Monday CS 113.
Week 13: Searching and Sorting
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Recursion
Warmup What is an abstract class?
Lesson Objectives Aims Understand the following “standard algorithms”:
Sorting by Tammy Bailey
Design and Analysis of Algorithms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Sorting Algorithms Written by J.J. Shepherd.
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.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Algorithm design and Analysis
Intro to Computer Science CS1510 Dr. Sarah Diesburg
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
Selection Sort Sorted Unsorted Swap
Searching and Sorting Topics Sequential Search on an Unordered File
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Binary Search and Intro to Sorting
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Searching and Sorting Topics Sequential Search on an Unordered File
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
UMBC CMSC 104 – Section 01, Fall 2016
Searching and Sorting Topics Sequential Search on an Unordered File
Algorithmic Complexity
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Searching and Sorting Arrays
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSCE 222 Discrete Structures for Computing
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Mod 3 Lesson 2 Me First! Sorting
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Module 8 – Searching & Sorting Algorithms
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?

Demo binarySearch() on different types of lists Ordered Odd Reverse

Demo binarySearch() on different types of lists Ordered Odd Reverse The reverse list doesn’t work because the list needs to be sorted in ascending order. How do we sort?

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 Humans will tend to want to fan out all the cards and scan them With 13 cards, this works But what if I gave you 10,000 student ID cards? Computers can only compare a finite number of cards together at a time Let’s start to think about how long each of these will take in the worst case

Big O (Worst Case) Selection sort First pass – compare 13 cards and set aside lowest Second pass – compare 12 cards and set aside lowest Etc…. How many passes do I make? – 13 N^2 = 169 but actually 91 As you double your data, you quadruple your time.