-9

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.

2 Answers2

2

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
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • Can u please do it for std::qsort where i will give an input for total number of string – Jahirul Islam Monir Aug 31 '16 at 17:13
  • 2
    why do you want to use std::qsort? std::sort is so simple – Stack Danny Aug 31 '16 at 17:14
  • 1
    @JahirulIslamMonir Why do you need to use `qsort`? If you're programming in C++ then use C++ algorithms instead of old C functions. – Some programmer dude Aug 31 '16 at 17:14
  • 1
    Oh, yeah and it's old c style, Like Joachim Pileborg said. – Stack Danny Aug 31 '16 at 17:16
  • 1
    @StackDanny Then you might as well tag your question as C. If you are using C++ you should use C++ and not *C with Classes* – NathanOliver Aug 31 '16 at 17:17
  • 1
    I am marking it as a Accepted answer.. Those who gave negative vote i have nothing to say them.. I just only word to say that i am a beginner in programming and also in Stackoverflow – Jahirul Islam Monir Aug 31 '16 at 17:22
  • 1
    @JahirulIslamMonir thank you. Also, if you'd like to avoid negative votes, show us what you have tried so far. And if you have no clue but you really need to do this, try to look for examples sorting strings (there are tons of). Or at least, ask what's up with std::qsort (like what do I need to pass over). Just tell a little bit more about your problem. :) good luck on next question! – Stack Danny Aug 31 '16 at 17:33
1

It just so happens to be that a string containing only digits is alphabetically sortable, so you just put each string into a vector, and simply sort the vector.

As noted, this only works if the "numbers" all have the same number of digits. Else you need pad the strings with leading zeroes so they are all of the same length. The leading zeroes can then be removed once you have sorted the vector.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621