Find Largest & Second Largest Number Index in Array - Ask Algorithm

  • Thread starter Thread starter nenyan
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary

Discussion Overview

The discussion revolves around finding the indices of the largest, second largest, third largest, and fourth largest numbers in an array. The focus is on algorithmic approaches to achieve this, including coding techniques and sorting methods.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant requests a function that can return the indices of the largest, second largest, third largest, and fourth largest numbers in a given array.
  • Another participant provides a code snippet that attempts to track the maximum values and their indices but does not clarify the overall logic or correctness.
  • Another suggestion involves using a sorting algorithm that maintains an index array, where the indices are swapped instead of modifying the original data array, allowing retrieval of the indices of the largest values.
  • A later reply reiterates the sorting method, emphasizing the initialization of an index array to track original positions while sorting.

Areas of Agreement / Disagreement

Participants present multiple approaches to the problem, with some agreeing on the utility of sorting algorithms while others provide different coding strategies. No consensus is reached on a single method or solution.

Contextual Notes

The discussion lacks clarity on the correctness of the provided code snippets and the assumptions behind the sorting method. There are also no details on handling edge cases or the efficiency of the proposed solutions.

nenyan
Messages
67
Reaction score
0
a function: can give the index of the largest number in an array. also give the index of the second largest number, and the index of the third and the forth.


such as

a={1,2,3,5,7,99,121,3,2,24,10}
the code can give the result followed:
6,5,9,10

how to make the function?
 
Technology news on Phys.org
eh, I got one

max[4]=-100;
maxIndex[4]=-1;
for i=1:4
for j=a1:an
if aj>maxi
max=aj;
maxindex=j;
end if
end for j
end for i
 
One way would be to use any sort algorithm, but instead of outputting the values to a target array, you give your sort function an array that is the same dimension but contains index information.

So basically you initialize your index array to a = i and then in your sort function swap the values in your index array instead of modifying your values in the actual data array itself.

You can use the above idea with any sort implementation.
 
That's a good idea! Thank you.
chiro said:
One way would be to use any sort algorithm, but instead of outputting the values to a target array, you give your sort function an array that is the same dimension but contains index information.

So basically you initialize your index array to a = i and then in your sort function swap the values in your index array instead of modifying your values in the actual data array itself.

You can use the above idea with any sort implementation.
 

Similar threads

  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 13 ·
Replies
13
Views
5K
Replies
9
Views
2K
Replies
20
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
7K
  • · Replies 31 ·
2
Replies
31
Views
3K
Replies
17
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K