Trace the PresortMode algorithm with the array A = [11, 14, 12, 13, 12, 11, 12,
ID: 3710606 • Letter: T
Question
Trace the PresortMode algorithm with the array A = [11, 14, 12, 13, 12, 11, 12, 14, 13, 12]. What is the modevalue, modefrequency, and runvalue after three iterations of the outer while loop?(please show all steps)
PresortMode(A[0..n – 1])
Sort the array A;
i = 0;
while (i <= n – 1) do
{
runlength = 1; runvalue = A[i];
while (i + runlength <= n – 1 AND A[i + runlength] = runvalue) do
runlength = runlength + 1;
if (runlength > modefrequency)
{
modefrequency = runlength;
modevalue = runvalue;
}
i = i + runlength;
}
return modevalue;
Explanation / Answer
Solution:
The given array is A = [11, 14, 12, 13, 12, 11, 12, 14, 13, 12]?
At i= 0;
runlength = 1, runvalue= 11
Now the while loop itertions
outer while llop is running n numnber of times which means 10 times
at i= 0
1<= 10, 14==11, condition false
at i= 1
1<= 10, 12==12, condition false
at i= 2
1<= 10, 12==13, condition false
at i= 3
1<= 10, 13==12, condition false
at i= 4
1<= 10, 12==11, condition false
at i= 5
1<= 10, 11==12, condition false
at i= 6
1<= 10, 12==14, condition false
at i= 7
1<= 10, 14==13, condition false
at i= 8
1<= 10, 13==12, condition false
at i= 9
1<= 10, 12==garbage, condition false
The loop is getting terminateed withour any calcualtions, please check if the algo is correct
specially while (i + runlength <= n – 1 AND A[i + runlength] = runvalue) do
A[i + runlength] = runvalue this condition.
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.