Algorithm to find nth highest number in an array - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Algorithm to find nth highest number in an array

Posts: 28Member
Hi,
Could anybody please give me hints/help on getting an algorithm to find the nth highest in an array of n numbers. Looking forward for an early reply. Thanks in advance.

With Regards
Murali

• Hi Murali,

you could simply sort your array an then pick out the nth number. You can also use an altered quick-sort algorithm for this, which will not sort the whole array, but only the parts of it that are needed to get the nth highest number. (If your familiar with quick-sort, i'll explain to you how to alter it.)

tron.

: Hi,
: Could anybody please give me hints/help on getting an algorithm to find the nth highest in an array of n numbers. Looking forward for an early reply. Thanks in advance.
:
: With Regards
: Murali
:
:

• Posts: 28Member
Hi tron,
Well I am familiar with quick sort algorithm. But do you think that sorting an array and finding the nth highest is most elegant way of doing it. Please do let me know. I am interested in knowing the method. I am familiar with Quick Sort Algo.

With Regards
Murali

: Hi Murali,
:
: you could simply sort your array an then pick out the nth number. You can also use an altered quick-sort algorithm for this, which will not sort the whole array, but only the parts of it that are needed to get the nth highest number. (If your familiar with quick-sort, i'll explain to you how to alter it.)
:
: tron.
:
: : Hi,
: : Could anybody please give me hints/help on getting an algorithm to find the nth highest in an array of n numbers. Looking forward for an early reply. Thanks in advance.
: :
: : With Regards
: : Murali
: :
: :
:
:

• Hi Murali,

yes I think that you "somehow" have to sort the array. You need to know at least the n highest (or the m-n lowest) elements to pick out the nth highest element.

If you are looking for the 2nd, 3rd, 4th, etc. element, it could be easily done by simply cycling through the array. However, to find the 4th element is getting quite complex - just imagine to find the 100th element ...

For this, the best way would be to sort the array.

Here's the altered quick-sort algorithm in some words:

In quick-sort, you are sorting an interval end then descending into two parts of it. Sorting [1..m] ends up in [[1..p-1] [p] [p+1..m]] ([p] is the pivot element) where you know that all elements [1..p-1] <= [p] <= [p+1..m]. Instead of sorting both parts ([1..p-1] and [p+1..m]) you only sort the part where your element is in. If (p+1 <= n <= m) then you sort [p+1..m], if (1 <= n <= p-1) then you sort [1..p-1], otherwise it's [p]. You do this until you find n.

I don't know the complexity of this algo (at max. it's the complexity of quick-sort, which would be O(n*log(n)) in best case) - however, you can see that this takes much less steps than sorting the whole array with quick-sort.

If you have any further questions, or if you find a better (less complexity) algo, please let me know - that's quite an interesting thread.

tron.

: Hi tron,
: Well I am familiar with quick sort algorithm. But do you think that sorting an array and finding the nth highest is most elegant way of doing it. Please do let me know. I am interested in knowing the method. I am familiar with Quick Sort Algo.
:
: With Regards
: Murali
:
: : Hi Murali,
: :
: : you could simply sort your array an then pick out the nth number. You can also use an altered quick-sort algorithm for this, which will not sort the whole array, but only the parts of it that are needed to get the nth highest number. (If your familiar with quick-sort, i'll explain to you how to alter it.)
: :
: : tron.
: :
: : : Hi,
: : : Could anybody please give me hints/help on getting an algorithm to find the nth highest in an array of n numbers. Looking forward for an early reply. Thanks in advance.
: : :
: : : With Regards
: : : Murali
: : :
: : :
: :
: :
:
:

• Posts: 28Member
Hi Tron,
Thank you for the solution. I shall look into it and try to come out with the solution. Thanks once again. I shall see if there are other solutions. Thank you once again.

With Regards
Murali

: Hi Murali,
:
: yes I think that you "somehow" have to sort the array. You need to know at least the n highest (or the m-n lowest) elements to pick out the nth highest element.
:
: If you are looking for the 2nd, 3rd, 4th, etc. element, it could be easily done by simply cycling through the array. However, to find the 4th element is getting quite complex - just imagine to find the 100th element ...
:
: For this, the best way would be to sort the array.
:
: Here's the altered quick-sort algorithm in some words:
:
: In quick-sort, you are sorting an interval end then descending into two parts of it. Sorting [1..m] ends up in [[1..p-1] [p] [p+1..m]] ([p] is the pivot element) where you know that all elements [1..p-1] <= [p] <= [p+1..m]. Instead of sorting both parts ([1..p-1] and [p+1..m]) you only sort the part where your element is in. If (p+1 <= n <= m) then you sort [p+1..m], if (1 <= n <= p-1) then you sort [1..p-1], otherwise it's [p]. You do this until you find n.
:
: I don't know the complexity of this algo (at max. it's the complexity of quick-sort, which would be O(n*log(n)) in best case) - however, you can see that this takes much less steps than sorting the whole array with quick-sort.
:
: If you have any further questions, or if you find a better (less complexity) algo, please let me know - that's quite an interesting thread.
:
: tron.
:
: : Hi tron,
: : Well I am familiar with quick sort algorithm. But do you think that sorting an array and finding the nth highest is most elegant way of doing it. Please do let me know. I am interested in knowing the method. I am familiar with Quick Sort Algo.
: :
: : With Regards
: : Murali
: :
: : : Hi Murali,
: : :
: : : you could simply sort your array an then pick out the nth number. You can also use an altered quick-sort algorithm for this, which will not sort the whole array, but only the parts of it that are needed to get the nth highest number. (If your familiar with quick-sort, i'll explain to you how to alter it.)
: : :
: : : tron.
: : :
: : : : Hi,
: : : : Could anybody please give me hints/help on getting an algorithm to find the nth highest in an array of n numbers. Looking forward for an early reply. Thanks in advance.
: : : :
: : : : With Regards
: : : : Murali
: : : :
: : : :
: : :
: : :
: :
: :
:
: