a. Prove that count[j] equals the number of times j appears in data[1::i], for every j from 1 up to r, after each iteration of the for loop in lines 4-6. b. Prove that count[j] equals 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.

icon
Related questions
Question
Prove the loop invariant of the counting sort algorithm
given below, you do not have to prove the correctness of
the algorithm.
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: Counting Sort
2 count = Array (r)
3 Initialize count to 0
4 for i=1 to n do
5
6 end
count[data[i]] = count[data[i]] + 1
7 for j = 2 to r do
8
12
13
count[j] = count[j] + count j - 1]
9 end
10 output= Array(n)
11 for i=1 to n do
output [count data[i]]] = data[i]
count [data[i]]= count[data[i]] - 1
14 end
15 return output
Transcribed Image Text:Prove the loop invariant of the counting sort algorithm given below, you do not have to prove the correctness of the algorithm. 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: Counting Sort 2 count = Array (r) 3 Initialize count to 0 4 for i=1 to n do 5 6 end count[data[i]] = count[data[i]] + 1 7 for j = 2 to r do 8 12 13 count[j] = count[j] + count j - 1] 9 end 10 output= Array(n) 11 for i=1 to n do output [count data[i]]] = data[i] count [data[i]]= count[data[i]] - 1 14 end 15 return output
a. Prove that count[j] equals the number of times j appears in
data[1::i], for every j from 1 up to r, after each iteration of the for
loop in lines 4-6.
b. Prove that count[j] equals 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.
Transcribed Image Text:a. Prove that count[j] equals the number of times j appears in data[1::i], for every j from 1 up to r, after each iteration of the for loop in lines 4-6. b. Prove that count[j] equals 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.
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer