Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

Size: px
Start display at page:

Download "Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I"

Transcription

1 Class #21: Searching/Sorting I Software Design II (CS 220): M. Allen, 26 Feb. 18 Searching for Information Many applications involve finding pieces of information Finding a book in a library or store catalogue Finding movie show-times and nearby locations online Finding a path through a maze Finding the best possible move in a game of chess Finding the shortest drive from La Crosse to Milwaukee Finding a flight from La Crosse to Los Angeles, leaving after 9:00 AM and costing less than $500 return-trip Monday, 26 Feb Software Design II (CS 220) 2 Simple Searching For complex, constrained searches (like shortest paths, or lowest fares), deciding how to find the information we want can be very challenging For simpler searches (like finding a book in a catalogue or a client in an address-book), things are often easier One key factor is the data structures we use How is data stored? How can we access individual pieces of data? Example: How can we find a single piece of data in an array? A Simple Method for Searching Consider how to explain search process in simple step-by-step English 1. Start at the beginning of the list 2. Check if it is what you are looking for [0] [1] [2]... [l-3] [l-2] [l-1] a. If it is, then you found it! Report it & stop. 3b. If it is not, and you are not at the end of list, repeat process on the next entry. 4. If you get to the end of the list, and haven t found it, it isn t there. Report & stop. Monday, 26 Feb Software Design II (CS 220) 3 Monday, 26 Feb Software Design II (CS 220) 4 1

2 Isolating the Logical Structure Take your English translation and try to isolate the basic moving parts 1. Start at the beginning of the list 2. Check if it is what you are looking for 3a. If it is, then you found it! Report it & stop. 3b. If it is not, and you are not at the end of list, repeat process on the next entry. 4. If you get to the end of the list, and haven t found it, it isn t there. Report & stop. : a loop : if/else Output: return statement Monday, 26 Feb Software Design II (CS 220) 5 Translating into Java Once you have the moving parts, you can translate into code; private int simplesearch( int target ) for ( int i = 0; i < array.length; i++ ) if ( array[i] == target ) return i; Output Monday, 26 Feb Software Design II (CS 220) 6 Why Return -1? Found Not found private int simplesearch( int target ) for ( int i = 0; i < array.length; i++ ) if (array[i] == target ) return i; If the method finds the target value, returns its array position Otherwise, returns signal value (-1), allowing us to tell that target wasn t found int index = simplesearch( 50 ); if ( index < 0 ) System.out.println( "Not found!" ); else System.out.println( "Found at: " + index ); Monday, 26 Feb Software Design II (CS 220) 7 Exploiting Data Structure With a random list of numbers, the simple search procedure might be the best we could do, since the number could be anywhere in the list If we know something about the data, we can often do something more intelligent How can you find if a word is in a regular book? What if that book is in fact a dictionary? How can you find a number in a sorted array? Monday, 26 Feb Software Design II (CS 220) 8 2

3 Binary Searching A more complex search process is possible with sorted data: [0] [1] [2]... [mid]... [l-3] [l-2] [l-1] Repeat as needed 4. Repeat as needed 1. Start in the middle position 3a. If middle value is too big, look in lower half of list 2. If it is not what you want, then either: 3a. If middle value is too small, look in top half of list Monday, 26 Feb Software Design II (CS 220) 9 Translating Binary Search into Java Too small: search top half of array Too big: search the bottom half private int binsearch( int target ) int start = 0; int stop = array.length - 1; while ( start <= stop ) int mid = ( start + stop ) / 2; if ( array[mid] == target ) return mid; else if ( array[mid] < target ) start = mid + 1; else if ( array[mid] > target ) stop = mid - 1; Find middle Target found (done)! Monday, 26 Feb Software Design II (CS 220) 10 Translating Binary Search into Java (B) We loop until the starting point crosses over and becomes larger than end point. (D) To search in bottom half of current range, we move the stopping point downwards. private int binsearch( int target ) int start = 0; int stop = array.length - 1; while ( start <= stop ) int mid = ( start + stop ) / 2; if ( array[mid] == target ) return mid; else if ( array[mid] < target ) start = mid + 1; else if ( array[mid] > target ) stop = mid - 1; (A) Initially, we set the limits of our search from front of array all the way to end. (C) To search in top half of current range, we move the starting point upwards. Monday, 26 Feb Software Design II (CS 220) 11 Simple Search vs. Binary Search: What s the Difference? Binary search is more complicated than simple search This complexity can pay for itself, however, when dealing with very large data sets Worst-case performance: how many elements, at most, will each algorithm look at before it finishes? Worst-case: Item not found at all Simple search: looks at all members of array Binary search: divides the data in half each time, which gives us a maximum of log 2 N, where N is the array size Monday, 26 Feb Software Design II (CS 220) 12 3

