Problem Set 6, Part I
Problem 1: Sorting practice {14, 7, 27, 13, 24, 20, 10, 33}
1-1) {7, 10, 27, 13, 24, 20, 14, 33}
1-2) {7, 13, 14, 24, 27, 20, 10, 33}
1-3) {7, 13, 14, 10, 20, 24, 27, 33}
1-4) {10, 7, 13, 27, 24, 20, 14, 33}
1-5) {7, 10, 13, 27, 24, 20, 14, 33}
1-6) {7, 13, 14, 27, 20, 24, 10, 33}
Problem 2: More practice with Big-O
2-1)
a) O(n)
b) O(n^3)
c) O(nlog(n))
d) O(log(n))
e) O(n^2)
2-2) count() is called 2n*(n-2) times. Expanding 2n*9n-2) out, we see that
count() is called 2(n^2)-4n times. Therefore, the code fragment has O(n^2)
run-time efficiency.
2-3) count() is called 4*(n-1)log(n) times. Expanding this expression out, we
see that 4n*log(n)-4*log(n). Therefore, the code fragment has O(nlog(n))
run-time efficiency.
Problem 3: Comparing two algorithms
worst-case time efficiency of algorithm A:
Explanation: The worst-case time efficiency of algorithm A is O(nlog(n)) because the
worst-case time-efficiency of merge sort is O(nlog(n)).
worst-case time efficiency of algorithm B:
Explanation: The worst-case time efficiency of algorithm B is O(n) because the algorithm only
makes one pass through the array every time it is run and therefore the max amount of times it
can call the line smallest = arr[i] is n times. The worst case is that this line is called for all
indexes i because every term in the array is smaller than the term in the index before it.
Problem 4: More practice with sorting - Quicksort {14, 7, 27, 13, 24, 20, 10, 33}
4-1){7, 10, 13, 14, 24, 20, 27, 33}
4-2) {7, 10, 13, 14, 20, 24, 27, 33}
Problem 5: More practice with sorting - Mergesort
5-1) {3, 24, 13, 27, 34, 2, 50, 12}
5-2) {3, 13, 24, 27, 2, 34, 50, 12}
5-3) {3, 13, 24, 27, 34, 2, 50, 12}
Problem 6: Practice with references
6-1)
Expression
Address
Value
n
Ox100
Ox712
n.ch
Ox712
‘n’
n.prev
Ox718
Ox064
n.prev.prev
Ox070
Ox360
n.prev.next.next
Ox066
null
n.prev.prev.next
Ox362
Ox064
6-2)
x.prev = n.prev;
x.next = n;
n.prev.next = x;
n.prev =x;