Week-4 Sorting, Dictionaries and Functions
Week-4 Sorting, Dictionaries and Functions
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 4, Lecture 1
Move it into C
Merging A and B
Merging two sorted lists
32 74 89
21 55 64
Merging two sorted lists
32 74 89
21 55 64
21
Merging two sorted lists
32 74 89
21 55 64
21 32
Merging two sorted lists
32 74 89
21 55 64
21 32 55
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64 74
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64 74 89
Merge Sort
Sort A[0:n//2]
Sort A[n//2:n]
43 32 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
22 32 43 78 63 57 91 13
32 43 22 78 57 63 13 91
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
22 32 43 78 13 57 63 91
32 43 22 78 57 63 13 91
43 32 22 78 63 57 91 13
Merge Sort
13 22 32 43 57 63 78 91
22 32 43 78 13 57 63 91
32 43 22 78 57 63 13 91
43 32 22 78 63 57 91 13
Divide and conquer
If n is 1, nothing to be done
Otherwise
mid = (left+right)//2
L = mergesort(A,left,mid)
R = mergesort(A,mid,right)
return(merge(L,R))
NPTEL MOOC
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 4, Lecture 2
Move it into C
Merging A and B
Analysis of Merge
How much time does Merge take?
Size of C is m+n
m+n ≤ 2 max(m,n)
If n is 1, nothing to be done
Otherwise
T(n) = 2T(n/2) + n
T(n) = 2T(n/2) + n
2 2
= 2 [ 2T(n/4) + n/2 ] + n = 2 T(n/2 ) + 2n
2 3 2 3 3
= 2 [ 2T(n/2 ) + n/2 ] + 2n = 2 T(n/2 ) + 3n
…
j j
= 2 T(n/2 ) + jn
j j
When j = log n, n/2 = 1, so T(n/2 ) = 1
Inherently recursive
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 4, Lecture 3
Inherently recursive
No need to merge!
Divide and conquer without merging
Suppose the median value in A is m
43 32 22 78 63 57 91 13
Quicksort
High level view
43 32 22 78 63 57 91 13
Quicksort
High level view
43 32 22 78 63 57 91 13
Quicksort
High level view
13 32 22 43 63 57 91 78
Quicksort
High level view
13 22 32 43 57 63 78 91
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 13 63 57 91 78
Quicksort: Partitioning
13 32 22 43 63 57 91 78
Quicksort in Python
def Quicksort(A,l,r): # Sort A[l:r]
if r - l <= 1: # Base case
return ()
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 4, Lecture 4
Spreadsheets
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 4, Lecture 5
l : {0,1,..,4} ⟶ integers
test1["Dhawan"] = 84
test1["Pujara"] = 16
test1["Kohli"] = 200
Python dictionary
Initialization: test1 = {}
score["Test1"]["Dhawan"] = 84
score["Test1"]["Kohli"] = 200
score["Test2"]["Dhawan"] = 27
d = {}
d[0] = 7 # No problem, d == {0:7}
… unlike a list
l = []
l[0] = 7 # IndexError!
Summary
Dictionaries allow a flexible association of values to
keys
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 4, Lecture 6
def power(x,n):
ans = 1
for i in range(0,n):
ans = ans*x
return(ans)
Call power(n=5,x=4)
Default arguments
Recall int(s) that converts string to integer
int("76") is 76
Order is important
Function definitions
def associates a function body with a name
if condition:
def f(a,b,c):
. . .
else:
def f(a,b,c):
. . .
Function definitions
def f(a,b,c):
. . .
g = f
Apply f to x n times
625
Passing functions
Useful for customizing functions such as sort
def sortfunction(l,cmpfn=defaultcmpfn):
Summary
PROGRAMMING,
DATA STRUCTURES AND
ALGORITHMS IN PYTHON
Week 4, Lecture 7
for x in l:
x = f(x)
def applylist(f,l):
for x in l:
x = f(x)
Built in function map()
map(f,l) applies f to each element of l
for i in map(f,l):
primelist = []
for i in numberlist:
if isprime(i):
primelist.append(i)
return(primelist)
Selecting a sublist
In general
def select(property,l):
sublist = []
for x in l:
if property(x):
sublist.append(x)
return(sublist)
list(map(square,filter(iseven,range(100))
def square(x):
return(x*x)
def iseven(x):
return(x%2 == 0)
List comprehension
Pythagorean triple: x2 + y2 = z2
{ (x,y,z) | 1 ≤ x,y,z ≤ n, x2 + y2 = z2 }
Extend to lists
List comprehension
for x in range(100):
for y in range(100):
for z in range(100):
Multiple generators
Initialise a 4 x 3 matrix
4 rows, 3 columns
Stored row-wise
l = [ [ 0 for i in range(3) ]
for j in range(4)]
Warning
What’s happening here?
>>> l[1][1] = 7
>>> l
[[0,7,0],[0,7,0],[0,7,0],[0,7,0]]