Write an algorithm to find the second largest element in a list of N values. How
ID: 3562128 • Letter: W
Question
Write an algorithm to find the second largest element in a list of N values. How many comparisons does your algorithm do in the worst case
Write an algorithm that determines, given a list of numbers and the average or mean of those numbers, if there are more numbers above the average than below. Describe the groups that the input would fall into for this algorithm. What is the best case for your algorithm? What is the worst case? What is the average case? (If the best and worst cases are the same, rewrite your algorithm so it stops as soon as it knows the answer, making the best case better than the worst case.)
Explanation / Answer
largest := numbers[0]; secondLargest := null for i=1 to numbers.length-1 do number := numbers[i]; if number > largest then secondLargest := largest; largest := number; else if number > secondLargest then secondLargest := number; end; end; end;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.