efficiency and computational complexity part 2 rate of
play

Efficiency and Computational Complexity (Part 2) Rate of growth of - PowerPoint PPT Presentation

Efficiency and 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


  1. Efficiency and Computational Complexity (Part 2)

  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/

  3. 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/

  4. 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/

  5. 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/

  6. 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/

  7. 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/

  8. 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/

  9. 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/

  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 10

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

  12. 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

  13. Merging two sorted lists (arrays) 0 1 2 3 a 15 18 42 51 i 0 1 2 3 4 5 6 7 j=j+1 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 k 13

  14. 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

  15. Merging two sorted lists (arrays) 0 1 2 3 a 15 18 42 51 i 0 1 2 3 4 5 6 7 j=j+1 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 k 15

  16. 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

  17. Merging two sorted lists (arrays) 0 1 2 3 a 15 18 42 51 i 0 1 2 3 4 5 6 7 i=i+1 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 k 17

  18. 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

  19. Merging two sorted lists (arrays) 0 1 2 3 a 15 18 42 51 i 0 1 2 3 4 5 6 7 j=j+1 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 k 19

  20. 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

  21. Merging two sorted lists (arrays) 0 1 2 3 a 15 18 42 51 i 0 1 2 3 4 5 6 7 j=j+1 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 k 21

  22. 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

  23. Merging two sorted lists (arrays) 0 1 2 3 a 15 18 42 51 i 0 1 2 3 4 5 6 7 i=i+1 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 k 23

  24. 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

  25. Merging two sorted lists (arrays) 0 1 2 3 a 15 18 42 51 i 0 1 2 3 4 5 6 7 i=i+1 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 k 25

  26. 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

  27. Merging two sorted lists (arrays) 0 1 2 3 a 15 18 42 51 i 0 1 2 3 4 5 6 7 j=j+1 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 k 27

  28. 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

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

  30. 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 B[j] to C[k] j 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

  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]) Order of time complexity i+=1 O(p+q) else: C.append(B[j]) p=len(A) j+=1 q=len(B) O(n) C += A[i:] C += B[j:] return C 31

  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 32

  33. Partitioning Note: In place partitioning another array/list not allowed Approach: 33

  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 34

  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] Order of time complexity arr[i] = arr[j] O(n) arr[j] = temp i+=1 j-=1 while arr[i] <= x: i += 1 while arr[j] > x: j -= 1 return j 35

Recommend


More recommend