        Linear Search 線性搜尋: char names[100][10];

Slides:



Advertisements
Similar presentations
Binary Search lo Binary search. Given a key and sorted array a[], find index i such that a[i] = key,
Advertisements

Computer Science 209 Software Development Equality and Comparisons.
Sort the given string, without using string handling functions.
The Binary Search Algorithm in Structured Flowchart Form Implemented in Both C/C++ and Java Bary W Pollack Dec. 28, 2001.
Lecture 4: Divide-and-Conquer. Steps in Divide and Conquer As its name implies divide-and-conquer involves dividing a problem into smaller problems that.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
Recitation 2 Tuesday, January 27, General Stuff Office Hours: Mon-Thurs 7-10 Habermann Did everybody manage to set up a way to turn things.
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
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.
Searching Arrays Linear search Binary search small arrays
Mon 29 Sep 2014Lecture 4 1. Running Time Performance analysis Techniques until now: Experimental Cost models counting execution of operations or lines.
Building Java Programs Chapter 13 Searching reading: 13.3.
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.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.:
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
Searching CS 105 See Section 14.6 of Horstmann text.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
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 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.
Binary Search From solving a problem to verifying an answer.
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 & 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.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i
LOOPING IN C. What would be the output of the following program main( ) { int j ; while ( j
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Searching CS 110: Data Structures and Algorithms First Semester,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
RecursionRecursion Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison Wesley.
APPLICATIONS OF RECURSION Copyright © 2006 Pearson Addison-Wesley. All rights reserved
Searching Arrays Linear search Binary search small arrays
Binary Search.
Divide-and-Conquer.
Chapter 7 Single-Dimensional Arrays
Adapted from slides by Marty Stepp and Stuart Reges
More complexity analysis & Binary Search
Applications of Recursion
CS 3343: Analysis of Algorithms
Adapted from Pearson Education, Inc.
Lecture 14: binary search and complexity reading:
searching Concept: Linear search Binary search
Lecture 15: binary search reading:
CSC 205 Java Programming II
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant.
Linear Search Binary Search Tree
Cs212: DataStructures Lecture 3: Searching.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Given value and sorted array, find index.
Linear search A linear search sequentially moves through your list looking for a matching value. 1. The element is found, i.e. a[i] = x. 2. The entire.
Adapted from slides by Marty Stepp and Stuart Reges
Module 8 – Searching & Sorting Algorithms
Binary Search Counting
Running Time Exercises
Notorious Bugs – BYTE, September byte
L.O. We are learning to make a tally chart.
Module 8 – Searching & Sorting Algorithms
ICS103: Programming in C Searching, Sorting, 2D Arrays
Sorting Algorithms.
Presentation transcript:

        Linear Search 線性搜尋: char names[100][10]; char target[20]="EGG";         1 2 3 4 5 6 7 8 99 CAT GOA ADA FISH DOG BOY HEN EGG KEN … for (i=0;i<100;i++) if (strcmp(names[i],target)==0) break; if ( ) printf ("found!\n"); else printf ("not found!\n"); i<100 2019年7月27日 Sorting & Searching

1 char A[10][5]; char target[5]="HEN"; int lo=0, hi=9, mid; Binary Search 二分搜尋: 1 lo=mid+1 mid=(lo+hi)/2 mid mid lo lo hi 1 2 3 4 5 6 7 8 9 ADA BOY CAT DOG EGG FISH GOA HEN IDA KEN A[i] No Yes A[mid]=target ?? A[mid]=target ?? 2019年7月27日 Sorting & Searching

2 char A[10][5]; char target[5]="CAT"; int lo=0, hi=9, mid; Binary Search 二分搜尋: 2 mid=(lo+hi)/2 lo=mid+1 hi=mid-1 lo mid mid lo hi 1 2 3 4 5 6 7 8 9 ADA BOY CAT DOG EGG FISH GOA HEN IDA KEN hi A[i] No Yes No A[mid]=target ?? A[mid]=target ?? 2019年7月27日 Sorting & Searching

3 char A[10][5]; char target[5]="CAT"; int lo=0, hi=9, mid; Binary Search 二分搜尋: "CAB" 3 mid=(lo+hi)/2 lo lo=mid+1 hi=mid-1 mid mid lo hi 1 2 3 4 5 6 7 8 9 ADA BOY CAT DOG EGG FISH GOA HEN IDA KEN hi A[i] No No No target < A[mid] hi<lo  not found 2019年7月27日 Sorting & Searching

4 二分搜尋 Binary Search int binarySearch (char target[]){ int lo=0, hi=9, mid, pos, n; pos = -1; // not found return pos; } 4 while ( ){ } lo<=hi && pos<0 mid = (lo+hi)/2; n = strcmp (target, A[mid]); pos = mid; if (n==0) else if (n<0) else hi = mid-1; lo = mid+1; mid lo hi 1 2 3 4 5 6 7 8 9 ADA BOY CAT DOG EGG FISH GOA HEN IDA KEN 2019年7月27日 Sorting & Searching A[i]