This preview shows pages 1–5. Sign up to view the full content.
SORTING
Problem: sort a list of numbers (or
comparable
objects).
Solution: An algorithm.
The problem is interesting for its theoretical value, and for
its practical utility. Many algorithms are available for the
purpose.
Bubble Sort
BubleSort (A)
.1
for i=1 through n do
.2
for j=n through i+1 do
.3
if A[j] < A[j1] then
.4
exchange A[j] <  > A[j1]
End algorithm.
Lemma
: Lines 2 through 4 get the smallest element of A[i]
through A[n] at the ith position of the array.
Loop invariant for lines 2 through 4 is the property that
A[j1]
≤
A[j]
Proof
:
Initialization:
Starts with A[n].
Maintenance:
After j=kth iteration, for some i<k
≤
n, A[k
1]
≤
A[k].
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Termination:
The loop terminates at j=i+1. At that point
A[i]
≤
A[i+1],
A[i]
≤
A[i+2], …, A[i]
≤
A[n].
So, A[i] is
the smallest element between A[i] through A[n].
QED
Theorem: BubbleSort
correctly sorts the input array A.
Loop invariant for lines 1 through 4 is that A[i]
≤
A[i+1].
Initialization:
Starts with A[1].
Maintenance:
After the iteration i=p, for some 1<p
≤
n, the
above lemma for lines 2 through 4 guarantees that A[p] is
the smallest element from A[p] through A[n]. So, A[p]
≤
A[p+1].
Termination:
The loop 1 through 4 terminates at i=n. At
that point A[1]
≤
A[2]
≤
. . .
≤
A[n]. Hence, the
correctness theorem is proved.
QED
Complexity:
∑
i=1
n
∑
j=i+1
n
1 =
∑
i=1
n
(n –i) = n
2
– n(n+1)/2
= n
2
/2 – n/2 =
Θ
(n
2
)
Insertion Sort
The way cards are sorted in the pack: pick up objects one
by one from one end and insert it in its correct position in
the partially sorted part.
Based on inductive proof technique:
Induction base: only one element in array – it is already
sorted (p=1)
Induction hypothesis: assume the algorithm works correctly
up to kth iteration, i.e., we have sorted array 1 through k
(p=k)
Induction step: a[k+1] is correctly inserted in the sorted
portion a[1.
.k] (step 3 5 in the algorithm below does this)
So, a[1.
.k+1] is sorted.
QED
.
Algorithm
insertionsort
(an array A of n comparable objects)
.(1)
for
p = 2 through
n
do
.(2)
temp = A[p];
.(3)
for
j = p
though 2
do until
A[j1]<temp
.(4)
A[j] = A[j1];
// shift all larger elements right
.(5)
A[j1] = temp;
// insert it in its correct position in the
//
already sorted portion
End Algorithm.
Example:
34
8
64
51
32
21
8
34
64
51
32
21
8
34
64
51
32
21
8
34
51
64
32
21
8
32
34
51
64
21
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document8
21
32
34
51
64.
Worstcase Complexity:
2 + 3 + 4 + … + n =
n(n+1)/2
1 = (n
2
)/2 + n/2 1 = O(n
2
)
(For reverse sorted array).
Actual run time could be better (loop may terminate earlier
than running for p1 times!
The best case scenario (sorted array as input):
Ω
(n).
Average case complexity:
Some observations:
(1)
There are 9 "inversions" in the above original list: (34,
8), (34, 32), … (51, 32), (51, 31), … [FIND OTHERS].
(2)
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 02/10/2012 for the course CSE 5211 taught by Professor Dmitra during the Spring '12 term at FIT.
 Spring '12
 Dmitra
 Algorithms, Sort, Bubble Sort

Click to edit the document details