There are numbers behind the doors. You are asked to find if the number 11 is behind any of the doors
Given an array ofN
items, and atarget
to search For every positioni
in the array { if the item as positioni
is our target{ Finish! We found our target! } }
Search for number
Given an arrayA
ofN
items, and atarget
to search Letstart
= 0,end
=N
-1 whiletarget
not found{ Setmid
= (start
+end
)/2 ifA[mid]
is our target{ Finish! We found our target! } else{ ifA[mid]
< target{ setstart
=mid
+ 1 } else ifA[mid]
> target{ setend
=mid
- 1 } } }
Search for number
Given an array of N
items, and a target
that is not in the array
For Linear Search:
Given an array of N
items, and a target
that is not in the array
For Binary Search:
In this case, the operations we measure are the number of comparisons
The items need to be comparable
The items need to be comparable
The items need to be comparable
Start with an unsorted arrayA
withN
items while the array is unsorted{ For everyposition
between 1 andN
{ ifA[i]
andA[i-1]
are not in the correct order{ SwapA[i]
andA[i-1]
} } }
Start with an unsorted arrayA
withN
items For everypos
between 1 andN-1
{ Letj
=pos
LetITEM
=A[pos]
Whilej
> 0 andITEM
is greater thanA[j -1]
{ SetA[j]
=A[j-1]
Decreasej
by 1 } SetA[j]
=ITEM
}
Why is insertion sort faster?
Can we do better?
Idea 1: instead of sorting a list of size N
, sort two lists of size N/2
and merge them together
Idea 1: instead of sorting a list of size N
, sort two lists of size N/2
and merge them together
Idea 2: instead of sorting a list of size N/2
, sort two lists of size N/4
and merge them together
Idea 1: instead of sorting a list of size N
, sort two lists of size N/2
and merge them together
Idea 2: instead of sorting a list of size N/2
, sort two lists of size N/4
and merge them together
Idea 3: instead of sorting a list of size N/4
, sort two lists of size N/8
and merge them together
Idea 1: instead of sorting a list of size N
, sort two lists of size N/2
and merge them together
Idea 2: instead of sorting a list of size N/2
, sort two lists of size N/4
and merge them together
Idea 3: instead of sorting a list of size N/4
, sort two lists of size N/8
and merge them together
... ... ...
Idea K: instead of sorting a list of size 2
, sort two lists of size 1
and merge them together
Idea 1: instead of sorting a list of size N
, sort two lists of size N/2
and merge them together
Idea 2: instead of sorting a list of size N/2
, sort two lists of size N/4
and merge them together
Idea 3: instead of sorting a list of size N/4
, sort two lists of size N/8
and merge them together
... ... ...
Idea i : instead of sorting a list of size N/(2^(i-1))
, sort two lists of size N/(2^i)
and merge them together
... ... ...
Idea K: instead of sorting a list of size 2
, sort two lists of size 1
and merge them together
Start with TWO sorted arrays:A
withN
items, andB
withM
items Create an empty arrayR
with sizeN+M
Leti
= 0, andj
= 0 Whilei
+j
<N
+M
{ ifi
==N
{ // Reached the end of array AR[i+j]
=B[j]
j
=j
+ 1 } else ifj
==M
{ // Reached the end of array BR[i+j]
=A[i]
i
=i
+ 1 } else ifA[i]
≤B[j]
{R[i+j]
=A[i]
i
=i
+ 1 } else ifA[i]
≥B[j]
{R[i+j]
=B[j]
j
=j
+ 1 } }
A:
B:
Merged result:
/