ALGORITHM 5 The Insertion Sort.
procedure insertion sort(a1, a2, ..., : real numbers with n > 2) for j = 2 ton i=1 while aj > a i=i+1 m = 0; for k:= 0 to j-i-1 aj-k := 0;-k-1 dim {a1, ..., An is in increasing order)
5.
b) (n5+100)(32 log n - 6) + log n (n5 + 2n4 log n)
c) (n6+1.5n)(1.1n+n5)
2.
To insert the last element, we need at most n-1 comparisons and at most n-1 swaps. To insert the second to last element, we need at most n-2 comparisons and at most n-2 swaps, and so on. The number of operations needed to perform insertion sort is therefore:
So, the algorithm's complexity is
5.1
b)
c)
a)
1.
input:
int n \\ the number of values
array x(n) \\ domain of function f(x)
array f(n) \\ values of function f(x)
for i from 1 to n
for j from 1 to n
if f[i]=f[j] then
break
output:
function is not injective
else
function is injective
Comments