4 A Concrete Example Consider a petabyte of data: A quadrillion bytes = Circa 2014, Google processed about 100 petabytes per day Suppose it takes a nanosecond (billionth of a second) to consider each value in a petabyte-sized array Sorting an Array Consider an array of integers in some random order Binary search will not work on such an array Want to sort in ascending order (lowest highest) first: Simple Search / 10 9 = 10 6 sec days Binary Search log = 50 nsec. 50 billionth of a sec Monday, 26 Feb Software Design II (CS 220) 13 Monday, 26 Feb Software Design II (CS 220) 14 One Way to Sort an Array One possibility: progressively swap array entries Start with the number in 2 nd place, and check whether it is smaller or greater than the one in 1 st place: Since it is smaller, swap them, and move on to 3 rd place Continuing the Process Consider what happens with the final entry (0) in array After we swap, it still isn t in the right place yet Must continue process, swapping until in its final position Monday, 26 Feb Software Design II (CS 220) 15 Monday, 26 Feb Software Design II (CS 220) 16 4

5 Logical Structure of a Simple Bubble Sort Translating the Logical Structure 1. Start at the beginning of list 2. Check if first & second elements are in correct order 3a. If not, then swap the elements. 3b. If necessary, repeat process in reverse order: keep swapping until there are no more swaps to do and smallest element is now in correct position 4. Repeat until end of list. Swap More! (nested loop) More! private void bubblesort( int[] list ) for ( int i = 1; i < list.length; i++ ) for ( int j = i; j > 0; j-- ) if ( list[j-1] > list[j] ) int temp = list[j]; list[j] = list[j-1]; list[j-1] = temp; Swap Monday, 26 Feb Software Design II (CS 220) 17 Monday, 26 Feb Software Design II (CS 220) 18 This Week & Next Meetings this week: Monday Tuesday: regular classroom Thursday Friday: in the CS Lab (16 Wing) Midterm 01: next Monday, 05 March Practice exam up on D2L, answer key Thursday morning Covers everything up to last week (recursion, lectures 1 10) Office Hours: Wing 210 Monday/Wednesday/Friday, 10:00 AM 11:00 AM Tuesday/Thursday, 1:30 PM 3:00 PM Monday, 26 Feb Software Design II (CS 220) 19 5

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2. Class #10: Understanding Primitives and Assignments Software Design I (CS 120): M. Allen, 19 Sep. 18 Java Arithmetic } Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = 2 + 5 / 2; 3.

More information

ECE 122. Engineering Problem Solving Using Java

ECE 122. Engineering Problem Solving Using Java ECE 122 Engineering Problem Solving Using Java Lecture 27 Linear and Binary Search Overview Problem: How can I efficiently locate data within a data structure Searching for data is a fundamental function

More information

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types. Class #07: Java Primitives Software Design I (CS 120): M. Allen, 13 Sep. 2018 Two Types of Types So far, we have mainly been dealing with objects, like DrawingGizmo, Window, Triangle, that are: 1. Specified

More information

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object Review: Diagrams for Inheritance - String makemodel - int mileage + (String, int) Class #3: Inheritance & Polymorphism Software Design II (CS 220): M. Allen, 25 Jan. 18 + (String, int) + void

More information

Basic Class Diagrams. Class Diagrams, cont d. Class Diagrams, cont d. Car. Car. Car. What does the minus sign here mean?

Basic Class Diagrams. Class Diagrams, cont d. Class Diagrams, cont d. Car. Car. Car. What does the minus sign here mean? Class #02: Inheritance and Object-Oriented Design Software Design II (CS 220): M. Allen, 23 Jan. 18 Basic Class Diagrams Describes a class and how it can be used properly Sketch of properties and behaviors

More information

CS 163 Practice Final Exam Winter 2012

CS 163 Practice Final Exam Winter 2012 CS 163 Practice Final Exam Winter 2012 The final exam is Saturday, 21 April. Any problem from either midterm or either practice midterm may (and often does) appear again on the final. In addition, make

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 06 / 11 / 2015 Instructor: Michael Eckmann Today s Topics Comments and/or Questions? Sorting Searching Michael Eckmann - Skidmore College - CS 106 - Summer 2015

