-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathSorting-big-integers.cpp
45 lines (37 loc) · 1.04 KB
/
Sorting-big-integers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Below is C++ code to sort the Big integers in
// ascending order
#include<bits/stdc++.h>
using namespace std;
// comp function to perform sorting
bool comp(const string &left, const string &right)
{
// if length of both string are equals then sort
// them in lexicographically order
if (left.size() == right.size())
return left < right;
// Otherwise sort them according to the length
// of string in ascending order
else
return left.size() < right.size();
}
// Function to sort arr[] elements according
// to integer value
void SortingBigIntegers(string arr[], int n)
{
// Copy the arr[] elements to sortArr[]
vector<string> sortArr(arr, arr + n);
// Inbuilt sort function using function as comp
sort(sortArr.begin(), sortArr.end(), comp);
// Print the final sorted array
for (auto &ele : sortArr)
cout << ele << " ";
}
// Driver code of above implementation
int main()
{
string arr[] = {"54", "724523015759812365462",
"870112101220845", "8723"};
int n = sizeof(arr) / sizeof(arr[0]);
SortingBigIntegers(arr, n);
return 0;
}