You are on page 1of 30

Sorting

max_index = 0;
for in range(len()):
if > A[max_index ]:
max_index =

What is the algorithm doing?

Finding the maximum in an array


= 0; m 𝑎𝑥 𝑖𝑛𝑑𝑒𝑥 =0
for in range(len()):
if > :
m 𝑎𝑥𝑖𝑛𝑑𝑒𝑥 ⁡=1
= m 𝑎𝑥𝑖𝑛𝑑𝑒𝑥 ⁡=3
2 9 4 10 5

𝑖=0𝑖=1 𝑖=2𝑖=3 𝑖=4


• Finding the maximum in an array
• And swap it with the last element in

= 0;
for in range(len()):
if > :
=
= 0;
for in range(len()):
if > :
=

2 9 4 10 5

max ⁡𝑖𝑛𝑑𝑒𝑥 =3
• Finding the maximum in an array
• And swap it with the last element in

Can you now think how to sort this array?

Using Recursion!!!
def mySort():
= 0;
for in range(len()):
if > :
=

mySort([:len()-1]

What is wrong about this program?

• No Stopping condition
• We are creating a new array in each recursive call of mySort
def mySort(,):
if == 1:
return
= 0;
for in range():
if > :
=

mySort()

This is the recursive implementation of the famous

BubbleSort Algorithm
A recursive algorithm can be written in a non-recursive manner.

Let us try to write a non-recursive version of BubbleSort with some


optimizations
def bubbleSort():

for in range(len()-1):
if > :
=

2 9 4 10 5

𝑖
def bubbleSort():
for in :
for in range(()- ): 1𝑗
if > :
=

BubbleSort Algorithm
Let us do some improvisations in this algorithm.
def bubbleSort():
for in range(1,len())
swap = False
for in range(len()-):
if > :
=
swap = True
if swap == False:
return

If there is no swapping done in an iteration of the bubble sort, then the


array is already sorted.
def bubbleSort():
for in range(1,len())
swap = False
for in range(len()-):
if > :
=
swap = True
if swap == False:
return

2 4 5 10 9

𝑗=1
𝑗=2 No swap
Running Time of An Algorithm
def bubbleSort():
for in range(1,len())
swap = False
for in range(len()-):
if > :
=
swap = True
if swap == False:
return

What is the running time of this algorithm?

Run this algorithm on an input and check the running time.


def bubbleSort():
for in range(1,len())
swap = False
for in range(len()-):
if > :
=
swap = True
if swap == False:
return

What is the best input of size 3 for this algorithm?

1 2 3

What is the worst input of size 3 for this algorithm?

3 2 1
Running time on the best input

Time Running time on the worst input

Average running time

𝑛
1 2 3 4 5 6 7 8
Time Running time on the worst input

What is the equation of the


curve?

This equation tells us how our


program behaves

𝑛
1 2 3 4 5 6 7 8
Problems with finding running time like this?

• Tedious
• Machine dependent. Different users will come up with different
running times.

Fortunately, there is an alternate method.


max_index = 0; 𝑐1
for in range(len()): 𝑐 2𝑛
if > A[max_index ]: 𝑐 3𝑛
max_index = 𝑐 4𝑛
We will calculate the running time of the algorithm manually on the worst
input.

Running time
(To simplify, each )
(Assuming )
(To simplify, set )
Time Running time on the worst input

24

20

16

12

𝑛
1 2 3 4 5 6 7
def bubbleSort(): 𝑐 (𝑛 −1)
for in range(1,len())
swap = False
𝑐 (𝑛 −1)
for in range(len()-): 𝑐 (𝑛 −1)
(𝑛 −1)
if > : 𝑐 (𝑛 −1)(𝑛 −1)
= 𝑐 (𝑛 −1)(𝑛 −1)
swap = True 𝑐 (𝑛 −1)(𝑛 −1)
if swap == False: 𝑐 (𝑛 − 1 )
return 0

Running Time
Time

Running time on the worst input

𝑛
1 2 3 4 5 6 7
def bubbleSort(): 𝑐 (𝑛 −1)
for in range(1,len())
swap = False
𝑐 (𝑛 −1)
for in range(len()-): 𝑐 (𝑛 −1)
(𝑛 −2)
if > : 𝑐 (𝑛−1)(𝑛 −2)
= 𝑐 (𝑛−1)(𝑛 −2)
swap = True 𝑐 (𝑛−1)(𝑛 −2)
if swap == False: 0
return 0

Problems with finding running time like this?

• But even this running time is tedious to calculate.


• We are lazy and do not want to devote this much amount of time to
calculate the running time.
2
2𝑛 Running time on the worst input
Time A curve dominates if it is
above for all values 2
𝑛 +2 𝑛+2

𝑛
dominates for all values
dominates for all values

Definition: If dominates for all values , then we say

2 2
𝑛 + 2 𝑛 + 2= 𝑂 ( 𝑛 )
Definition: If dominates for all values , then we say

Is ?

dominates for all values

Is ?

This would imply that there is a constant such that


(for all values )

But this is impossible.


Definition: If dominates for all values , then we say

Observation:
Just look at the highest order term of . Lower order terms of do not matter
for the order notation.
def bubbleSort(): 𝑐 (𝑛 −1)
for in range(1,len())
swap = False
𝑐 (𝑛 −1)
for in range(len()-): 𝑐 (𝑛 −1)
(𝑛 −2)
if > : 𝑐 (𝑛−1)(𝑛 −2)
= 𝑐 (𝑛−1)(𝑛 −2)
swap = True 𝑐 (𝑛−1)(𝑛 −2)
if swap == False: 0
return 0
Running Time

Running Time

• Do rough calculation --- not exact.


• There are two for loops in the algorithm both roughly iterating from 1 to .
• So, we deduce that the running time is term without even calculating it exactly.
• Once we have deduced this, the running time of the algorithm is

You might also like