Python Lecture 11-Efficiency
Python Lecture 11-Efficiency
Computational Complexity
(Part 2)
Rate of growth of functions
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-
2
science-and-programming-in-python-fall-2016/lecture-slides-code/
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-
3
science-and-programming-in-python-fall-2016/lecture-slides-code/
About Big O notation
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-
4
science-and-programming-in-python-fall-2016/lecture-slides-code/
About Big O notation
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-
5
science-and-programming-in-python-fall-2016/lecture-slides-code/
About Big O notation
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-
6
science-and-programming-in-python-fall-2016/lecture-slides-code/
About Big O notation
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-
7
science-and-programming-in-python-fall-2016/lecture-slides-code/
About Big O notation
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-
8
science-and-programming-in-python-fall-2016/lecture-slides-code/
About Big O notation
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-
9
science-and-programming-in-python-fall-2016/lecture-slides-code/
Merging two sorted lists (arrays)
Given two sorted arrays, merge them to
get a single ordered array
p+q
0 1 2 3
a 15 18 42 51
i
B[j] < A[i]
0 1 2 3 4 5 6 7
b 8 11 16 17 44 58 71 74
j
0 1 2 3 4 5 6 7 8 9 10 11
c
k
11
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
B[j] < A[i]
0 1 2 3 4 5 6 7 C[k]=B[j]
b 8 11 16 17 44 58 71 74
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8
k
12
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
0 1 2 3 4 5 6 7
b 8 11 16 17 44 58 71 74 j=j+1
k=k+1
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8
k
13
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
B[j] < A[i]
0 1 2 3 4 5 6 7 C[k]=B[j]
b 8 11 16 17 44 58 71 74
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11
k
14
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
0 1 2 3 4 5 6 7
b 8 11 16 17 44 58 71 74 j=j+1
k=k+1
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11
k
15
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
A[i] < B[j]
0 1 2 3 4 5 6 7 C[k]=A[i]
b 8 11 16 17 44 58 71 74
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15
k
16
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
0 1 2 3 4 5 6 7
b 8 11 16 17 44 58 71 74 i=i+1
k=k+1
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15
k
17
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
B[j] < A[i]
0 1 2 3 4 5 6 7 C[k]=B[j]
b 8 11 16 17 44 58 71 74
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16
k
18
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
0 1 2 3 4 5 6 7
b 8 11 16 17 44 58 71 74 j=j+1
k=k+1
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16
k
19
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
B[j] < A[i]
0 1 2 3 4 5 6 7 C[k]=B[j]
b 8 11 16 17 44 58 71 74
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16 17
k
20
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
0 1 2 3 4 5 6 7
b 8 11 16 17 44 58 71 74 j=j+1
k=k+1
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16 17
k
21
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
A[i] < B[j]
0 1 2 3 4 5 6 7 C[k]=A[i]
b 8 11 16 17 44 58 71 74
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16 17 18
k
22
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
0 1 2 3 4 5 6 7
b 8 11 16 17 44 58 71 74 i=i+1
k=k+1
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16 17 18
k
23
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
A[i] < B[j]
0 1 2 3 4 5 6 7 C[k]=A[i]
b 8 11 16 17 44 58 71 74
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16 17 18 42
k
24
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
0 1 2 3 4 5 6 7
b 8 11 16 17 44 58 71 74 i=i+1
k=k+1
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16 17 18 42
k
25
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
B[j] < A[i]
0 1 2 3 4 5 6 7 C[k]=B[j]
b 8 11 16 17 44 58 71 74
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16 17 18 42 44
k
26
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
0 1 2 3 4 5 6 7
b 8 11 16 17 44 58 71 74 j=j+1
k=k+1
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16 17 18 42 44
k
27
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
A[i] < B[j]
0 1 2 3 4 5 6 7 C[k]=A[i]
b 8 11 16 17 44 58 71 74
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16 17 18 42 44 51
k
28
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
0 1 2 3 4 5 6 7
b 8 11 16 17 44 58 71 74
k=k+1
j
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16 17 18 42 44 51
k
29
Merging two sorted lists (arrays)
0 1 2 3
a 15 18 42 51
i
0 1 2 3 4 5 6 7
b 8 11 16 17 44 58 71 74 copy the remaining
j B[j] to C[k]
0 1 2 3 4 5 6 7 8 9 10 11
c 8 11 15 16 17 18 42 44 51 58 71 74
k
30
Merging two sorted lists (arrays)
# Algorithm Merge
def merge(A, B):
C=[]
i,j=0,0
while ( (i<len(A)) and (j<len(B)) ):
if (A[i] <= B[j]):
C.append(A[i])
i+=1 Order of time complexity
else: O(p+q)
C.append(B[j])
j+=1
p=len(A)
q=len(B)
C += A[i:] O(n)
C += B[j:]
return C
31
Partitioning
x = 17
Approach:
33
Partitioning
def partition(arr,x):
i = 0 # leftcounter
j = len(arr)-1 # rightcounter
while i < j and arr[i] <= x: # postion for the first left counter
i += 1
while i < j and arr[j] > x: # position for the first right counter
j -= 1
if arr[j]>x: # special case when x is less than all elements
j -= 1
while i<j:
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
i+=1
j-=1
while arr[i] <= x: i += 1
while arr[j] > x: j -= 1
return j
34
Partitioning
def partition(arr,x):
i = 0 # leftcounter
j = len(arr)-1 # rightcounter
while i < j and arr[i] <= x: # postion for the first left counter
i += 1
while i < j and arr[j] > x: # position for the first right counter
j -= 1
if arr[j]>x: # special case when x is less than all elements
j -= 1
while i<j:
temp = arr[i]
arr[i] = arr[j] Order of time complexity
arr[j] = temp O(n)
i+=1
j-=1
while arr[i] <= x: i += 1
while arr[j] > x: j -= 1
return j
35