Search code examples
c++sortingquicksort

How can i sort a string with integers in c++?


I want to sort some strings of the same size. The size of the string can be very large(10^18). How can I sort all the stings in less possible time? The size of all the inputted string will be equal. How can I sort these strings in less amount of time possible?

922003001020293839297830207344987344973074734
766352786207892397340783784078348747606208602
182823068326283756515117829362376823572395775
//the size of all the strings are equal

Can anyone please explain a better way of sorting? Thanks Your any help will be highly appreciated.


Solution

  • Here it's done with std::sort from the header algorithm

    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <vector>
    
    int main(){
    
        std::vector<std::string> nums{
            "922003001020293839297830207344987344973074734",
            "766352786207892397340783784078348747606208602",
            "182823068326283756515117829362376823572395775"
        };
    
    
        std::cout << "unsorted: " << std::endl;
        for (auto i : nums){
            std::cout << i << std::endl;
        }
    
        std::sort(nums.begin(), nums.end()); //sort it
    
    
        std::cout << "\nsorted: " << std::endl;
        for (auto i : nums){
            std::cout << i << std::endl;
        }
    
        system("pause");
        return 0;
    }
    

    output:

    unsorted: 
    922003001020293839297830207344987344973074734
    766352786207892397340783784078348747606208602
    182823068326283756515117829362376823572395775
    
    sorted: 
    182823068326283756515117829362376823572395775
    766352786207892397340783784078348747606208602
    922003001020293839297830207344987344973074734