Академический Документы
Профессиональный Документы
Культура Документы
CS 477/677
Sorting Part A
Instructor: George Bebis
(Chapter 2)
Output:
A permutation (reordering) a1, a2, . . . , an of the input
sequence such that a1 a2 an
Structure of data
Some Definitions
Internal Sort
The data to be sorted is all stored in the computers
main memory.
External Sort
Some of the data to be sorted might be stored in
some external, slower, device.
In Place Sort
The amount of extra space required to sort the data is
constant with the input size.
Stability
A STABLE sort preserves relative order of records with
equal keys
Sorted on first key:
Insertion Sort
Idea: like sorting a hand of playing cards
Start with an empty left hand and the cards facing
down on the table.
Remove one card at a time from the table, and insert
it into the correct position in the left hand
compare it with each of the cards already in the hand, from
right to left
Insertion Sort
Insertion Sort
Insertion Sort
10
Insertion Sort
input array
sorted
right sub-array
unsorted
11
Insertion Sort
12
INSERTION-SORT
Alg.: INSERTION-SORT(A)
for j 2 to n
do key A[ j ]
a1 a2 a3 a4 a5 a6 a7 a8
key
ij-1
while i > 0 and A[i] > key
do A[i + 1] A[i]
ii1
A[i + 1] key
Insertion sort sorts the elements in place
13
ij-1
while i > 0 and A[i] > key
do A[i + 1] A[i]
ii1
A[i + 1] key
Invariant: at the start of the for loop the elements in A[1 . . j-1]
are in sorted order
14
Termination:
When the loop terminates, the invariant gives us a useful
property that helps show that the algorithm is correct
Stop the induction when the loop terminates
15
sorted
16
17
cost
c1
c2
Insert A[ j ] into the sorted sequence A[1 . . j -1] 0
c4
ij-1
c5
while i > 0 and A[i] > key
c6
do A[i + 1] A[i]
c7
ii1
c8
A[i + 1] key
for j 2 to n
do key A[ j ]
times
n
n-1
n-1
n-1
j 2 j
n
j 2
n
j 2
(t j 1)
(t j 1)
n-1
T (n) c1n c2 (n 1) c4 (n 1) c5 t j c6 t j 1 c7 t j 1 c8 (n 1)
n
j 2
j 2
j 2
19
tj = 1
j 2
j 2
j 2
20
The array is in reverse sorted order while i > 0 and A[i] > key
Always A[i] > key in while loop test
Have to compare key with all elements to the left of the j-th
position compare with j-1 elements tj = j
using
j 1
n
n(n 1)
n(n 1)
j
1
2
2
j 2
( j 1)
j 2
n(n 1)
2
we have:
n(n 1)
n(n 1)
n( n 1)
T (n ) c1n c2 (n 1) c4 (n 1) c5
1 c6
c7
c8 (n 1)
2
2
2
an2 bn c
T(n) = (n2)
a quadratic function of n
order of growth in n2
T (n) c1n c2 (n 1) c4 (n 1) c5 t j c6 t j 1 c7 t j 1 c8 (n 1)
n
j 2
j 2
j 2
21
cost
times
c1
c2
n-1
n-1
n2/2
n-1
ij-1
comparisons c4
c5
do A[i + 1] A[i]
c6
ii1
A[i + 1] key
n2/2
exchanges c7
c8
j 2 j
n
j 2
n
j 2
(t j 1)
(t j 1)
n-1
22
Disadvantages
(n2) running time in worst and average case
n2/2 comparisons and exchanges
23
1
j
24
Example
8
i=1
i=1
i=1
i=1
i=3
i=4
9
j
i=5
i=2
i=1
i=1
i=1
9
j
i=6
9
i=7
j
25
Bubble Sort
Alg.: BUBBLESORT(A)
for i 1 to length[A]
do for j length[A] downto i + 1
do if A[j] < A[j -1]
then exchange A[j] A[j-1]
i
8
i=1
1
j
26
i 1
i 1
T(n) = c1(n+1) + c2 (n i 1) c3 (n i ) c4
= (n) + (c2 + c2 + c4)
c4
(n i)
i 1
(n i)
i 1
2
n
(
n
1)
n
n
2
where (n i ) n i n
2
2 2
i 1
i 1
i 1
n
Thus,T(n) = (n2)
27
Disadvantage:
Running time depends only slightly on the amount of
order in the file
28
Example
8
29
Selection Sort
Alg.: SELECTION-SORT(A)
n length[A]
8 4 6
for j 1 to n - 1
do smallest j
for i j + 1 to n
do if A[i] < A[smallest]
then smallest i
exchange A[j] A[smallest]
30
times
n length[A]
c1
for j 1 to n - 1
c2
c3
n-1
n2/2
do smallest j
for i j + 1 to n
comparisons
cost
c4 nj11 (n j 1)
c5
then smallest i
c6
exchanges
n 1
n 1
j 1
j 1
j 2
n 1
j 1
n 1
j 1
(n j )
(n j )
n-1
T ( n) c1 c2 n c3 (n 1) c4 (n j 1) c5 n j c6 n j c7 (n 1) (n 2 )
31