CS261 Data Structures Ordered Bag Dynamic Array Implementation.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
HST 952 Computing for Biomedical Scientists Lecture 9.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Searching and Sorting I 1 Searching and Sorting 1.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
Searching/Sorting Introduction to Computing Science and Programming I.
Searching Arrays Linear search Binary search small arrays
Elementary Data Structures and Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Search - CIS 1068 Program Design and Abstraction
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015.
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)
Searching and Sorting Topics Sequential Search on an Unordered File
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)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
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.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
CS261 – Recitation 5 Fall Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1.
CSC 211 Data Structures Lecture 13
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
Chapter 12 Binary Search and QuickSort Fundamentals of Java.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
CS Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 16: Searching, Sorting, and the vector Type.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
Sorted Dynamic Array Bag and Set
Searching an Array: Binary Search
Searching & Sorting "There's nothing hidden 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.
More complexity analysis & Binary Search
Abstract Data Types (ADTs)
Searching CSCE 121 J. Michael Moore.
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.
Adapted from Pearson Education, Inc.
CSc 110, Spring 2017 Lecture 39: searching.
CSC215 Lecture Algorithms.
Chapter 8 Search and Sort
MSIS 655 Advanced Business Applications Programming
Chapter 5 Ordered List.
Searching and Sorting Topics Sequential Search on an Unordered File
24 Searching and Sorting.
Principles of Computing – UFCFA3-30-1
Searching.
Algorithm Efficiency and Sorting
CS 261 – Data Structures AVL Trees.
Presentation transcript:

CS261 Data Structures Ordered Bag Dynamic Array Implementation

Goals Understand the downside of unordered containers Binary Search Ordered Bag ADT

Downside of unordered collections What is the complexity of finding a particular element in the dynamic array implementation of the Bag? What about the linked list implementation of the Bag? Many applications will require a significant number of accesses of particular values… so we need a more efficient means for finding values.

Power of Ordered Collections Why do you suppose that dictionaries or phonebooks keep their elements in order? Suppose I asked you to find the phone number for Chris Smith? Suppose I asked you who was the person with phone number ?

Guess My Number We all know the heuristic of cutting a collection in half from the game “guess my number” I’m thinking of a number between 1 and 100. What questions will you ask to find my number (efficiently)?

Binary Search The formal name for this process is binary search Each step cuts region containing the value in half Starting with n items, how many times can I cut in half before reaching a set of size one?

Binary Search: O(log n) A O(log n) search is much much faster than an O(n) search (for large n) Log 2 1,000,000 ~ 20 Log of largest unsigned integer value in C ( ) is 32 – Instead of 4 billion comparisons, you only need 32!

What are the requirements for performing a binary search? Binary Search…

Binary Search Requirments Random access to the elements Elements are already in sorted order

Binary Search Ordered Array: Intuition Compute the middle index Check for the value at that index If found the value, done, return the index If not found – If value is less than value at the index, repeat with left half of array – Else repeat with right half of array

Binary Search: Ordered Array Algorithm int _binarySearch(TYPE * data, int size, TYPE val) { int low = 0; int high = size; int mid; while (low < high) { mid = (low + high) / 2; //mid less than val looking for if (LT(data[mid],val)) low = mid + 1; else high = mid; } return low; }

Binary Search Ordered Array: Return Value If value is found, returns index of that value If value is not found, returns position where it can be inserted without violating ordering NOTE: returned index can be larger than a legal index int _binarySearch(TYPE * data, int size, TYPE val) { int low = 0; int high = size; int mid; while (low < high) { mid = (low + high) / 2; if (LT(data[mid],val)) low = mid + 1; else high = mid; } return low; }

Ordered Bag Abstraction Same operations as Bag ADT – Add – Contains – Remove Property: elements maintain sorted order How can we do this efficiently?

Which operation is now faster? Using a dynamic array for an Ordered Bag, which of the following operations is made faster by using a binary search? – add(element) – contains(element) – remove(element) Are any made slower?

Applications of Ordered Collections You will get your chance to write an implementation of a ordered bag But first, other reasons for keeping a collection of elements in order: – Fast merge – Set operations  union, intersection, etc.

Fast Merge

Fast Merge (cont.)

Set Operations: Similar to Merge You can quickly merge two ordered arrays into a new ordered array – What is its complexity? Set operations (intersection, union, difference, subset) are similar to merge – Try these on your own…(See Chapter 9)  O(n)

Summary Searching DynArr and LinkedLists are O(N) on average Binary Search provides O(log N) search but requires that – We have random access to data (ie. data is in an array) – The data is ordered This means, of course, that we can only do efficient binary search on an array (NOT a linked list)

Question? Why not just sort the array every time you add an element to the collection? Is it more/less efficient…or the same?

Bug?? In 2006, a bug was found by Google – Where is it? How can we fix it? int _binarySearch(TYPE * data, int size, TYPE val) { int low = 0; int high = size; int mid; while (low < high) { mid = (low + high) / 2; if (LT(data[mid],val)) low = mid + 1; else high = mid; } return low; } mid = low + ((high-low) / 2)

Your Turn Now that we have _binarySearch, how do the following change? – addBag – containsBag – removeBag Complete Worksheet #26 Read Binary Search Correctness Argument