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 those stings with minimum number of time.The size of the all inputed string will be equal.How can i sort these string with faster time.
922003001020293839297830207344987344973074734
766352786207892397340783784078348747606208602
182823068326283756515117829362376823572395775
//the size of all string is equal
Anyone please explain the better way of sorting.
答案 0 :(得分: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.
答案 1 :(得分:1)
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