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
A function can be created to find the indices of the largest, second largest, third largest, and fourth largest numbers in an array. The proposed method involves initializing arrays for the maximum values and their corresponding indices. By iterating through the array and comparing values, the function updates these arrays to store the largest numbers and their indices. An alternative approach suggested is to utilize a sorting algorithm, where instead of sorting the values directly, an index array is maintained. This index array is initialized to reflect the original indices, and during sorting, the indices are swapped to track the positions of the largest numbers without altering the original data array. This method can be adapted to any sorting algorithm.
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

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