0% found this document useful (0 votes)
3 views

C Programming Searching

The document discusses two main searching algorithms: linear search and binary search. Linear search checks each element in an unsorted list sequentially, while binary search is more efficient for sorted lists, reducing the search space by half with each comparison. The time complexity for linear search is O(n), whereas binary search operates in O(log n) time complexity.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C Programming Searching

The document discusses two main searching algorithms: linear search and binary search. Linear search checks each element in an unsorted list sequentially, while binary search is more efficient for sorted lists, reducing the search space by half with each comparison. The time complexity for linear search is O(n), whereas binary search operates in O(log n) time complexity.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CS1101: Foundations of Programming

Searching

Dept. of Computer Science & Engineering


Indian Institute of Technology Patna

IIT Patna 1
Searching
• Check if a given element, usually called key, is present in a list / array
• Example: Searching for a student record, roll number can be the key
• Two broad approaches will be looked into:
• List / array is not-sorted: We have no other option but to check every ele-
ment present in the list. (Linear search)
• List / array is sorted: Though the previous method works here, it will be in-
efficient. We can use binary search

IIT Patna 2
Linear search
• Basic idea
• Start from the 1st element of the array
• Inspect each element in sequence and check if it matches with the key
• If a match is found, then report the element is present
• If no match is found, report that the key does not exist. We can return special
value such as −1 to indicate such situation

IIT Patna 3
Linear search
• Returns the index of the array if key is present, else -1

int linearSearch(int a[], int size, int key){


int loc = 0;
while ((loc < size) && (key != a[loc]))
loc++;
if (loc < size)
return loc;
return -1
}

IIT Patna 4
Time complexity of linear search
• This is measured using the number of basic operations that needs to be per-
formed before termination
• Example: Number of comparisons that we perform here
• Assume that there are n element in the array
• Best case: match found in the first comparison. (1 comparison)
• Worst case: no match found or the last element matches (n comparisons)
• Average case: (n+1)
2 comparisons

IIT Patna 5
Binary search
• Binary search is applicable if the array is sorted
• Basic idea
• Look for the key at the mid position of the array
• If it matches, then we are done. If key is less than mid element, then we
need to search on the left of array. Otherwise, right half of the array will be
looked into
• In every step, we reduce the number of elements by a factor of 2

IIT Patna 6
Basic strategy
• Initially the search window is the whole array, that is, between L & R
• We compare the key with the middle element and decide which side to search

<= key > key


L R

(L+R)/2

IIT Patna 7
Binary search
int bsearch(int x[], int size, int key){
int l = 0, r = size - 1, mid;
while (l != r){
mid = (l+r)/2;
if(key <= x[mid]) r = mid;
else l = mid+1;
}
if(key == x[l]) return l;
else return -1;
}

IIT Patna 8
Binary search

-10 -4 2 3 8 12 17 25 27

0 1 2 3 4 5 6 7 8

bsearch(x, 9, 2)
L R mid condition
0 8 4 8 <= 2
0 4 2 2 <= 2
0 2 1 -4 <= 2
2 2 Terminated
x[2] == 2 ? TRUE

IIT Patna 9
Binary search

-10 -4 2 3 8 12 17 25 27

0 1 2 3 4 5 6 7 8

bsearch(x, 9, 2) bsearch(x, 9, 1)
L R mid condition L R mid condition
0 8 4 8 <= 2 0 8 4 8 <= 1
0 4 2 2 <= 2 0 4 2 2 <= 1
0 2 1 -4 <= 2 0 2 1 -4 <= 1
2 2 Terminated 2 2 Terminated
x[2] == 2 ? TRUE x[2] == 1 ? FALSE

IIT Patna 9
Binary search: alternative version
int bsearch(int x[], int size, int key){
int l = 0, r = size - 1, mid;
while (l <= r){
mid = (l+r)/2;
if(key == x[mid]) return mid;
if(key < x[mid]) r = mid - 1;
else l = mid+1;
}
return -1;
}

IIT Patna 10
Binary search: recursive version
• The algorithm is called recursively by adjusting the left or right pointers as ap-
plicable
• The base condition is: the element is found, or the left and right pointers cross
int bsearch(int x[], int l, int r, int key){
int mid;
if (l <= r){
mid = (l+r)/2;
if(key == x[mid]) return mid;
if(key < x[mid]) return bsearch(x, l, mid-1, key);
else return bsearch(x, mid+1, r, key);
}
return -1;
}

IIT Patna 11
Time complexity
• Consider an array of size 1000 sorted in increasing order
• Linear search: need to check every element, in the worst case we need to per-
form 1000 comparisons
• Binary search: first we compare with 500th element and discard 500 elements,
next we discard 250 elements, and so on. Roughly, in 10 steps you end up with
a single element (210 = 1024). Approximately 10 comparisons are need in
the worst case
• If size of the array in n, linear search will need n comparisons, and binary search
will require ⌈log2n ⌉ comparisons

IIT Patna 12

You might also like