More information

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray();

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray(); Converting Collections to Arrays Every Java collection can be converted to an array This is part of the basic Collection interface The most elementary form of this method produces an array of base-type

More information

Review: Using Imported Code. What About the DrawingGizmo? Review: Classes and Object Instances. DrawingGizmo pencil; pencil = new DrawingGizmo();

Review: Using Imported Code. What About the DrawingGizmo? Review: Classes and Object Instances. DrawingGizmo pencil; pencil = new DrawingGizmo(); Review: Using Imported Code Class #06: Objects, Memory, & Program Traces Software Engineering I (CS 120): M. Allen, 30 Jan. 2018 ; = new ();.setbackground( java.awt.color.blue );.setforeground( java.awt.color.yellow

More information

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation Week 12 - Friday What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation Here is some code that sorts an array in ascending order

More information

LECTURE 17. Array Searching and Sorting

LECTURE 17. Array Searching and Sorting LECTURE 17 Array Searching and Sorting ARRAY SEARCHING AND SORTING Today we ll be covering some of the more common ways for searching through an array to find an item, as well as some common ways to sort

More information

Today. CISC101 Reminders & Notes. Searching in Python - Cont. Searching in Python. From last time

Today. CISC101 Reminders & Notes. Searching in Python - Cont. Searching in Python. From last time CISC101 Reminders & Notes Test 3 this week in tutorial USATs at the beginning of next lecture Please attend and fill out an evaluation School of Computing First Year Information Session Thursday, March

More information

CS 177 Week 15 Recitation Slides. Review

CS 177 Week 15 Recitation Slides. Review CS 177 Week 15 Recitation Slides Review 1 Announcements Final Exam on Friday Dec. 18 th STEW 183 from 1 3 PM Complete your online review of your classes. Your opinion matters!!! Project 6 due Just kidding

More information

Week 12: Running Time and Performance

Week 12: Running Time and Performance Week 12: Running Time and Performance 1 Most of the problems you have written in this class run in a few seconds or less Some kinds of programs can take much longer: Chess algorithms (Deep Blue) Routing

More information

CS 12 Fall 2003 Solutions for mid-term exam #2

CS 12 Fall 2003 Solutions for mid-term exam #2 CS 12 Fall 2003 Solutions for mid-term exam #2 1. (10 points) Compilers and interpreters Provide short answers (a few sentences at most) to the following questions. (a) What is the difference between a

More information

Search,Sort,Recursion

Search,Sort,Recursion Search,Sort,Recursion Searching, Sorting and Recursion Searching Linear Search Inserting into an Array Deleting from an Array Selection Sort Bubble Sort Binary Search Recursive Binary Search Searching

More information

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

More on Arrays CS 16: Solving Problems with Computers I Lecture #13 More on Arrays CS 16: Solving Problems with Computers I Lecture #13 Ziad Matni Dept. of Computer Science, UCSB Announcements Homework #12 due today No homework assigned today!! Lab #7 is due on Monday,

More information

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms Sorting Savitch Chapter. Why sort Easier to search (binary search) Sorting used as a step in many algorithms Sorting algorithms There are many algorithms for sorting: Selection sort Insertion sort Bubble

More information

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms CSE 373: Data Structures and Algorithms Lecture 19: Comparison Sorting Algorithms Instructor: Lilian de Greef Quarter: Summer 2017 Today Intro to sorting Comparison sorting Insertion Sort Selection Sort

More information

To become familiar with array manipulation, searching, and sorting.

To become familiar with array manipulation, searching, and sorting. ELECTRICAL AND COMPUTER ENGINEERING 06-88-211: COMPUTER AIDED ANALYSIS LABORATORY EXPERIMENT #2: INTRODUCTION TO ARRAYS SID: OBJECTIVE: SECTIONS: Total Mark (out of 20): To become familiar with array manipulation,

More information

} Each object in a Java program has an identifier (name) } This includes:

} Each object in a Java program has an identifier (name) } This includes: Class #05: More about Objects and Methods Software Design I (CS 120): M. Allen, 11 Sept. 2018 Important Java Syntax I: Identifiers Each object in a Java program has an identifier (name) This includes:

More information

