Searching in Arrays
1
Instructor
Dhiviya Rose J , AP-Sr. Scale | SoCSE
INFO 106 – Computer Programming
Searching Arrays: Linear Search and
Binary Search
• Search an array for a key value
• Linear search
▫ Simple
▫ Compare each element of array with key value
▫ Useful for small and unsorted arrays
2
INFO 106 – Computer Programming
Linear Search
• Step through array of records, one at a time.
• Look for record with matching key.
• Search stops when
▫ record with matching key is found
▫ or when search has examined all records without
success.
INFO 106 – Computer Programming
How Linear Search works
4
5
Case 1: Target
found
6
Case 1: Target NOT
found
INFO 106 – Computer Programming
7
Program
#include<stdio.h>
void main()
{
int a[10],i,target;
printf("Enter array value n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Which value to be search ->");
scanf("%d",&target);
/* Linear Search logic */
for(i=0;i<n;i++)
if(target==a[i])
{
printf(“Value found at %d”,i);
}
}
INFO 106 – Computer Programming
Advantages of Linear Search
• Don't have to sort the data we search.
• Works well if only search operation is minimum
• Not optimal in case of large amount of data
8
INFO 106 – Computer Programming
Binary Search
• Assume that we are give an array of records that
is sorted. For instance:
▫ an array of records with integer keys sorted from
smallest to largest (e.g., ID numbers), or
▫ an array of records with string keys sorted in
alphabetical order (e.g., names).
Binary Search
[ 0 ] [ 1 ]
Example: sorted array of integer keys. Target=7.
3 6 7 11 32 33 53
[ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
11
INFO 106 – Computer Programming
Binary Search Pseudocode
…
if(size == 0)
found = false;
else {
middle = index of approximate midpoint of array segment;
if(target == a[middle])
target has been found!
else if(target < a[middle])
search for target in area before midpoint;
else
search for target in area after midpoint;
}
…
INFO 106 – Computer Programming
Program
int result=-1;
int low=0;
int high=length-1;
int mid;
while( result==-1 && low<=high )
{ mid= low + ((high - low) / 2);
if( list[mid] == target )
result = mid;
else if( list[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
13
INFO 106 – Computer Programming
Other Searching Algorithms
• Interpolation Search
• Indexed Searching
• Binary Search Trees
• Hash Table Searching
• Grover's Algorithm
• Best-first
• A*
14

Searching in Arrays

  • 1.
    Searching in Arrays 1 Instructor DhiviyaRose J , AP-Sr. Scale | SoCSE
  • 2.
    INFO 106 –Computer Programming Searching Arrays: Linear Search and Binary Search • Search an array for a key value • Linear search ▫ Simple ▫ Compare each element of array with key value ▫ Useful for small and unsorted arrays 2
  • 3.
    INFO 106 –Computer Programming Linear Search • Step through array of records, one at a time. • Look for record with matching key. • Search stops when ▫ record with matching key is found ▫ or when search has examined all records without success.
  • 4.
    INFO 106 –Computer Programming How Linear Search works 4
  • 5.
  • 6.
  • 7.
    INFO 106 –Computer Programming 7 Program #include<stdio.h> void main() { int a[10],i,target; printf("Enter array value n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Which value to be search ->"); scanf("%d",&target); /* Linear Search logic */ for(i=0;i<n;i++) if(target==a[i]) { printf(“Value found at %d”,i); } }
  • 8.
    INFO 106 –Computer Programming Advantages of Linear Search • Don't have to sort the data we search. • Works well if only search operation is minimum • Not optimal in case of large amount of data 8
  • 9.
    INFO 106 –Computer Programming Binary Search • Assume that we are give an array of records that is sorted. For instance: ▫ an array of records with integer keys sorted from smallest to largest (e.g., ID numbers), or ▫ an array of records with string keys sorted in alphabetical order (e.g., names).
  • 10.
    Binary Search [ 0] [ 1 ] Example: sorted array of integer keys. Target=7. 3 6 7 11 32 33 53 [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ]
  • 11.
  • 12.
    INFO 106 –Computer Programming Binary Search Pseudocode … if(size == 0) found = false; else { middle = index of approximate midpoint of array segment; if(target == a[middle]) target has been found! else if(target < a[middle]) search for target in area before midpoint; else search for target in area after midpoint; } …
  • 13.
    INFO 106 –Computer Programming Program int result=-1; int low=0; int high=length-1; int mid; while( result==-1 && low<=high ) { mid= low + ((high - low) / 2); if( list[mid] == target ) result = mid; else if( list[mid] < target) low = mid + 1; else high = mid - 1; } 13
  • 14.
    INFO 106 –Computer Programming Other Searching Algorithms • Interpolation Search • Indexed Searching • Binary Search Trees • Hash Table Searching • Grover's Algorithm • Best-first • A* 14