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

Insertion and Merge Sort

The document describes the merge sort algorithm. It explains that merge sort works by recursively splitting an array in half and then merging the sorted halves. The running time of merge sort is O(n log n) as each recursion level takes linear time and there are log n recursion levels.

Uploaded by

borateswayam8
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Insertion and Merge Sort

The document describes the merge sort algorithm. It explains that merge sort works by recursively splitting an array in half and then merging the sorted halves. The running time of merge sort is O(n log n) as each recursion level takes linear time and there are log n recursion levels.

Uploaded by

borateswayam8
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Sorting

9 2 4 10 5

Algorithm (in English)


• Put the next number in it correct place in the sorted array using swaps

Implement this algorithm in python.


Assume that array is already sorted.
for in range(len()):

𝑗≥1
while _________:
if

1 2 3 4 5 After comparing 4 and 5, there was no


need to compare 3 and 4 as the array is
𝑗=2 𝑗=3𝑗=4 already sorted.
for in range(len()):

while :
if

else:
break

What is the best input for this algorithm?


1 2 3

What is the worst input for this algorithm?


3 2 1
for in range(len()): 𝑐𝑛
𝑐𝑛
while : 𝑐𝑛×2 𝑖 ≈ 𝑐 𝑛 2
if 𝑐 𝑛2
𝑐𝑛
else: 0
break 0
2
𝑐𝑛
What is the running time of this algorithm?

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

A quicker way to find the running time of the algorithm.


• 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

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.

Can we design a better algorithm for this problem?


Given two sorted arrays and , design an algorithm that sorts the elements of
and .

𝐴 𝐵
2 7 9 15 25 1 3 4 8

𝑖=0𝑖=1𝑖=2𝑖=3𝑖=4𝑖=5 𝑗=0𝑗=1𝑗=2𝑗=3 𝑗=4

𝐶
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:

while : • Let us assume both these code blocks take


time.
while :
• We just need to calculate how many time
these code blocks are executed.
• This will give us our running time.
return
def merge:
Everytime this code is executed,
increments by 1.
while and :
if : How many times can increment?

else:
times
while :

Everytime this code is executed,


while : increments by 1.

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

• Let us assume that some how we sort these two arrays.


• Then, we can use merge to get the final sorted array

But how do you sort the array of size 4?

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(

What is the running time of MergeSort?

How to calculate the running time of a recursive function?


def mergeSort: 𝑇 (𝑛)
if
return
mergeSort(//)
mergeSort(//
2𝑇 ( )
𝑛
2
return merge( 𝑐𝑛

Let us assume that is the running time of MergeSort of an array of size .

Then, what is the running time of MergeSort on two halfs?


def mergeSort: 𝑇 (𝑛)
if
return
mergeSort(//)
mergeSort(//
2𝑇 ( )
𝑛
2
return merge( 𝑐𝑛

The running time of MergeSort can be represented as:


Recurrence Relation
Can you see the pattern?
• But cannot be arbitrarily large.
• Specifically, when , then we hit the base case.
Running Time of MergeSort

You might also like