& SORTING
Budi Hartono, ST, MPM
Algorithm is sequencing
Declaration: Declaration:
A, B: integer A, B: integer
Description: Description:
A 10 A 10
A 2*A B A
B A A 2*A
read (totalSeconds)
J.Hh totalSeconds div 3600
Spillage totalSeconds mod 3600
J.Mm spillage div 60
J.Ss spillage mod 60
Write (J.hh, J.mm, J.ss)
SORTING
What Are Sorting Algorithms?
Basic evaluation
Time to complete sorting
Function of number of items to be sorted
(1) Straight Selection
Recall previous discussion
1 2 3 4 5 6 N=6
12 29 17 56 11 23
1 2 3 4 5 6 N=6
87 74 71 54 88 60
Number of items: n
Worst case:
(n – 1) + (n – 2) + … + 2 + 1= n (n – 1) / 2
1 2 3 4 5 6 7 8
10 5 7 9 2 1 8 6
(3) Straight insertion
Pass # Results
0 10 5 7 9 2 1 8 6
1 5 10 7 9 2 1 8 6
2 5 7 10 9 2 1 8 6
3 5 7 9 10 2 1 8 6
4 2 5 7 9 10 1 8 6
5 1 2 5 7 9 10 8 6
6 1 2 5 7 8 9 10 6
7 1 2 7 6 7 8 9 10
Straight insertion - Flow Chart
START
Read N, X[I]
T = X[I]
X[0]=T
J=I-1 STOP
YES
WHILE T<x[J]
X [J+1] = X[J]
J=J-1 NO
X[J+1]=T
NEXT I
Straight insertion - Time
Complexity
Number of items: n
Worst case:
1 + 2 + 3 + … + (n – 1) = n (n – 1) / 2
Steps:
1. Define Location
2. Shift all ordered data
3. Insert the new data
(4) Binary (Heap) Insertion
1 2 3 4 5 6 7 8
10 5 7 9 2 1 8 6
5 7 10 9 2 1 8 6
LB UB
MID Q
(3) Insert
Pass # Results
0 10 5 7 9 2 1 8 6
1 5 10 7 9 2 1 8 6
2 5 7 10 9 2 1 8 6
3 5 7 9 10 2 1 8 6
4 2 5 7 9 10 1 8 6
5 1 2 5 7 9 10 8 6
6 1 2 5 7 8 9 10 6
7 1 2 7 6 7 8 9 10
Data to be #Location for
Iteration # Counter inserted #LB #UB #MID Inserting
…
5 6 1 1 5 3 -
1 1 2 2 -
1 1 1 1 1
6 7 8 1 6 4 -
8 4 5 4 -
8 5 5 5 5
O(n log n)
quick-sort fastest (good for large inputs)
expected
1 2 3 4 5 6 7
10 2 17 7 16 3 9
N=7
X
location = J
smaller greater
(5) Quick Sort
10, 2, 17, 7, 16, 3, 9.
Quick-Sort
Quick-sort is a randomized
sorting algorithm based on
the divide-and-conquer x
paradigm:
– Divide: Pick a random
element x (called the
pivot) and partition S into x
L elements less than x
E elements equal to x L E G
G elements greater than x
– Recurse: Sort L and G
– Conquer: Join L, E, and
G x
Partition
We partition an input Algorithm partition(S, p)
sequence as follows: Input sequence S, position p of pivot
We remove, in turn, each
element y from S and Output subsequences L, E, G of the
We insert y into L, E or G, elements of S less than, equal to,
depending on the result of or greater than the pivot, resp.
the comparison with the L, E, G ← empty sequences
pivot x
x ← S.remove(p)
Each insertion and removal
is at the beginning or at the while ¬S.isEmpty()
end of a sequence, and y ← S.remove(S.first())
hence takes O(1) time if y < x
Thus, the partition step of L.insertLast(y)
quick-sort takes O(n) time
else if y = x
Pivot sometimes chosen as
first element of S (assuming E.insertLast(y)
S unsorted), or median of else { y > x }
first, middle, and last G.insertLast(y)
elements of S
return L, E, G
Quick-Sort Tree
An execution of quick-sort is depicted by a binary tree
Each node represents a recursive call of quick-sort and stores
Unsorted sequence before the execution and its pivot
Sorted sequence at the end of the execution
The root is the initial call
The leaves are calls on subsequences of size 0 or 1
I.e., recurse on L or G if it has 2 or more members
7 4 9 6 2 → 2 4 6 7 9
4 2 → 2 4 7 9 → 7 9
2→ 2 9→ 9
Execution Example
Pivot selection
7 2 9 43 7 6 1 → 1 2 3 4 6 7 8 9
7 2 9 4 → 2 4 7 9 3 8 6 1 → 1 3 8 6
2→ 2 9 4 → 4 9 3→ 3 8→ 8
9→ 9 4→ 4
Execution Example (cont.)
Partition, recursive call, pivot selection
7 2 9 4 3 7 6 1→ 1 2 3 4 6 7 8 9
2 4 3 1→ 2 4 7 9 3 8 6 1 → 1 3 8 6
2→ 2 9 4 → 4 9 3→ 3 8→ 8
9→ 9 4→ 4
Execution Example (cont.)
Partition, recursive call, base case
7 2 9 43 7 6 1→ → 1 2 3 4 6 7 8 9
2 4 3 1 →→ 2 4 7 3 8 6 1 → 1 3 8 6
1→ 1 9 4 → 4 9 3→ 3 8→ 8
9→ 9 4→ 4
Execution Example (cont.)
Recursive call, …, base case, join
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9
2 4 3 1 → 1 2 3 4 3 8 6 1 → 1 3 8 6
1→ 1 4 3 → 3 4 3→ 3 8→ 8
9→ 9 4→ 4
Execution Example (cont.)
Recursive call, pivot selection
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9
2 4 3 1 → 1 2 3 4 7 9 7 1 → 1 3 8 6
1→ 1 4 3 → 3 4 8→ 8 9→ 9
9→ 9 4→ 4
Execution Example (cont.)
Partition, …, recursive call, base case
7 2 9 43 7 6 1→ 1 2 3 4 6 7 8 9
2 4 3 1 → 1 2 3 4 7 9 7 1 → 1 3 8 6
1→ 1 4 3 → 3 4 8→ 8 9→ 9
9→ 9 4→ 4
Execution Example (cont.)
Join, join
7 2 9 4 3 7 6 1 → 1 2 3 4 6 7 7 9
2 4 3 1 → 1 2 3 4 7 9 7 → 17 7 9
1→ 1 4 3 → 3 4 8→ 8 9→ 9
9→ 9 4→ 4
Worst-case Running Time
The worst case for quick-sort occurs when the pivot is the unique
minimum or maximum element
One of L and G has size n − 1 and the other has size 0
The running time is proportional to the sum
n + (n − 1) + … + 2 + 1
Thus, the worst-case running time of quick-sort is O(n2)
depth time
0 n
1 n−1
…
… …
n−1 1
Expected Running Time
Consider a recursive call of quick-sort on a sequence of size s
Good call: the sizes of L and G are each less than 3s/4
Bad call: one of L and G has size greater than 3s/4
7 2 9 43 7 6 19 7 2 9 43 7 6 1
2 4 3 1 7 9 7 1 → 1 1 7294376
O(n log n)
quick-sort fastest (good for large inputs)
expected
Two search operations started ‘together’, one from the left, and one
from the right.
At the beginning of the search operations, the index Left is the same
as First, and
the index Right is the same as Last. During the operation, Right
moves to the left, and Left moves to the right.
The search operations continue as long as the two search
operations do not meet.
This occurs when Right=Left. At this point, the WHILE loop ends and
the first element takes a new position stored in the index Mid.
The DivideAndConquer procedure does the main process, which is
arranging the array passed to it. This procedure is called from the
quicksort procedure