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 3603320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:51:27+00:00 2026-05-18T20:51:27+00:00

In C++ would like to sort a lengthy ( 2^20 ) vector of reals,

  • 0

In C++ would like to sort a lengthy (2^20) vector of reals, obviously sort() does the trick. Having used R before I was used to the nice order() function which yields the permutation that leads to the sorted vector.

Example:

x = {24, 55, 22, 1}

Then the permutation

perm = {3, 2, 0, 1}

Maps the original x to the sorted x in ascending order.

I can probably implement some bubble sort which does not only sort x but performs the same transpositions on the vector {0,1,2,...} and outputs both, but I believe someone must have thought about it and especially have done it efficiently.

  • 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-05-18T20:51:28+00:00Added an answer on May 18, 2026 at 8:51 pm

    Edit

    Better than before approach without using helper vectors: (source on ideone):

    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    template<class Vals>
    void sortingPermutation(const Vals& values, std::vector<int>& v){
      int size = values.size(); 
      v.clear(); v.reserve(size);
      for(int i=0; i < size; ++i)
        v.push_back(i);
    
      std::sort(v.begin(), v.end(), [&values](int a, int b) -> bool { 
        return values[a] < values[b];
      });
    }
    
    int main()
    {
        std::vector<double> values;
        values.push_back(24);
        values.push_back(55);
        values.push_back(22);
        values.push_back(1);
    
        std::vector<int> permutation;
        sortingPermutation(values, permutation);
    
        typedef std::vector<int>::const_iterator I;
        for (I p = permutation.begin(); p != permutation.end(); ++p)
            std::cout << *p << " ";
        std::cout << "\n";
    }
    

    I am using lambda from C++0x, but it can be replaced with simple functor object:

    template<class T>
    struct CmpPairs{
      CmpPairs(const std::vector<T> &v): v_(v) {}
      std::vector<T> v_;
      bool operator()(int a, int b){ return v_[a] < v_[b]; }
    };
    
    template<class T>
    CmpPairs<T> CreateCmpPairs(const std::vector<T> & v) { return CmpPairs<T>(v); }
    //in sortingPermutation:
    std::sort(v.begin(), v.end(), CreateCmpPairs(values));
    

    Source of old solution with std::map: ideone

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

Sidebar

Related Questions

I would like to create some sort of function where you pass in a
I would like to have a unique sort function for several associative arrays. The
I would like to sort this array but this code works if I do
I would like to sort the data of a core data NSSet (I know
I would like to sort the characters in a string. E.g. echo cba |
I have an array which i would like to sort it based on values
I have an array which i would like to sort it based on values
Given an array 'a' I would like to sort the array by columns sort(a,
I have an array that I would like to sort using a date field
I have a very large multidimensional array and I would like to sort it

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.