COMP1511 focuses on writing programs. Effciency is also important. Often need to consider:

COMP1511 focuses on writing programs. Effciency is also important. Often need to consider: Efficiency COMP1511 focuses on writing programs. Effciency is also important. Often need to consider: execution time memory use. A correct but slow program can be useless. Efficiency often depends on the

More information

Binary Search and Worst-Case Analysis

Binary Search and Worst-Case Analysis Department of Computer Science and Engineering Chinese University of Hong Kong A significant part of computer science is devoted to understanding the power of the RAM model in solving specific problems.

More information

Sorting and Searching. Sudeshna Sarkar 7 th Feb 2017

Sorting and Searching. Sudeshna Sarkar 7 th Feb 2017 Sorting and Searching Sudeshna Sarkar 7 th Feb 2017 Searching an Array: Linear and Binary Search Spring 2012 Programming and Data Structure 2 Searching Check if a given element (key) occurs in the array.

More information

Intro to Algorithms. Professor Kevin Gold

Intro to Algorithms. Professor Kevin Gold Intro to Algorithms Professor Kevin Gold What is an Algorithm? An algorithm is a procedure for producing outputs from inputs. A chocolate chip cookie recipe technically qualifies. An algorithm taught in

More information

Sorting. Weiss chapter , 8.6

