















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















Sorting Algorithms
2
-^ Internal Sort– The data to be sorted is all stored in the computer’s
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.
4
To insert 12, we need tomake room for it by movingfirst 36 and then 24.
5
7
input array
left sub-array
right sub-array
at each iteration, the array is divided in two sub-arrays:
sorted
unsorted
8
10
for^ j
to^ n do^ key
←^ A[ j ] Insert
A[ j ]
into the sorted sequence
A[1.. j -1]
i^ ←^
j - 1 while
i > 0
and
A[i] > key
do^ A[i + 1]
←^ A[i] i^ ←^
i – 1
A[i + 1]
←^ key
Invariant
: at the start of the
for^ loop the elements in
A[1.. j-1]
are in sorted order
11
n c^2
n- 0
n- c^4
n- c^5 c^6 c^7 c^8
n j^ n- tj 2 ^ ^
n tjj^^2
) 1 ( ^ ^
n tjj^^2
) 1 (
^
^
^
^
) 1 ( 1 1
) 1 ( ) 1 ( )(
8 7 2 6 2 5 2 4 2 1
^
^
nc t c t ct c nc nc nc nT
n j j n j j n j j
for^ j
to^ n do^ key
←^ A[ j ] Insert A[ j ] into the sorted sequence A[1.. j -1]i^
←^ j - 1 while
i > 0 and A[i] > key do^ A[i + 1]
←^ A[i] i^ ←^
i – 1
A[i + 1]
←^ key t: # of times the while statement is executed at iteration jj^
docsity.com
13
-^ The array is in reverse sorted order– Always
A[i] > key
in^ while
loop test
key^
with all elements to the left of the
j - th
position
^ compare with
j-1^ elements
^ tj
= j
a quadratic function of n
-^ T(n) =
(n
order of growth in
(^2) n
1
2
2
(^ 1)^
(^ 1)^
(^ 1)
1
(^ 1)
2
2
2
n^
n^
n
j^
j^
j
n n^
n n^
n n
j^
j^
j
^
^
^
^
^
^
^
^
^
^
^
) (^1) ( ) (^1) ( 2 ) (^1) ( 2 1 ) (^1) ( 2 ) 1 () (^1) ( )(
8 7 6 5 4 2 1 ^
nc nnc nnc
nn c nc nc nc nT
2
“while
i > 0 and A[i] > key” ^
^
^
^
) 1 ( 1 1
) 1 ( ) 1 ( )(
8 7 2 6 2 5 2 4 2 1
^
^
nc t c t ct c nc nc nc nT
n j j n j j n j j
using
we have:
docsity.com
14
Comparisons and Exchanges in
Insertion Sort
for^ j
to^ n do^ key
←^ A[ j ] Insert A[ j ] into the sorted sequence A[1.. j -1]i^
←^ j - 1 while
i > 0 and A[i] > key do^ A[i + 1]
←^ A[i] i^ ←^
i – 1
A[i + 1]
←^ key
n c^2
n- 0
n- c^4
n- c^5 c^6 c^7 c^8
n ^ j^ n- tj 2 ^ ^
n tjj^^2
) 1 ( ^ ^
n tjj^^2
) 1 (
comparisons
exchanges
docsity.com
16
-^ Idea:– Repeatedly pass through the array– Swaps adjacent elements that are out of order•^ Easier to implement, but slower than Insertionsort
1 2
3
n
i
(^13) (^29) (^64) 8
j
17
(^32) (^96) (^48) i = 1^
j (^31) (^29) (^64) (^8) i = 1^
j^321 (^96) (^48) i = 1^
j
(^32) (^91) (^64) (^8) i = 1^
j
(^32) (^96) (^14) (^8) i = 1^
j
(^32) (^96) (^41) (^8) i = 1^ j
(^32) (^96) (^48) (^1) i = 1^ j
(^32) (^96) (^48) 1 i = 2
j 3 (^96) (^48) 21 i = 3
j 9 (^64) (^83) (^21)
i = 4^
j 9 (^68) (^43) (^21)
i = 5^
j 9 (^86) (^43) (^21)
i = 6^
j 9 (^86) (^43) (^21)
i = 7j
19
(n
2
2
1
1
1
n^
n^
n
i^
i^
i
^
^
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]
in 1
) 1 (
n i
in 1
) (^
n i
in 1
) (
n i
in 1
) (
Comparisons:
(^2) n / Exchanges:
(^2) n /
c^1
c^2 c^3
c^4 docsity.com
20
the element in the second position– Continue until the array is sorted
-^ Disadvantage:– Running time depends only slightly on the amount of
order in the file