Insertion and Merge Sort
Insertion and Merge Sort
9 2 4 10 5
𝑗≥1
while _________:
if
while :
if
else:
break
Running Time
for in range(len()):
while :
if • Let be the running time
of this code.
else: • Calculate how many time
break this code is executed.
• This time is roughly
while :
if • Let be the running time
of this code.
else: • Calculate how many time
break this code is executed.
• This time is roughly
Running Time
for in range(len()):
while :
if
else:
break
Insertion Sort
Given two sorted arrays and , design an algorithm that sorts the elements
of and .
InsertionSort()
Running Time
Did not even use the fact that the arrays are sorted.
𝐴 𝐵
2 7 9 15 25 1 3 4 8
𝐶
def merge:
while and :
if :
else:
def merge:
while and :
if :
else:
while :
while :
def merge:
while and :
if :
else:
while :
while :
return
print(merge())
def merge:
What is the running time of this algorithm?
while and :
if :
else:
else:
times
while :
return
This code is executed times.
def merge:
Running time of Merge
while and :
if :
else:
while :
while :
return
15 8 1 9 2 6 3 11
Recursion
15 8 1 9 2 6 3 11
def merge: def mergeSort:
if
return
mergeSort(//)
while and :
if :
mergeSort(//
return merge(
else:
while :
while :
return
def mergeSort:
if
return
mergeSort(//)
mergeSort(//
return merge(