Documente Academic
Documente Profesional
Documente Cultură
1-1
Using Figure 7.1 as a model, illustrate the operation of PARTITION on the array A = [13, 19,
3, 5, 12, 8, 7, 4, 21, 2, 6, 11].
Answer
12
21
A[1]
21
12
A[2]
21
12
A[3]
11
19
13
14
11
19
13
14
11
19
13
14
12
21
A[4]
12
11
19
13
14
21
11
19
13
14
21
11
19
13
14
11
21
19
13
14
A[5] x
5
12
A[5]
12
A[6]
12
11
21
19
13
14
11
13
21
19
14
11
13
21
19
14
13
21
19
14
11
13
21
19
14
14
19
21
A[8] x
5
12
A[7]
12
12
12
A[8]
11
A[9]
6
hi
A[
12
11
13
12
11
13
Devide
5
A[
lo
.... m-1]
14
A[ m ]
19
21
A [m+1....
h1
Conquer
5
A[
12
lo
11
13
11
12
13
11
12
13
.... m-1]
19
21
A [m+1....
19
h1
21
Combine
2
14
19
21
Exercises 7.1-2
What value of q does PARTITION return when all elements in the array A[p...r] have the
same value? Modify PARTITION so that q = (p+r)/2 when all elements in the array A[p...r]
have the same value.
Answer
It will return r. So we have to modify the code in case the worst situation.
code
( isi code :
#!/usr/bin/env python
# coding=utf-8
if items[j] <= x:
i=i+1
items[i],items[j] = items[j],items[i]
items[i+1],items[r] = items[r],items[i+1]
return i+1-count/2
items = [2,5,9,3,7,0,-1]
quicksort(items, 0, len(items)-1)
print items )
Jawaban lain ( menyambut dengan jawaban soal 7.2.2 ):
2. It returns r.
We can modify PARTITION by counting the number of comparisons in which A[j]=A[r]
and then subtracting half that number from the pivot index.
Python code
def partition(numbers, start = 0, end = None):
last = end - 1 if end else len(numbers) - 1
pivot_value = numbers[last]
pivot
= start
repetitions = 0
for i in range(start, last):
value = numbers[i]
if value == pivot_value:
repetitions += 1
if value <= pivot_value:
numbers[pivot], numbers[i] = numbers[i], numbers[pivot]
pivot += 1
numbers[pivot], numbers[last] = numbers[last], numbers[pivot]
return pivot - repetitions // 2
def quicksort(numbers, start = 0, end = None):
end = end if end else len(numbers)
if start < end - 1:
pivot = partition(numbers, start, end)
quicksort(numbers, start, pivot)
quicksort(numbers, pivot + 1, end)
Exercises 7.1-3
Give a brief argument that the running time of PARTITION on a subarray of size n is (n).
Answer
Exercise 7.21
Use substitution method to prove that the recurrence T (n) = T (n1)+
solution T (n) =
( n
).
Solution:
We guess that T (n)
( n2 ).
T (n)
2
c 1 (n1) + + c 0 n
c1
Thus T (n)
T (n)
c1 ( n1)2 + (n)
(
2
2n + 1) +
c 1 c 0 )n + c 1
c1 n
c 1 n 2 for n0
(2
c0 n
1 and c 0 > c 1
( n2 ).
Exercise 7.2.2
What is the running time of QUICKSORT when all elements of the array A have the same
value?
It is (n2), since one of the partitions is always empty (see exercise 7.1.2).
Exercise 7.2.3
Show that the running time of QUICKSORT is (n2) when the array A contains distict
elements and is sorted in decreasing order.
In this case PARTITION always returns p because all the elements are greated than the pivot.
While the if will never be executed, we still get one empty partition and the recurrence is
T(n)=T(n1)+(n) (even if its body is not executed, the for is still (n)).