Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 162571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:24:53+00:00 2026-05-11T11:24:53+00:00

I have an array of float values and want the value and more importantly

  • 0

I have an array of float values and want the value and more importantly the position of the maximum four values.

I built the system originally to walk through the array and find the max the usual way, by comparing the value at the current position to a recorded max-so-far, and updating a position variable when the max-so-far changes. This worked well, an O(n) algo that was very simple. I later learned that I need to keep not only the top value, but the top three or four. I extended the same procedure and complicated the max-so-far into an array of four max-so-fars and now the code is ugly.

It still works and is still sufficiently fast because only a trivial amount of computations have been added to the procedure. it still effectively walks across the array and checks each value once.

I do this in MATLAB with a sort function that returns two arrays, the sorted list and the accompanying original position list. By looking at the first few values I have exactly what I need. I am replicating this functionality into a C# .NET 2.0 program.

I know that I could do something similar with a List object, and that the List object has a built in sort routine, but I do not believe that it can tell me the original positions, and those are really what I am after.

It has been working well, but now I find myself wanting the fifth max value and see that rewriting the max-so-far checker that is currently an ugly mess of if statements would only compound the ugliness. It would work fine and be no slower to add a fifth level, but I want to ask the SO community if there is a better way.

Sorting the entire list takes many more computations than my current method, but I don’t think it would be a problem, as the list is ‘only’ one or two thousand floats; so if there is a sort routine that can give back the original positions, that would be ideal.

As background, this array is the result of a Fourier Transform on a kilobyte of wave file, so the max values’ positions correspond to the sample data’s peak frequencies. I had been content with the top four, but see a need to really gather the top five or six for more accurate sample classification.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:24:53+00:00Added an answer on May 11, 2026 at 11:24 am

    I can suggest an alternative algorithm which you’ll have to code 🙂

    Use a heap of size K where K denotes the count of top elements you want to save. Initialize this to the first K elements of your original array. For all N – K elements walk the array, inserting as and when required.

    proc top_k (array<n>, heap<k>) heap <- array<1..k-1> for each (array<k..n-1>)    if array[i] > heap.min      heap.erase(heap.min)      heap.insert(array[i])   end if end for 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 252k
  • Answers 252k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found a script that seems to do exactly that,… May 13, 2026 at 9:38 am
  • Editorial Team
    Editorial Team added an answer $handle = new Upload($_SERVER['DOCUMENT_ROOT'].$_POST['tempfile']); it should be $handle = new… May 13, 2026 at 9:38 am
  • Editorial Team
    Editorial Team added an answer I'm not sure if I understood your problem correctly but… May 13, 2026 at 9:38 am

Related Questions

I'm struggling to create a generic (or untyped) array in C (I'm aware that
I'm working on an old-school image warp filter. Essentially, I have a 2D array
I have a huge dictionary of blank values in a variable called current like
I have a byte stream I need parsed into a struct, and I also

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.