02.introduction Linear+Binar Search
02.introduction Linear+Binar Search
1
Definition
Human Genome
Project(Bioinformatics)
Internet – Information
management/ route
selection/search engines
Electronic Commerce –
Public/Private Key Cryptography
Resource allocation- Air flight route
selection, Inventory system etc.
and a lot more.
Why Efficient
Algorithm?
Time to sort n items:
Suppose,
Computer A = 10 Billion Instruction/second
1010]
[
C1 < C2 ; C1 =2
;
C2 = 50
To sort 10 million items
A takes:
2 X (107) 2 instructions
1010 instructions/Second
=
20,000 sec (more than 5.5 hours)
B takes:
50 X 107 X lg(107) instructions
107 instructions/Second
=
1163 sec (less than 20 min)
So, with an efficient algorithm we can solve a problem within a very short time,
even with a slow compiler.
Searching Algorithm
2. Binary Search
1. Sequential Search
Let,
Sequential list (array): X1,X2,…..,Xn
*
X 2 66 11 23 24 88 64 77 108 290
Search object: Z = 24
Algorithm:
i 1
while i < n & Z != X[i] do
i i+1
if i < n then
FOUND
else
NOT FOUND
2. Binary Search
l 1 [start position]
h n [no. of elements]
flag = false
while l < h and not flag do
l+h
mid
2
Xm X[mid]
case
Xm < Z : l mid + 1
Xm > Z : h mid - 1
Xm = Z : flag true
if flag = = true
FOUND
else
NOT FOUND
Complexity
Sequential Search:
Worst Case Complexity: O(n)
Best Case Complexity: O(1)
Average Case Complexity: (n+1)/2
== O(n)
Binary Search
running time of binary search
could be written as O(log n)
Question
Ifwe have an array of length
70000000 but sorted, which
searching algorithm will work
fast and why??