Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.

Slides:



Advertisements
Similar presentations
Data Structures & Algorithms Kathleen Christie Teaching Fellow, Department of Computing Science, University of Aberdeen.
Advertisements

Zabin Visram Room CS115 CS126 Searching
CSE Lecture 3 – Algorithms I
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Binary Search Visualization i j.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
J. Michael Moore Searching & Sorting CSCE 110. J. Michael Moore Searching with Linear Search Many times, it is necessary to search an array to find a.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
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.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Chapter 8 ARRAYS Continued
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Building Java Programs Chapter 13 Searching reading: 13.3.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
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.
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.
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.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
1 Searching and Sorting Linear Search Binary Search.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
CSC 205 Java Programming II Algorithm Efficiency.
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 Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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]
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.
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.
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
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 Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
Searching Chapter 13 Objectives Upon completion you will be able to:
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.
CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Sorting and Searching Bubble Sort Linear Search Binary Search.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
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 Arrays
COP 3503 FALL 2012 Shayan Javed Lecture 15
Sorting Data are arranged according to their values.
CSC215 Lecture Algorithms.
Sorting Data are arranged according to their values.
Searching and Sorting Arrays
CSC 205 Java Programming II
Cs212: DataStructures Lecture 3: Searching.
Data Structures: Searching
Searching and Sorting Arrays
Applications of Arrays
Presentation transcript:

Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms

2 Searching algorithms Linear search: sorted and unsorted array Binary search: sorted array

3 Search Concept

4 Linear Search

5 Unsorted List

6 Algorithm for Linear Search (Unsorted Array) Go through the array comparing the item required with the item in the array at each position If item is not found state this If item is found state position of item Number of comparisons: Average for successful search = number of elements/2 (n/2) Unsuccessful search = number of elements (n) O (n) Performance = O (n)

7 Linear Search (Another Algorithm) int LinearSearch(int [] array, int item) { //local variable to store position of required item set initially to –1 int place = -1; int index = 0; //Go through array till whole array searched or item found while ( index < array.length) { //check if item is found, and if so set place accordingly if ( array[index] == item) place = index; else index++; } //Return the item’s place in the array return place; } while ( index < array.length && place == -1) {

8 /*Linear (Sequential) Search algorithm for finding an element in the array. If the search element is found, it returns the position(index) of the element. otherwise, it return -1. */ public int LinearSearch(int v) { inti=0, iv = -1; while( i < NElements && iv == -1 ) { if( A[i] == v ) iv = i; i++; } return iv; //iv is either index of the found element or -1 } Linear Search Code for Linear Search (Unsorted Array)

9 Binary Search

10 Binary Search Algorithm int Binary Search (Array A, item v) int first = 0, last = n-1, mid, found =-1; while (first <= last and found == -1) mid = (first+last)/2; if A[mid] == v then found = mid; else if A[mid] < v then first = mid +1; else last = mid -1; end while return (found) end Binary Search What is the order of magnitude of the time performance for binary search? O(Log 2 n) Binary Search

11 Exercise Implement Binary search using Java and test it using a testing program of your design. Run feeding array + search item Print the run time

12 Binary Search program

13