I want to be able to find the 10 highest integer indexes in an array full of integers. How do I go forward? I can’t think of a simple way of doing it.
Say that you have a long list of scores, the largest 10 won’t be the 10 last integers.
I want to be able to find the 10 highest integer indexes in an array full of integers. How do I go forward? I can’t think of a simple way of doing it.
Say that you have a long list of scores, the largest 10 won’t be the 10 last integers.
if you’re just looking for the last 10 indexes, you can get the length of the array as a base point to work with. so you would build a new list called ‘10 highest members’ or something, then iterate through your original list and add the member to your new list if its index is greater than or equal to the oldlist’s length - 9.
if you’re talking about getting the highest values of the member at index, that’s a different issue.
This post sorts floats. should work for ints too.
yeah I am talking the highest integers, I should have clarified that.
Getting the 10 largest indexes is ofc a lot simpler.
Based on the answer from trutty refered to by Justin.Dooley, here’s how you can get the 10 top elements from an array in O(10n + 10) = O(n) steps (10 times find max item + readding) instead of O(nlog n) steps (efficient sorting and just using the last 10 elements; the float sorting example requires O(n^2) steps). So with a huge list, not sorting the complete list may be faster:
Please note, that this changes the order of the elements of the original array, as I first remove the top items and then add them again.