Searching.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Garfield AP Computer Science
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
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)
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
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.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
1 Sorting and Searching "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.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
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.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Chapter 23 Sorting Jung Soo (Sue) Lim Cal State LA.
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Chapter 16: Searching, Sorting, and the vector Type
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Top 50 Data Structures Interview Questions
Data Structures I (CPCS-204)
Analysis of Algorithms
Searching Given a collection and an element (key) to find… Output
Data Structures Using C++ 2E
COP 3503 FALL 2012 Shayan Javed Lecture 15
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Chapter 7 Single-Dimensional Arrays
Lecture 22 Binary Search Trees Chapter 10 of textbook
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.
COSC160: Data Structures Linked Lists
Week 11 - Friday CS221.
Search by Hashing.
Teach A level Computing: Algorithms and Data Structures
Searching.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
structures and their relationships." - Linus Torvalds
Topic 14 Searching and Simple Sorts
Sorting and Searching "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,
CS202 - Fundamental Structures of Computer Science II
Data Structures and Algorithms
Searching and Sorting Arrays
Searching and Sorting 1-D Arrays
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Searching: linear & binary
Searching CLRS, Sections 9.1 – 9.3.
Sub-Quadratic Sorting Algorithms
CS202 - Fundamental Structures of Computer Science II
Topic 14 Searching and Simple Sorts
Cs212: DataStructures Lecture 3: Searching.
Data Structures: Searching
Topic 24 sorting and searching arrays
Chapter 4.
Data Structures and Algorithms
Advanced Implementation of Tables
Searching.
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Data Structures and Algorithms
CS203 Lecture 15.
Data Structures Using C++ 2E
Searching.
structures and their relationships." - Linus Torvalds
Presentation transcript:

Searching

Linear Search Given a list of data, find the location of a particular value or report that value is not present Linear search intuitive approach: start at first item is it the one I am looking for? If not, go to next item repeat until found or all items checked If items not sorted or unsortable this approach is necessary CS 321 - Data Structures

Linear Search Code /* return the index of the first occurrence of target in list, or -1 if target not present in list */ public int linearSearch(int list[], int target) { int i = 0; while(i < list.length && list[i] != target) i++; if(i >= list.length) return -1; else return i; } CS 321 - Data Structures

Question 1 What is the average case Big-O of linear search in an array with n items, if an item is present? O(n) O(n2) O(1) O(log(n)) O(n log(n)) CS 321 - Data Structures

Question 1 What is the average case Big-O of linear search in an array with n items, if an item is present? O(n) O(n2) O(1) O(log(n)) O(n log(n)) CS 321 - Data Structures

Searching in a Sorted List If items are sorted, we can divide and conquer Dividing your work in half with each step Generally a good thing The Binary Search on list in ascending order start at middle of list is that the item? if not, is it less than or greater than the item? less than, move to second half of list greater than, move to first half of list repeat until found or sub-list size = 0 CS 321 - Data Structures

Binary Search list Is middle item what we are looking for? low item middle item high item Is middle item what we are looking for? If not, is it more or less than the target? If lower… list low middle high item item item and so forth… CS 321 - Data Structures

Recursive Binary Search public static int search(int list[], int target) { return b-search(list, target, 0, list.length – 1); } public static int b-search(int list[], int target, int low, int high) { if( low <= high ) int mid = low + ((high - low) / 2); if( list[mid] == target ) return mid; else if( list[mid] > target ) return b-search(list, target, low, mid – 1); else return b-search(list, target, mid + 1, high); return -1; CS 321 - Data Structures

Question 2 What is the worst case Big O of binary search in an array with n items, if an item is present? O(n) O(n2) O(1) O(log(n)) O(n log(n)) CS 321 - Data Structures

Question 2 What is the worst case Big O of binary search in an array with n items, if an item is present? O(n) O(n2) O(1) O(log(n)) O(n log(n)) CS 321 - Data Structures

Other Searching Algorithms Interpolation Search more like what people really do Binary Search Trees Hash Table Searching Best-First A* Interpolation Search: Interpolation search is an algorithm for searching for a given key in an indexed array that has been ordered by numerical values assigned to the keys (key values). It parallels how humans search through a telephone book for a particular name, the key value by which the book's entries are ordered. In each search step it calculates where in the remaining search space the sought item might be, based on the key values at the bounds of the search space and the value of the sought key, usually via a linear interpolation. The key value actually found at this estimated position is then compared to the key value being sought. If it is not equal, then depending on the comparison, the remaining search space is reduced to the part before or after the estimated position. This method will only work if calculations on the size of differences between key values are sensible. [Wikipedia] Binary Search Trees: Binary search trees keep their keys in sorted order, so that lookup and other operations can use the principle of binary search: when looking for a key in a tree (or a place to insert a new key), they traverse the tree from root to leaf, making comparisons to keys stored in the nodes of the tree and deciding, based on the comparison, to continue searching in the left or right subtrees. On average, this means that each comparison allows the operations to skip about half of the tree, so that each lookup, insertion or deletion takes time proportional to the logarithm of the number of items stored in the tree. This is much better than the linear time required to find items by key in an (unsorted) array, but slower than the corresponding operations on hash tables. [Wikipedia] Hash Tables: Hash table (hash map) is a data structure, which implements an associative array abstract data type, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found. Ideally, the hash function will assign each key to a unique bucket, but most hash table designs employ an imperfect hash function, which might cause hash collisions, where the hash function generates the same index for more than one key. Such collisions must be accommodated in some way. In a well-dimensioned hash table, the average cost for each lookup is independent of the number of elements stored in the table. [Wikipedia] Best-First: Best-first search is a search algorithm which explores a graph by expanding the most promising node chosen according to a specified rule. [Wikipedia] A*: A* (pronounced as "A star") is a computer algorithm that is widely used in pathfinding and graph traversal, the process of plotting an efficiently directed path between multiple points, called nodes. It enjoys widespread use due to its performance and accuracy. However, in practical travel-routing systems, it is generally outperformed by algorithms which can pre-process the graph to attain better performance, although other work has found A* to be superior to other approaches. [Wikipedia] CS 321 - Data Structures

CS 321 - Data Structures