1. Engineering
  2. Computer Science
  3. loop invariant has to be used...

Question: loop invariant has to be used...

Question details

Prove the following loop invariants about the CountingSort algorithm, below. Note that you do not need to prove that this algorithm is correct. Input: data: array of n integers that are between 1 and r Input: n: size of data Input: r: range of data Output: permutation of data such that data[1] < data[2] <... < data/n] 1 Algorithm: CountingSort 2 count -Array(r) 3 Initialize count to 0 4for i = 1 to n do count[data[i]] count [data[i]] 1 6 end 7 for j 2 to r do 8Count 9 end 10 output -Array(n) 12output[count[datai 11 for i-1 to n do datali 13countIdatailcount datalill- 1 14 end 15 return output 1. Prove that count[j] equals the number of times j appears in data[1. , for y j from 1 up to r, after each iteration of the for loop in lines 4-6 2. Prove that countlequals the number of values in data that are less than or equal to j after every iteration of the for loop in lines 7-9. Hint: first, prove that count|j] is the number of times j appears in data after the loop in lines 4-6Loop invariant has to be used

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution