Think Different Materi ASD 2014 pertemuan_23
SORTING (PENGURUTAN)
Quicksort Concept
• Basic Concept: divide and conquer
• Select a pivot and split the data into
two groups: (< pivot) and (> pivot):
pivot
( pivot)
RIGHT group
• Recursively apply Quicksort to the
subgroups
Quicksort Start
Start with all data
in an array, and
consider it
unsorted
Unsorted Array
Quicksort Step 1
pivot
Step 1, select a pivot
(it is arbitrary)
26
We will select the first
element, as presented in
the
original algorithm by
C.A.R. Hoare in 1962.
33
35
29
19
12
22
Quicksort Step 2
Step 2, start process of
dividing data into LEFT
and RIGHT groups:
The LEFT group will
have elements less than
the pivot.
The RIGHT group will have
elements greater that the
pivot.
Use markers left and right
pivot
26
33
left
35
29
19
12
22
right
Quicksort Step 3
Step 3,
If left element belongs
to LEFT group, then
increment
left index.
If right index element
belongs
to RIGHT, then
decrement right.
Exchange when you
find
elements that belong
to the other
group.
pivot
26
33
left
35
29
19
12
22
right
Quicksort Step 4
pivot
Step 4:
Element 33
belongs
to RIGHT
group.
26
35
29
19
12
left
Element 22
belongs
to LEFT group.
Exchange the
two
elements.
33
22
right
pivot
26
22
left
35
29
19
12
33
right
Quicksort Step 5
pivot
Step 5:
After the exchange,
increment left marker,
decrement right
marker.
26
22
35
left
29
19
12
right
33
Quicksort Step 6
pivot
Step 6:
Element 35
belongs
to RIGHT
group.
26
35
29
19
12
left
Element 12
belongs
to LEFT group.
33
right
pivot
26
Exchange,
increment left,
and
decrement right.
22
22
12
29
left
19
35
right
33
Quicksort Step 7
pivot
Step 7:
Element 29
belongs
to RIGHT.
26
12
29
left
Element 19
belongs
to LEFT.
Exchange,
increment left,
decrement right.
22
19
35
33
right
pivot
26
22
12
19
right
29
left
35
33
Quicksort Step 8
Step 8:
When the left and
right
markers pass each
other,
we are done with the
partition task.
Swap the right with
pivot.
pivot
26
22
12
19
right
29
35
33
left
pivot
19
22
LEFT
12
26
29
35
RIGHT
33
Quicksort Step 9
previous pivot
Step 9:
Apply Quicksort
to the LEFT and
RIGHT groups,
recursively.
Assemble parts when
done
26
Quicksort
19 22
pivot
12
12
19
22
12
19
26
22
26
Quicksort
29
pivot
35
33
29
33
35
29
33
35
Heap Sort Algorithm
• The heap sort algorithm starts by using procedure
BUILD-HEAP to build a heap on the input array A[1 . .
n]. Since the maximum element of the array stored at
the root A[1], it can be put into its correct final position
by exchanging it with A[n] (the last element in A). If we
now discard node n from the heap than the remaining
elements can be made into heap. Note that the new
element at the root may violate the heap property. All
that is needed to restore the heap property.
Heapsort(A)
{
Buildheap(A)
for i length[A] //down to 2
do swap A[1] A[i]
heapsize[A] heapsize[A] - 1
Heapify(A, 1)
}
Example: Convert the following array to a heap
16
4
7
1
12
19
Picture the array as a complete binary tree:
16
4
1
7
12
19
16
16
4
4
7
19
swap
12
1
19
12
1
7
19
16
swap
12
12
19
16
swap
1
4
7
1
4
7
Heap Sort
•
The heapsort algorithm consists of two phases:
- build a heap from an arbitrary array
- use the heap to sort the data
•
•
To sort the elements in the decreasing order, use a min heap
To sort the elements in the increasing order, use a max heap
19
12
1
16
4
7
Example of Heap Sort
Take out biggest
12
1
16
4
Move the last element
to the root
7
Array A
12 16 1 4 7
19
Sorted:
19
7
swap
HEAPIFY()
12
1
16
4
Array A
7 12 16 1 4
Sorted:
19
16
12
1
7
4
Array A
16 12 7 1 4
Sorted:
19
Take out biggest
Move the last element
to the root
12
1
7
4
Array A
12 7 1 4
Sorted:
16 19
16
4
12
7
1
Array A
4 12 7 1
Sorted:
16 19
4
swap
HEAPIFY()
12
7
1
Array A
4 12 7 1
Sorted:
16 19
12
7
4
1
Array A
12 4 7 1
Sorted:
16 19
Take out biggest
Move the last
element to the
root
7
4
1
Array A
4 7 1
Sorted:
12 16 19
12
1
swap
7
4
Array A
1 4 7
Sorted:
12 16 19
7
4
1
Array A
7 4 1
Sorted:
12 16 19
Take out biggest
4
1
Array A
1 4
Move the last
element to the
root
Sorted:
7 12 16 19
7
1
swap
HEAPIFY()
4
Array A
4 1
Sorted:
7 12 16 19
Take out biggest
Move the last
element to the
root
1
Array A
1
Sorted:
4 7 12 16 19
4
1
Array A
Take out biggest
Sorted:
1 4 7 12 16 19
Sorted:
1 4 7 12 16 19
Counting sort
Sorting in linear time
Counting sort: No comparisons between elements
• Input: A[1 . . n], where A[ j]{1, 2, …, k} .
• Output: B[1 . . n], sorted.
• Auxiliary storage: C[1 . . k] .
Counting sort
for i 1 to k
do C[i] 0
for j 1 to n
do C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |
{key = i}|
for i 2 to k
do C[i] C[i] + C[i–1]
⊳ C[i] = |
{key i}|
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Counting-sort example
A:
B:
1
2
3
4
5
4
1
3
4
3
1
C:
2
3
4
Loop 1
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 0
0
0
0
B:
for i 1 to k
do C[i] 0
Loop 2
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 0
0
0
1
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1
⊳ C[i] = |{key = i}|
Loop 2
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
0
1
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1
⊳ C[i] = |{key = i}|
Loop 2
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
1
1
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1
⊳ C[i] = |{key = i}|
Loop 2
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
1
2
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1
⊳ C[i] = |{key = i}|
Loop 2
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
2
2
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1
⊳ C[i] = |{key = i}|
Loop 3
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
2
2
C': 1
1
2
2
B:
for i 2 to k
do C[i] C[i] + C[i–1]
⊳ C[i] = |{key i}|
Loop 3
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
2
2
C': 1
1
3
2
B:
for i 2 to k
do C[i] C[i] + C[i–1]
⊳ C[i] = |{key i}|
Loop 3
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
2
2
C': 1
1
3
5
B:
for i 2 to k
do C[i] C[i] + C[i–1]
⊳ C[i] = |{key i}|
Loop 4
A:
B:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
1
3
5
C': 1
1
2
5
3
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Loop 4
A:
B:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
1
2
5
4
C': 1
1
2
4
3
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Loop 4
A:
B:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
1
2
4
3
3
4
C': 1
1
1
4
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Loop 4
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
1
1
4
B: 1
3
3
4
C': 0
1
1
4
A:
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Loop 4
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 0
1
1
4
B: 1
3
3
4
4
C': 0
1
1
3
A:
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Quicksort Concept
• Basic Concept: divide and conquer
• Select a pivot and split the data into
two groups: (< pivot) and (> pivot):
pivot
( pivot)
RIGHT group
• Recursively apply Quicksort to the
subgroups
Quicksort Start
Start with all data
in an array, and
consider it
unsorted
Unsorted Array
Quicksort Step 1
pivot
Step 1, select a pivot
(it is arbitrary)
26
We will select the first
element, as presented in
the
original algorithm by
C.A.R. Hoare in 1962.
33
35
29
19
12
22
Quicksort Step 2
Step 2, start process of
dividing data into LEFT
and RIGHT groups:
The LEFT group will
have elements less than
the pivot.
The RIGHT group will have
elements greater that the
pivot.
Use markers left and right
pivot
26
33
left
35
29
19
12
22
right
Quicksort Step 3
Step 3,
If left element belongs
to LEFT group, then
increment
left index.
If right index element
belongs
to RIGHT, then
decrement right.
Exchange when you
find
elements that belong
to the other
group.
pivot
26
33
left
35
29
19
12
22
right
Quicksort Step 4
pivot
Step 4:
Element 33
belongs
to RIGHT
group.
26
35
29
19
12
left
Element 22
belongs
to LEFT group.
Exchange the
two
elements.
33
22
right
pivot
26
22
left
35
29
19
12
33
right
Quicksort Step 5
pivot
Step 5:
After the exchange,
increment left marker,
decrement right
marker.
26
22
35
left
29
19
12
right
33
Quicksort Step 6
pivot
Step 6:
Element 35
belongs
to RIGHT
group.
26
35
29
19
12
left
Element 12
belongs
to LEFT group.
33
right
pivot
26
Exchange,
increment left,
and
decrement right.
22
22
12
29
left
19
35
right
33
Quicksort Step 7
pivot
Step 7:
Element 29
belongs
to RIGHT.
26
12
29
left
Element 19
belongs
to LEFT.
Exchange,
increment left,
decrement right.
22
19
35
33
right
pivot
26
22
12
19
right
29
left
35
33
Quicksort Step 8
Step 8:
When the left and
right
markers pass each
other,
we are done with the
partition task.
Swap the right with
pivot.
pivot
26
22
12
19
right
29
35
33
left
pivot
19
22
LEFT
12
26
29
35
RIGHT
33
Quicksort Step 9
previous pivot
Step 9:
Apply Quicksort
to the LEFT and
RIGHT groups,
recursively.
Assemble parts when
done
26
Quicksort
19 22
pivot
12
12
19
22
12
19
26
22
26
Quicksort
29
pivot
35
33
29
33
35
29
33
35
Heap Sort Algorithm
• The heap sort algorithm starts by using procedure
BUILD-HEAP to build a heap on the input array A[1 . .
n]. Since the maximum element of the array stored at
the root A[1], it can be put into its correct final position
by exchanging it with A[n] (the last element in A). If we
now discard node n from the heap than the remaining
elements can be made into heap. Note that the new
element at the root may violate the heap property. All
that is needed to restore the heap property.
Heapsort(A)
{
Buildheap(A)
for i length[A] //down to 2
do swap A[1] A[i]
heapsize[A] heapsize[A] - 1
Heapify(A, 1)
}
Example: Convert the following array to a heap
16
4
7
1
12
19
Picture the array as a complete binary tree:
16
4
1
7
12
19
16
16
4
4
7
19
swap
12
1
19
12
1
7
19
16
swap
12
12
19
16
swap
1
4
7
1
4
7
Heap Sort
•
The heapsort algorithm consists of two phases:
- build a heap from an arbitrary array
- use the heap to sort the data
•
•
To sort the elements in the decreasing order, use a min heap
To sort the elements in the increasing order, use a max heap
19
12
1
16
4
7
Example of Heap Sort
Take out biggest
12
1
16
4
Move the last element
to the root
7
Array A
12 16 1 4 7
19
Sorted:
19
7
swap
HEAPIFY()
12
1
16
4
Array A
7 12 16 1 4
Sorted:
19
16
12
1
7
4
Array A
16 12 7 1 4
Sorted:
19
Take out biggest
Move the last element
to the root
12
1
7
4
Array A
12 7 1 4
Sorted:
16 19
16
4
12
7
1
Array A
4 12 7 1
Sorted:
16 19
4
swap
HEAPIFY()
12
7
1
Array A
4 12 7 1
Sorted:
16 19
12
7
4
1
Array A
12 4 7 1
Sorted:
16 19
Take out biggest
Move the last
element to the
root
7
4
1
Array A
4 7 1
Sorted:
12 16 19
12
1
swap
7
4
Array A
1 4 7
Sorted:
12 16 19
7
4
1
Array A
7 4 1
Sorted:
12 16 19
Take out biggest
4
1
Array A
1 4
Move the last
element to the
root
Sorted:
7 12 16 19
7
1
swap
HEAPIFY()
4
Array A
4 1
Sorted:
7 12 16 19
Take out biggest
Move the last
element to the
root
1
Array A
1
Sorted:
4 7 12 16 19
4
1
Array A
Take out biggest
Sorted:
1 4 7 12 16 19
Sorted:
1 4 7 12 16 19
Counting sort
Sorting in linear time
Counting sort: No comparisons between elements
• Input: A[1 . . n], where A[ j]{1, 2, …, k} .
• Output: B[1 . . n], sorted.
• Auxiliary storage: C[1 . . k] .
Counting sort
for i 1 to k
do C[i] 0
for j 1 to n
do C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |
{key = i}|
for i 2 to k
do C[i] C[i] + C[i–1]
⊳ C[i] = |
{key i}|
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Counting-sort example
A:
B:
1
2
3
4
5
4
1
3
4
3
1
C:
2
3
4
Loop 1
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 0
0
0
0
B:
for i 1 to k
do C[i] 0
Loop 2
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 0
0
0
1
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1
⊳ C[i] = |{key = i}|
Loop 2
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
0
1
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1
⊳ C[i] = |{key = i}|
Loop 2
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
1
1
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1
⊳ C[i] = |{key = i}|
Loop 2
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
1
2
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1
⊳ C[i] = |{key = i}|
Loop 2
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
2
2
B:
for j 1 to n
do C[A[ j]] C[A[ j]] + 1
⊳ C[i] = |{key = i}|
Loop 3
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
2
2
C': 1
1
2
2
B:
for i 2 to k
do C[i] C[i] + C[i–1]
⊳ C[i] = |{key i}|
Loop 3
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
2
2
C': 1
1
3
2
B:
for i 2 to k
do C[i] C[i] + C[i–1]
⊳ C[i] = |{key i}|
Loop 3
A:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
0
2
2
C': 1
1
3
5
B:
for i 2 to k
do C[i] C[i] + C[i–1]
⊳ C[i] = |{key i}|
Loop 4
A:
B:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
1
3
5
C': 1
1
2
5
3
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Loop 4
A:
B:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
1
2
5
4
C': 1
1
2
4
3
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Loop 4
A:
B:
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
1
2
4
3
3
4
C': 1
1
1
4
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Loop 4
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 1
1
1
4
B: 1
3
3
4
C': 0
1
1
4
A:
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
Loop 4
1
2
3
4
5
1
2
3
4
4
1
3
4
3
C: 0
1
1
4
B: 1
3
3
4
4
C': 0
1
1
3
A:
for j n downto 1
do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1