CS212: DATASTRUCTURES Lecture 3: Searching 1. Search Algorithms Sequential Search It does not require an ordered list. Binary Search It requires an ordered.

Slides:



Advertisements
Similar presentations
Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.
Advertisements

CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
Searching Arrays Linear search Binary search small arrays
LAB 10.
Computer Programming Lab(4).
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Computer Programming Lab(5).
CS1101: Programming Methodology Aaron Tan.
Building Java Programs Chapter 13 Searching reading: 13.3.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
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.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/23/2012 and 10/24/2012 -Using Exceptions -Homework 4.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
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.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
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.
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.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
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.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Chapter 5 Case Study. Chapter 5 The RPS Flowchart.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
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.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
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.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Algorithm Definition An algorithm is a step-by-step solution to a problem.
Array Review Selection Sort Get out your notes.. Learning Objectives Be able to dry run programs that use arrays Be able to dry run programs that use.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Slides by Evan Gallagher
Slides by Evan Gallagher
Chapter 2 Clarifications
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
Chapter 5 Ordered List.
Maha AlSaif Maryam AlQattan
Computer Programming Methodology Input and While Loop
Repetition.
Data types, Expressions and assignment, Input from User
Something about Java Introduction to Problem Solving and Programming 1.
Decision statements. - They can use logic to arrive at desired results
Chapter 5 Ordered List.
Java Language Basics.
Cs212: DataStructures Lecture 3: Searching.
Repetition Statements
Building Java Programs
Building Java Programs
Array Review Selection Sort
Random Numbers while loop
Building Java Programs
Presentation transcript:

CS212: DATASTRUCTURES Lecture 3: Searching 1

Search Algorithms Sequential Search It does not require an ordered list. Binary Search It requires an ordered list. 2

1/ Sequential (Linear) Search 3  Look at every element :  very straightforward loop comparing every element in the array with the target(key).  Eighter we find it or we reach the end of the list!

2/ Binary search algorithm 4  Search a sorted array by repeatedly dividing the search interval in half.  A fast way to search a sorted array is to use a binary search.

Example 1 5  Write a program that Prints the index of a number in the following list. The number is entered from the user (using the binary search). {4,6,8,10,14,16,22,34,47,55,58,90}

Example 1 – C++ 6  #include  int BinSearchI(int list[], int key, int size){  int mid;  int first=0;  int last=size-1;  while(first<=last)  {  mid = (first+last)/2;  if (list[mid]==key)  return mid;  else if (list[mid]<key)  first=mid+1;  else  last = mid-1;  }  return -1; }  void main() {  int A[]= {4,6,8,10,14,16,22,34,47,55,58,90}; int x ; int size=12;  cout<<"The list is: {";  for (int i = 0; i< 12;i++){  cout<<A[i]<<","; }  cout<<"}";  cout<<"Please enter a number: ";  cin>>x;  cout<<"The index of "<<x<<" is: "<<BinSearchI (A,x,size);  system ("pause");  return 0;  } 

Example 1 – Java 7 package binarys1; import java.util.Scanner; public class BinaryS1 { public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); int [] A = {4,6,8,10,14,16,22,34,47,55,58,90}; System.out.print("The list is: {"); for (int i = 0; i<A.length;i++){ System.out.print(A[i]+","); } System.out.println("}"); System.out.print("Please enter a number: "); int x = input.nextInt(); System.out.println("The index of "+x+" is: "+BinSearchI (A,x,A.length)); } public static int BinSearchI(int [] list, int key, int size){ int mid,first=0,last=size-1; while(first<=last){ mid = (first+last)/2; if (list[mid]==key) return mid; else if (list[mid]<key) first=mid+1; else last = mid-1; } return -1; }

Example 2 8  Write a program that search for a number in the following list and prints FOUND if the number is in the list and NOT FOUND if the number is not in the list. The number must be entered from the user (using binary search) {0,3,5,7,8,9,12,21,26,33,37,38,40,43,46,50}

Example 2 – C++ 9  #include  void BinarySearchR(int list[], int first, int last, int key){  int loc;  int m = (first+last)/2;  if (key==list[m])  cout<<key<<" is FOUND.\n";  else if (first==last)  cout<<key<<" is NOT FOUND.\n");  else if(key<list[m])  BinarySearchR(list,first,m-1,key);  else if(key>list[m])  BinarySearchR(list,m+1,last,key);  }  void main() {  int B[]= {0,3,5,7,8,9,12,26,33,37,38,40,43,46,50};  int y;  int size=12  cout<<"The list is: {";  for(int i=0; i<12; i++){  cout<<B[i];  cout<<",";  }  cout<<"}\n“;  cout<<"Enter a number: ";  cin>>y;  BinarySearchR(B,0,size-1,y);  system("pause");  return 0;  } 

Example 2 – Java 10 package binarys2; import java.util.Scanner; public class BinaryS2 { public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); int [] B = {0,3,5,7,8,9,12,26,33,37,38,40,43,46,50}; System.out.print("The list is: {"); for(int i=0; i<B.length; i++){ System.out.print(B[i]+","); } System.out.println("}"); System.out.print("Enter a number: "); int y = input.nextInt(); BinarySearchR(B,0,B.length-1,y); } public static void BinarySearchR(int [] list, int first, int last, int key){ int loc,m = (first+last)/2; if (key==list[m]) System.out.println(key+" is FOUND."); else if (first==last) System.out.println(key+" is NOT FOUND."); else if(key<list[m]) BinarySearchR(list,first,m-1,key); else if(key>list[m]) BinarySearchR(list,m+1,last,key); }