Sunteți pe pagina 1din 7

Exercises 7.

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

( akan tetapi soal no 7.1.1 kita diubah menjadi :


Buat barisan bilangan random berukuran 12 setiap kelompok harus berbeda. Dalam contoh
quicksort sampai selesai : devide, conquer, dan combine )
Misalkan
5

12

21

A[1] x, maka A[1]

A[1]

21

12

A[2] x, maka A[2]

A[2]

21

12

A[3] x, maka A[3]

A[3]

11

19

13

14

11

19

13

14

11

19

13

14

12

21

A[4] x, maka A[4]

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[6] x, maka A[6]

A[5]

12

A[7] x, maka A[7]

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[9] x, maka A[9]

A[7]

12

A[10] x, maka A[10]


5

12

A[11] x, maka A[11]


5

12

End for loop A[14]


5

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

def quicksort(items, p, r):


if p < r:
q = partition(items, p, r)
quicksort(items, p, q-1)
quicksort(items, q+1, r)

def partition(items, p, r):


x = items[r]
i = p-1
count = 0

for j in range(p, r):


if items[j] == x:
count += 1

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

Because we just iterate the array once.

Exercise 7.21
Use substitution method to prove that the recurrence T (n) = T (n1)+
solution T (n) =

( n

(n) has the

).

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 ).Similarly, we can prove that T (n) ( n2 ). Consequently,

( 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)).

S-ar putea să vă placă și