Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def...
Transcript of Efficiency and Computational Complexity (Part 2)pkalra/col100/slides/11...35 Partitioning def...
Efficiency and Computational Complexity
(Part 2)
Rate of growth of functions
2 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/
3 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/
4 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/
About Big O notation
5 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/
About Big O notation
6 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/
About Big O notation
7 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/
About Big O notation
8 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/
About Big O notation
9 Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/
About Big O notation
10
Merging two sorted lists (arrays)
Given two sorted arrays, merge them to get a single ordered array
p
q
p+q
Reference RG Dromey book
11
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
c
1 2 3 0
k
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
B[j] < A[i]
12
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 c
1 2 3 0
k
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
B[j] < A[i] C[k]=B[j]
13
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 c
1 2 3 0
k
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
j=j+1 k=k+1
14
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 c
1 2 3 0
k
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
B[j] < A[i] C[k]=B[j]
15
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 c
1 2 3 0
k
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
j=j+1 k=k+1
16
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 c
1 2 3 0
k
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
A[i] < B[j] C[k]=A[i]
17
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 c
1 2 3 0
k
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
i=i+1 k=k+1
18
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
B[j] < A[i] C[k]=B[j]
19
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
j=j+1 k=k+1
20
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k 17
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
B[j] < A[i] C[k]=B[j]
21
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k 17
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
j=j+1 k=k+1
22
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k 17 18
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
A[i] < B[j] C[k]=A[i]
23
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k 17 18
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
i=i+1 k=k+1
24
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k 17 18 42
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
A[i] < B[j] C[k]=A[i]
25
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k 17 18 42
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
i=i+1 k=k+1
26
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k 17 18 42 44
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
B[j] < A[i] C[k]=B[j]
27
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k 17 18 42 44
5 6 7 4 9 10 11 8
Merging two sorted lists (arrays)
j=j+1 k=k+1
28
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k 17 18 42 44
5 6 7 4
51
9 10 11 8
Merging two sorted lists (arrays)
A[i] < B[j] C[k]=A[i]
29
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k 17 18 42 44
5 6 7 4
51
9 10 11 8
Merging two sorted lists (arrays)
k=k+1
30
8 11 16 17 44 b
1 2 3 4 0
15 18 42 51 a
1 2 3 0
58 71 74
6 7 5
i
j
8 11 15 16 c
1 2 3 0
k 17 18 42 44
5 6 7 4
51 58 71 74
9 10 11 8
Merging two sorted lists (arrays)
copy the remaining B[j] to C[k]
31
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 else: C.append(B[j]) j+=1 C += A[i:] C += B[j:] return C
Order of time complexity O(p+q) p=len(A) q=len(B) O(n)
32
Partitioning
Given a randomly ordered array of n elements, partition the elements into two subsets such that elements <= x are in one subset and elements > x are in the other set.
x = 17
Reference RG Dromey book
33
Partitioning
Note: In place partitioning another array/list not allowed Approach:
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] arr[j] = temp i+=1 j-=1 while arr[i] <= x: i += 1 while arr[j] > x: j -= 1 return j
35
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
Order of time complexity O(n)