Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

Chapter 19: Searching and Sorting Algorithms
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Data Structures Using Java1 Chapter 8 Search Algorithms.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching Arrays Linear search Binary search small arrays
Elementary Data Structures and Algorithms
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Data Structures Using C++1 Chapter 9 Search Algorithms.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Applications of Arrays (Searching and Sorting) and Strings
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)
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.:
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Maps and Dictionaries Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
CSC 211 Data Structures Lecture 13
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Review and Prepare for Test 3 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Big Oh CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Chapter 14: Searching and Sorting
Bubble Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
Priority Queues, Trees, and Huffman Encoding CS 244 This presentation requires Audio Enabled Brent M. Dingle, Ph.D. Game Design and Development Program.
HelloWorld – GameOver CS 244 – Data Structures and Algorithms Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Priority Queues and Sorting CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Data Structures Using C++1 Chapter 10 Sorting Algorithms.
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Quicksort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer.
Data Structures Using C++
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Chapter 16: Searching, Sorting, and the vector Type.
Take the Test Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Data Structures I (CPCS-204)
Data Structures Using C++ 2E
More on Merge Sort CS 244 This presentation is not given in class
Searching CSCE 121 J. Michael Moore.
Searching.
Searching CLRS, Sections 9.1 – 9.3.
Review and Prepare for Test 2
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Data Structures Using C++ 2E
Data Structures and Algorithms CS 244
Presentation transcript:

Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Content derived from: Data Structures Using C++ (D.S. Malik) and Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount)

In the Past Priority Queues Unordered List implementation Ordered List implementation

Now and Future Searching Sequential Binary

Objectives (chapter 9) Learn various search algorithms Explore how to implement sequential and binary search algorithms Discover how sequential and binary search algorithms perform Be aware of lower bound on comparison-based search algorithms

Marker Slide General Questions? Next Searching General Sequential (Linear) Binary

Search Algorithms Item Key Unique member of the item Used in searching, sorting, insertion, deletion Number of key comparisons Comparing the key of the search item with the key of an item in the list Book reference: Can use class arrayListType from Chapter 3 Implements a list and basic operations in an array

Marker Slide Questions On: Searching General Next Searching Sequential (Linear) Binary

Motivation: Linear Search Say you have 1,000 action figures You keep them in an unorganized (but very cool) looking pile on your otherwise unused bed A friend comes over and wants to see the super rare and totally awesome Boba Fett In the worst case how many action figures will you have to look at before you are guaranteed to have found the specified Boba Fett? Review

Motivation: Linear Search (cont) Worst case: 1000 If there were 10,000 action figures? Worst case is 10,000 If there were 100,000? Worst case is 100,000 So the input size (1000, 10000, ) is directly proportional to the worst case search time This is described as O(n) for k = 1 to 1000 look at action figure number k stop if it is Boba Fett for k = 1 to 10,000 look at action figure number k stop if it is Boba Fett for k = 1 to 100,000 look at action figure number k stop if it is Boba Fett for k = 1 to n look at action figure number k stop if it is Boba Fett Review

Sequential Search More book refs: Array-based lists See Chapter 3 Linked Lists See Chapter 5 Code for Sequential Search works the same for array based lists and linked list See code circa page 499 AND You were always using something as a UNIQUE key to find the item being searched for POINT IS You have seen sequential searches (A LOT)

Analysis of Sequential Search template int arrayListType ::seqSearch(const elemType& item) const { int loc; bool found = false; for (loc = 0; loc < length; loc++) { if (list[loc] == item) { found = true; break; } if (found) return loc; else return -1; } //end seqSearch Count number of comparisons Note list and length are member variables of class arrayListType N = length Worst case results in N comparisons O(N) Aside: Best case takes 1 compare O(1) Average case (consider all cases, divide by N) (1+2+3+…+N) / N = (N^2 + N) / 2N = (N + 1) / 2 = O(N) Code from book, circa page 499

Marker Slide Questions on: Searching General Sequential (Linear) Next Searching Binary

Recall: Using a Dictionary Keep cutting the search area in half Open dictionary to the middle Then to ¼ or ¾ mark Then (1/8 or 3/8 mark) or (5/8 or 7/8) Then … Find H appiness Review

Recall: Binary Search By investigating dictionaries We discovered an ordering allows us to perform a binary search Binary Search REQUIRES an ORDERED List Binary Search is O(log n) Uses a Divide and Conquer Strategy Review

C++ Code int BinarySearchForInteger(vector v, int from, int to, int val) { if (from > to) return -1; // val not found int mid = (from + to)/2; if (v[mid] == val) return mid; else if (val > v[mid]) return search(v, mid+1, to, val); else return search(v, from, mid-1,val); } See also Code from book circa page 503 Count number of comparisons Note list and length are member variables of class orderedArrayListType N = length Worst case results in N comparisons O(lg N)

C++ Code template int orderedArrayListType ::binarySearch(const elemType& item) const { int first = 0; int last = length - 1; int mid; bool found = false; while ( (first <= last) && (!found) ) { mid = (first + last) / 2; if (list[mid] == item) found = true; else if (list[mid] > item) last = mid - 1; else first = mid + 1; } if (found) return mid; else return -1; }//end binarySearch Code from book circa page 503 Count number of comparisons Note list and length are member variables of class orderedArrayListType N = length Worst case results in N comparisons O(lg N)

Marker Slide Questions on: Searching General Sequential (Linear) Binary Next Searching Closing Comments Lower bound on Comparison Based Search Algorithms

Comparison Based Searches Sequential (linear) and Binary Searches Are Comparison Based Searches Sequential is O(N) Binary is O(lg N) A linear search checks each element one at a time, starting from the beginning of the list A binary search repeatedly divides the search space in half until it finds the value This assumes the list is sorted Requires a data structure that supports immediate O(1) look-up e.g. std::vector

Book Gives Theorem Thus for a search algorithm to be (worst-case) faster than O(lg N) It cannot be comparison based Knowing this result is good thing Math helps CS again Implication is: To do a search faster will require something more than just comparisons

The End (of This Part) End Check for more Peek into future Hash Tables do Searches in the worst case O(n) but average case O(1) O(n) is NOT better than O(lg n) but on average it can be faster O(1) is faster than O(lg n) This “average” case analysis will show up in later classes