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

  • SEARCH
  • Home
  • 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 7859849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:03:16+00:00 2026-06-02T22:03:16+00:00

I have a question that could seem very basic, but it is in a

  • 0

I have a question that could seem very basic, but it is in a context where “every CPU tick counts” (this is a part of a larger algorithm that will be used on supercomputers).

The problem is quite simple : what is the fastest way to sort a list of unsigned long long int numbers and their original indexes ? (At the beginning, the unsigned long long int numbers are in a completely random order.)

Example :
Before
Numbers: 32 91 11 72
Indexes: 0 1 2 3
After
Numbers: 11 32 72 91
Indexes: 2 0 3 1 

By “fastest way”, I mean : what algorithm to use : std::sort, C qsort, or another sorting algorithm available on the web ? What container to use (C array, std::vector, std::map…) ? How to sort the indexes at the same time (use structures, std::pair, std::map…) ?

How many element to sort ? -> typically 4Go of numbers

  • 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. Editorial Team
    Editorial Team
    2026-06-02T22:03:18+00:00Added an answer on June 2, 2026 at 10:03 pm

    The obvious starting point would be a structure with operator< defined for it:

    struct data { 
        unsigned long long int number;
        size_t index;
    };
    
    struct by_number { 
        bool operator()(data const &left, data const &right) { 
            return left.number < right.number;
        }
    };
    

    …and an std::vector to hold the data:

     std::vector<data> items;
    

    and std::sort to do the sorting:

     std::sort(items.begin(), items.end(), by_number());
    

    The simple fact is, that the normal containers (and such) are sufficiently efficient that using them doesn’t make your code substantially less efficient. You might be able to do better by writing some part in a different way, but you might about as easily do worse. Start from solid and readable, and test — don’t (attempt to) optimize prematurely.

    Edit: of course in C++11, you can use a lambda expression instead:

    std::sort(items.begin(), items.end(), 
              [](data const &a, data const &b) { return a.number < b.number; });
    

    This is generally a little more convenient to write. Readability depends–for something simple like this, I’d say sort ... by_number is pretty readable, but that depends (heavily) on the name you give to the comparison operator. The lambda makes the actual sorting criteria easier to find, so you don’t need to choose a name carefully for the code to be readable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize that this is probably a very basic question, but I have spent
This probably is a very very basic question but i can't seem to find
I have a question that must surely seem very trivial, but the answer has
Hello I have a question that could seem complicated. But I will try to
This may seem like a very basic question but I did not seem to
I have a rather simple question that I could normally debug myself, but I
I have a question that is similar to this question , but mine concerns
This may seem like a basic/stupid/obviously-answered question, but I wanted to check: why use
This question might seem very specific but I am in need of some ideas
it seems to me that this is kind of a very easy question, but

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.