Solution Found!
8E Devise an efficient algorithm for finding the second
Chapter 3, Problem 8E(choose chapter or problem)
?8E Devise an efficient algorithm for finding the second largest element in a sequence of ?n? elements and determine the worst-case complexity of your algorithm.
Questions & Answers
QUESTION:
?8E Devise an efficient algorithm for finding the second largest element in a sequence of ?n? elements and determine the worst-case complexity of your algorithm.
ANSWER:Solution Step 1 In this Question we have to write the best way to find the second largest number from the list of n elements. Algorithms second_largest_Num(num1, num2, num3, …………...set of integers) Assign a variable largest_num1 and largest_num2 then set it as 0 Now, initialize the loop For ( i = 0 ; i < size ; i++) where n is the number of element in the list. Check the largest number to the second largest number till size. if( element at ith position > largest_num1) then Largest_num2 := largest_num1 Largest_num1 := element at ith position Else if (element at ith position > largest_num2) then Largest_num2 := element at ith position return largest_num2 which is the second largest number.