Sorting. Weiss chapter , 8.6 Sorting Weiss chapter 8.1 8.3, 8.6 Sorting 5 3 9 2 8 7 3 2 1 4 1 2 2 3 3 4 5 7 8 9 Very many different sorting algorithms (bubblesort, insertion sort, selection sort, quicksort, heapsort, mergesort, shell

More information

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE Looking for something COMP 10 EXPLORING COMPUTER SCIENCE Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE Searching algorithms Linear search Complexity Sorting algorithms

More information

Lecture 15: Algorithms. AP Computer Science Principles

Lecture 15: Algorithms. AP Computer Science Principles Lecture 15: Algorithms AP Computer Science Principles Algorithm algorithm: precise sequence of instructions to solve a computational problem. Search for a name in a phone s contact list. Sort emails by

More information

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place? Sorting Binary search works great, but how do we create a sorted array in the first place? Sorting in Arrays Sorting algorithms: Selection sort: O(n 2 ) time Merge sort: O(nlog 2 (n)) time Quicksort: O(n

More information

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to: Objectives After studying this chapter, students should be able to: Chapter 8 Algorithms Define an algorithm and relate it to problem solving. Define three construct and describe their use in algorithms.

More information

Topic 14 Searching and Simple Sorts

Topic 14 Searching and Simple Sorts Topic 14 Searching and Simple Sorts "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 and the Sorcerer's Stone

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today Recursive Sorting Methods and their Complexity: Mergesort Conclusions on sorting algorithms and complexity Next Time:

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Searching and Sorting

Computer Science 210 Data Structures Siena College Fall Topic Notes: Searching and Sorting Computer Science 10 Data Structures Siena College Fall 016 Topic Notes: Searching and Sorting Searching We all know what searching is looking for something. In a computer program, the search could be:

More information

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017 CS 137 Part 8 Merge Sort, Quick Sort, Binary Search November 20th, 2017 This Week We re going to see two more complicated sorting algorithms that will be our first introduction to O(n log n) sorting algorithms.

More information

Lecture 7: Searching and Sorting Algorithms

Lecture 7: Searching and Sorting Algorithms Reading materials Dale, Joyce, Weems:Dale, Joyce, Weems: 10.1-10.5 OpenDSA: 11 (Sorting) and 13 (Searching) Liang (10): 7 (for searching and quadratic sorts), 25 (comprehensive edition only) Contents 1

More information

COP 3337 Test 3. private int minimumposition(int from) { Part 1: Selection Sort

COP 3337 Test 3. private int minimumposition(int from) { Part 1: Selection Sort COP 3337 Test 3 NAME Part 1: Selection Sort The SelectionSorter class implements a selection sort on an array of strings. It is missing the minimumposition() method that returns the index position of the

More information

LINEAR INSERTION SORT

LINEAR INSERTION SORT LINEAR INSERTION SORT Textbook (pages 95 03) The term insertion sort refers to an entire category of sorting algorithms which work by successively taking each item in the array and finding its proper position.

More information

C/C++ Programming Lecture 18 Name:

C/C++ Programming Lecture 18 Name: . The following is the textbook's code for a linear search on an unsorted array. //***************************************************************** // The searchlist function performs a linear search

More information

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58 08 A: Sorting III CS1102S: Data Structures and Algorithms Martin Henz March 10, 2010 Generated on Tuesday 9 th March, 2010, 09:58 CS1102S: Data Structures and Algorithms 08 A: Sorting III 1 1 Recap: Sorting

More information

Maintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater

Maintain binary search tree property nodes to the left are less than the current node, nodes to the right are greater CS61B, Summer 2002 Lecture #8 Barath Raghavan UC Berkeley Topics: Binary Search Trees, Priority queues 1 Binary search trees (BSTs) Represented as ordinary binary trees Maintain binary search tree property

More information

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin CS 61B Summer 2005 (Porter) Midterm 2 July 21, 2005 - SOLUTIONS Do not open until told to begin This exam is CLOSED BOOK, but you may use 1 letter-sized page of notes that you have created. Problem 0:

More information

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11 Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11 Ziad Matni Dept. of Computer Science, UCSB Thursday, 5/17 in this classroom Starts at 2:00 PM **SHARP** Please

More information

Binary Search and Worst-Case Analysis

Binary Search and Worst-Case Analysis Yufei Tao ITEE University of Queensland A significant part of computer science is devoted to understanding the power of the RAM model in solving specific problems. Every time we discuss a problem in this

More information

Review: Classes and Object Instances. Review: Creating an Object. Using Multiple Objects. DrawingGizmo pencil; pencil = new DrawingGizmo();

Review: Classes and Object Instances. Review: Creating an Object. Using Multiple Objects. DrawingGizmo pencil; pencil = new DrawingGizmo(); Review: Classes and Object Instances ; = new (); Class #05: Objects, Memory, & Program Traces Software Engineering I (CS 120): M. Allen, 12/13 Sept. 17 We are working with both a class () and an object

More information

5/31/2006. Last Time. Announcements. Today. Variable Scope. Variable Lifetime. Variable Scope - Cont. The File class. Assn 3 due this evening.

5/31/2006. Last Time. Announcements. Today. Variable Scope. Variable Lifetime. Variable Scope - Cont. The File class. Assn 3 due this evening. Last Time Announcements The File class. Back to methods Passing parameters by value and by reference. Review class attributes. An exercise to review File I/O, look at passing by reference and the use of

More information

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms C. L. Wyatt Today we will continue looking at sorting algorithms Bubble sort Insertion sort Merge sort Quick sort Common Sorting Algorithms

More information

SORTING AND SEARCHING

SORTING AND SEARCHING SORTING AND SEARCHING Today Last time we considered a simple approach to sorting a list of objects. This lecture will look at another approach to sorting. We will also consider how one searches through

More information

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University Chapter 3 Sorting Objectives To study and analyze time complexity of various sorting algorithms ( 3. 3.7). To design, implement, and analyze insertion sort ( 3.). To design, implement, and analyze bubble

More information

CompSci 101 Introduction to Computer Science

CompSci 101 Introduction to Computer Science CompSci 101 Introduction to Computer Science Apr 25, 2017 Prof. Rodger compsci 101 spring 2017 1 Announcements Last Day of class! Assign 9 by Friday, none accepted after that APT 9 due by Thursday, no

More information

Chapter 10 - Notes Applications of Arrays

Chapter 10 - Notes Applications of Arrays Chapter - Notes Applications of Arrays I. List Processing A. Definition: List - A set of values of the same data type. B. Lists and Arrays 1. A convenient way to store a list is in an array, probably a

More information

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search? Searching and Sorting [Chapter 9, pp. 402-432] Two important problems Search: finding something in a set of data Sorting: putting a set of data in order Both very common, very useful operations Both can

More information

Searching and Sorting

Searching and Sorting CS 211 SEARCH & SORT SEARCHING & SORTING Searching and Sorting Searching means that we have some collection of data, and we seek a particular value that might be contained within our collection. We provide

More information

I. True/False: (2 points each) On your bubble form fill out a for true and b for false.

I. True/False: (2 points each) On your bubble form fill out a for true and b for false. CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2010 What is your name?: There are three sections: I. True/False..............60 points; (30 questions, 2 points each) II. Multiple

More information

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Searching and Sorting

Computer Science 252 Problem Solving with Java The College of Saint Rose Spring Topic Notes: Searching and Sorting Computer Science 5 Problem Solving with Java The College of Saint Rose Spring 016 Topic Notes: Searching and Sorting Searching We all know what searching is looking for something. In a computer program,

More information

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 10 CS2110 Fall 2016 Miscellaneous 2 A3 due Monday night. Group early! Only 325 views of the piazza A3 FAQ yesterday morning. Everyone should

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 12: Sorting Algorithms MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Outline 2 Last week Implementation of the three tree depth-traversal algorithms Implementation of the BinarySearchTree

More information

What is an algorithm?

What is an algorithm? /0/ What is an algorithm? Searching and Sorting (Savitch, Chapter 7.) TOPICS Algorithms Complexity Binary Search Bubble Sort Insertion Sort Selection Sort A finite set of precise instrucons for performing

More information

Sorting (Weiss chapter )

Sorting (Weiss chapter ) Sorting (Weiss chapter 8.1 8.3) Sorting 5 3 9 2 8 7 3 2 1 4 1 2 2 3 3 4 5 7 8 9 Zillions of sorting algorithms (bubblesort, insertion sort, selection sort, quicksort, heapsort, mergesort, shell sort, counting

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon

CSCI 136 Data Structures & Advanced Programming. Lecture 12 Fall 2018 Profs Bill & Jon CSCI 136 Data Structures & Advanced Programming Lecture 12 Fall 2018 Profs Bill & Jon Last Time Assertions SLL Improvements Tail pointers Circularly Linked Lists Doubly Linked Lists Practice with recursion

More information

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6 More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6 Ziad Matni Dept. of Computer Science, UCSB Administrative CHANGED T.A. OFFICE/OPEN LAB

More information

Primitive Types and Methods. Reference Types and Methods. Review: Methods and Reference Types

Primitive Types and Methods. Reference Types and Methods. Review: Methods and Reference Types Primitive Types and Methds Java uses what is called pass-by-value semantics fr methd calls When we call a methd with input parameters, the value f that parameter is cpied and passed alng, nt the riginal

More information

CISC 1100: Structures of Computer Science

CISC 1100: Structures of Computer Science CISC 1100: Structures of Computer Science Chapter 8 Algorithms Gary M. Weiss Fordham University Department of Computer and Information Sciences Fall, 2010 What is an algorithm? There are many ways to define

More information

CS3: Introduction to Symbolic Programming. Lecture 5:

CS3: Introduction to Symbolic Programming. Lecture 5: CS3: Introduction to Symbolic Programming Lecture 5: Spring 2006 Nate Titterton nate@berkeley.edu Announcements Nate's office hours this week only: - Thursday, 2-4, in 329 Soda - (Usually, they are Wed

More information

ECE251: Tuesday September 11

ECE251: Tuesday September 11 ECE251: Tuesday September 11 Finish Branch related instructions Stack Subroutines Note: Lab 3 is a 2 week lab, starting this week and covers the Stack and Subroutines. Labs: Lab #2 is due this week. Lab

More information

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015 MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 4 due tonight at midnight -assignment 5 is out -midterm next Tuesday 3 last time 4

More information

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3) Outline Part 5. Computational Complexity (2) Complexity of Algorithms Efficiency of Searching Algorithms Sorting Algorithms and Their Efficiencies CS 200 Algorithms and Data Structures 1 2 (revisit) Properties

More information

What is an algorithm? CISC 1100/1400 Structures of Comp. Sci./Discrete Structures Chapter 8 Algorithms. Applications of algorithms

What is an algorithm? CISC 1100/1400 Structures of Comp. Sci./Discrete Structures Chapter 8 Algorithms. Applications of algorithms What is an algorithm? CISC 1100/1400 Structures of Comp. Sci./Discrete Structures Chapter 8 Algorithms Gary M. Weiss Fordham University Department of Computer and Information Sciences Copyright Gary M.

More information

Searching Elements in an Array: Linear and Binary Search. Spring Semester 2007 Programming and Data Structure 1

Searching Elements in an Array: Linear and Binary Search. Spring Semester 2007 Programming and Data Structure 1 Searching Elements in an Array: Linear and Binary Search Spring Semester 2007 Programming and Data Structure 1 Searching Check if a given element (called key) occurs in the array. Example: array of student

More information

Introduction Data Structures Nick Smallbone

Introduction Data Structures Nick Smallbone Introduction Data Structures Nick Smallbone Course organisation Lecturer: Nick Smallbone (me) nicsma@chalmers.se Course assistants: Nachiappan Valliappan (nacval@chalmers.se) Adi Hrustic (hrustic@student.chalmers.se)

More information

Introduction to Computer Science Midterm 3 Fall, Points

Introduction to Computer Science Midterm 3 Fall, Points Introduction to Computer Science Fall, 2001 100 Points Notes 1. Tear off this sheet and use it to keep your answers covered at all times. 2. Turn the exam over and write your name next to the staple. Do

More information

CmpSci 187: Programming with Data Structures Spring 2015

CmpSci 187: Programming with Data Structures Spring 2015 CmpSci 187: Programming with Data Structures Spring 2015 Lecture #22, More Graph Searches, Some Sorting, and Efficient Sorting Algorithms John Ridgway April 21, 2015 1 Review of Uniform-cost Search Uniform-Cost

More information

Data Types. Operators, Assignment, Output and Return Statements

Data Types. Operators, Assignment, Output and Return Statements Pseudocode Reference Sheet rev 4/17 jbo Note: This document has been developed by WeTeach_CS, and is solely based on current study materials and practice tests provided on the TEA website. An official

More information

Copyright 2009, Artur Czumaj 1

Copyright 2009, Artur Czumaj 1 CS 244 Algorithm Design Instructor: Artur Czumaj Lecture 2 Sorting You already know sorting algorithms Now you will see more We will want to understand generic techniques used for sorting! Lectures: Monday

More information

Divide and Conquer Algorithms: Advanced Sorting

Divide and Conquer Algorithms: Advanced Sorting Divide and Conquer Algorithms: Advanced Sorting (revisit) Properties of Growth-rate functions(1/3) 1. You can ignore low-order terms in an algorithm's growth-rate function. O(n 3 +4n 2 +3n) it is also

More information

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return 0. How would we write the BinaryHeap siftdown function recursively? [0] 6 [1] [] 15 10 Name: template class BinaryHeap { private: int maxsize; int numitems; T * heap;... [3] [4] [5] [6] 114 0

More information

Computer Science Foundation Exam

Computer Science Foundation Exam Computer Science Foundation Exam December 18, 015 Section I B COMPUTER SCIENCE NO books, notes, or calculators may be used, and you must work entirely on your own. SOLUTION Question # Max Pts Category

More information

Enjoy a Python Cookie Half a logo per person (blue or yellow python)! CompSci 101 Introduction to Computer Science. More Announcements.

Enjoy a Python Cookie Half a logo per person (blue or yellow python)! CompSci 101 Introduction to Computer Science. More Announcements. CompSci 101 Introduction to Computer Science Enjoy a Python Cookie Half a logo per person (blue or yellow python)! Apr 25, 2017 Prof. Rodger compsci 101 spring 2017 1 compsci 101 spring 2017 2 Announcements

More information

CompSci 101 Introduction to Computer Science

CompSci 101 Introduction to Computer Science CompSci 101 Introduction to Computer Science Apr 25, 2017 Prof. Rodger compsci 101 spring 2017 1 Enjoy a Python Cookie Half a logo per person (blue or yellow python)! compsci 101 spring 2017 2 Announcements

More information

Lecture 6 Sorting and Searching

Lecture 6 Sorting and Searching Lecture 6 Sorting and Searching Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 42 77 101 There are many algorithms for sorting a list

More information

CPSC 213. Introduction to Computer Systems. About the Course. Course Policies. Reading. Introduction. Unit 0

CPSC 213. Introduction to Computer Systems. About the Course. Course Policies. Reading. Introduction. Unit 0 About the Course it's all on the web page... http://www.ugrad.cs.ubc.ca/~cs213/winter1t1/ - news, admin details, schedule and readings CPSC 213 - lecture slides (always posted before class) - 213 Companion

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University 0/6/6 CS Introduction to Computing II Wayne Snyder Department Boston University Today Conclusions on Iterative Sorting: Complexity of Insertion Sort Recursive Sorting Methods and their Complexity: Mergesort

More information

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough.

17 February Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough. Midterm Review CSE 2011 Winter 2011 17 February 2011 1 Algorithm Analysis Given an algorithm, compute its running time in terms of O, Ω, and Θ (if any). Usually the big-oh running time is enough. Given

More information

Agenda: Discussion Week 7. May 11, 2009

Agenda: Discussion Week 7. May 11, 2009 Agenda: Discussion Week 7 Method signatures Static vs. instance compareto Exceptions Interfaces 2 D arrays Recursion varargs enum Suggestions? May 11, 2009 Method signatures [protection] [static] [return

More information

SELECTION. (Chapter 2)

SELECTION. (Chapter 2) SELECTION (Chapter 2) Selection Very often you will want your programs to make choices among different groups of instructions For example, a program processing requests for airline tickets could have the

More information

COMP 250. Lecture 7. Sorting a List: bubble sort selection sort insertion sort. Sept. 22, 2017

COMP 250. Lecture 7. Sorting a List: bubble sort selection sort insertion sort. Sept. 22, 2017 COMP 250 Lecture 7 Sorting a List: bubble sort selection sort insertion sort Sept. 22, 20 1 Sorting BEFORE AFTER 2 2 2 Example: sorting exams by last name Sorting Algorithms Bubble sort Selection sort

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Recursive list processing (part I) Version of March 24, 2013 Abstract These lecture notes are

More information

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm cmpt-225 Sorting Sorting Fundamental problem in computing science putting a collection of items in order Often used as part of another algorithm e.g. sort a list, then do many binary searches e.g. looking

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 12: Sorting Algorithms MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Outline 2 Last week Implementation of the three tree depth-traversal algorithms Implementation of the BinarySearchTree

More information

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3) Outline Part 5. Computational Complexity (2) Complexity of Algorithms Efficiency of Searching Algorithms Sorting Algorithms and Their Efficiencies CS 200 Algorithms and Data Structures 1 2 (revisit) Properties

More information

We will stamp HW Block day:

We will stamp HW Block day: Sorting Videos! We will stamp HW Block day: #10 Recursion worksheet #3 #11 12 Recursion-1 Coding Bats #12 Code Step By Step (see canvas) Today we Dance! No Homework tonight :) Guest Speaker Masters in

More information

CS171 Midterm Exam. October 29, Name:

CS171 Midterm Exam. October 29, Name: CS171 Midterm Exam October 29, 2012 Name: You are to honor the Emory Honor Code. This is a closed-book and closed-notes exam. You have 50 minutes to complete this exam. Read each problem carefully, and

More information

Searching in General

Searching in General Searching in General Searching 1. using linear search on arrays, lists or files 2. using binary search trees 3. using a hash table 4. using binary search in sorted arrays (interval halving method). Data

More information

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal CSC212 Data Structure t Lecture 18 Searching Instructor: George Wolberg Department of Computer Science City College of New York @ George Wolberg, 2016 1 Topics Applications Most Common Methods Serial Search

More information

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Announcements. PS 4 is ready, due next Thursday, 9:00pm. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Announcements PS 4 is ready, due next Thursday, 9:00pm Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am Room TBD Scope: Lecture 1 to Lecture 9 (Chapters 1 to 6 of text) You may bring a sheet of paper (A4, both

More information

Searching, Sorting. Arizona State University 1

Searching, Sorting. Arizona State University 1 Searching, Sorting CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 9 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State

More information

Algorithm Analysis. Performance Factors

Algorithm Analysis. Performance Factors Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases Every algorithm has certain inputs that allow it

More information

UNIT 5B Binary Search

UNIT 5B Binary Search 205/09/30 UNIT 5B Binary Search Course Announcements Written exam next week (Wed. Oct 7 ) Practice exam available on the Resources page Exam reviews: Sunday afternoon; watch Piazza for times and places

More information

CS 171: Introduction to Computer Science II. Arrays. Li Xiong

CS 171: Introduction to Computer Science II. Arrays. Li Xiong CS 171: Introduction to Computer Science II Arrays Li Xiong 1 Fundamentals Roadmap Types, variables, assignments, expressions Control flow statements Methods Arrays and binary search algorithm Programming

More information

Final Exam. COMP Summer I June 26, points

Final Exam. COMP Summer I June 26, points Final Exam COMP 14-090 Summer I 2000 June 26, 2000 200 points 1. Closed book and closed notes. No outside material allowed. 2. Write all answers on the test itself. Do not write any answers in a blue book

More information

Recursion: The Beginning

Recursion: The Beginning Yufei Tao ITEE University of Queensland This lecture is the inception of a powerful technique called recursion. If used judiciously, this technique can simplify the design of an algorithm significantly,

More information