Write the forpseudocode an algorithm that determines if a function is injective, assuming that you can iterate through all the elements in the domain and the co-domain in a finite number of steps. Recall the insertion sort algorithm discussed in class (pseudocode shown below). Find the big-O of the number of comparisons made in the execution of insertion sort on the input list of numbers: k, k-1, k-2, ..., 2, 1. Explain your reasoning. 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. Find the O(g) estimate for each of the following functions. The order of g should be as small as possible, and g should be as simple as possible. a) nn100+nn(n!)+n1.2n b) (n5+100)(32 log n - 6) + log n (n5 + 2n4 log n) c) (n6+1.5n)(1.1n+n5)
The Answer to the Question
is below this banner.
Can't find a solution anywhere?
NEED A FAST ANSWER TO ANY QUESTION OR ASSIGNMENT?
Get the Answers Now!You will get a detailed answer to your question or assignment in the shortest time possible.
Here's the Solution to this Question
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