Homework Week 2 Big Oh
Homework Week 2 Big Oh
1. How do you use Big O notation to analyze the time complexity of an algorithm?
Why is Big O analysis important?
1. Algorithm 1:
1 def deductOne ( num ) :
2 num -= 1
3 return num
2. Algorithm 2:
1 def s ( _list ) :
2 prefix_sum = 0
3 for i in _list :
4 prefix_sum + = 1
5 return prefix_sum
3. Algorithm 3:
Data Structures and Algorithms Homework Week 1
1 testList = [1 , 3 , 4 , 5 , 61 , 96 , 69 , 13 , 22 , 2001]
2
3 def al ( testList ) :
4 for i in range ( len ( testList ) ) :
5 for j in range ( i +1 , len ( testList ) ) :
6 if testList [ j ] < testList [ i ]:
7 testList [ j ] , testList [ i ] = testList [ i ] , testList [ j ]
8 return testList
4. Algorithm 4:
1 a = 100
2 N = 40
3
4 def add (a , N ) :
5 for i in range ( N ) :
6 a = a + 10
7 return a
5. Algorithm 5:
1 def count (A , B ) :
2 n = len ( A )
3 count = 0
4 for i in range ( n ) :
5 total = 0
6 for j in range ( n ) :
7 for k in range (1+ j ) :
8 total += A [ k ]
9 if B [ i ] == total :
10 count += 1
11 return count
6. Algorithm 6:
1 def fibo ( n ) :
2 if n <= 1:
3 return n
4 return fibo (n -1) + fibo (n -2)
2. List 2:
1 a = "f"
2 list = [ 'a ' , 'b ' , 'c ' , 'd ' , 'e ' , 'f ' , 'g ' , 'h ' , 'i ' , 'j ' , 'k ' , 'l ' ,
'm ' , 'n ' , 'o ' , 'p ' , 'q ' , 'r ' , 's ' , 't ' , 'u ' , 'v ' , 'w ' , 'x ' , 'y '
, 'z ']
2
Data Structures and Algorithms Homework Week 1
3. List 3:
1 a = 5
2 list =[2 , 1 , 2163 , -125 , 1 , 5 , 7 , 9 , 6 , 5 , 3 , 11 , 22 , 55 , 88 , 99 ,
111 , 222 , 555 , 321 , 653 , 963 , 654 , 998 , 651]
4. List 4:
1 a = " foo "
2 list = [ ' aac ' , ' bac ' , ' cbaasc ' , ' duc ' , ' enk ' , ' fire ' , ' foo ' , ' hv ' ,
' isg ' , ' loj ' , ' qwek ' , ' scsl ' , ' vfbgm ' , ' nig ' , ' go ' , ' prii ' , '
qqqq ' , ' kkree ' , ' lis ' , ' acst ' , ' qweu ' , ' vfsdv ' , ' wfefe ' , ' xsfed '
, ' yagr ' , ' zyz ']
• Your submission will be graded and it is likely that homework grade will contribute
as a component in your GPA.
• If your submission is later than the due date without special consideration approval,
you will receive a penalty on your mark.