
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sorting Strings with Decimal Points in C++
A string containing decimal points is sorted to solve complex programming problems. This question was asked in the Accenture coding assessment in 2025. In C++, there are different ways to sort such strings efficiently. In this article, we are going to discuss various approaches to sorting strings that contain decimal points using C++.
How to Sort Strings with Decimal Points?
In this problem, we are given an array of strings, where each string represents a decimal number. The goal is to sort these strings in increasing numerical order while ensuring the correct handling of decimal values.
Example 1
-
Input:
string array = {"3.14", "2.71", "1.618", "10.01", "9.99"} -
Output:
{"1.618", "2.71", "3.14", "9.99", "10.01"}
Explanation:
The strings are sorted based on their numerical values.
Example 2
-
Input:
string array = {"0.1", "0.01", "1.0", "0.001", "10.1"}
Output:
{"0.001", "0.01", "0.1", "1.0", "10.1"}
Explanation:
The strings are sorted numerically rather than lexicographically.
Different Approaches to Sorting Strings with Decimal Points
Using a Simple Sorting Algorithm
This is the simplest and most direct approach. In this approach, we convert the strings into floating-point numbers and use the in-built sort() function to sort them numerically.
Steps for Implementation
- Take the input for the array of strings.
- Convert each string into a floating-point number.
- Sort the array using the sort() function.
- Output the sorted array.
Implementation Code:
#include<bits/stdc++.h> using namespace std; int main() { vector<string> strArray = {"3.14", "2.71", "1.618", "10.01", "9.99"}; sort(strArray.begin(), strArray.end(), [](const string &a, const string &b) { return stod(a) < stod(b); }); cout << "Sorted strings: "; for (const string &s : strArray) { cout << s << " "; } return 0; }
Output:
Sorted strings: 1.618 2.71 3.14 9.99 10.01
Time Complexity: O(N log N)
Space Complexity: O(1)
Using a Custom Comparator Approach
Instead of converting each value inside the sort() function, we can define a separate comparator function to perform the comparison.
Steps for Implementation
- Define a comparator function that converts the strings to doubles and compares them.
- Use sort() with the custom comparator.
- Print the sorted array.
Implementation Code:
#include<bits/stdc++.h> using namespace std; bool compareStrings(const string &a, const string &b) { return stod(a) < stod(b); } int main() { vector<string> strArray = {"4.2", "1.1", "3.3", "2.9", "5.0"}; sort(strArray.begin(), strArray.end(), compareStrings); cout << "Sorted strings: "; for (const string &s : strArray) { cout << s << " "; } return 0; }
Output:
Sorted strings: 1.1 2.9 3.3 4.2 5.0
Time Complexity: O(N log N)
Space Complexity: O(1)
Using the Prefix Sum Array Approach for Optimized Sorting
The sort() method approach is efficient for sorting the array with decimal points. We can use another way to enhance its performance for repeated queries is by using a prefix sum array technique to preprocess numerical values for fast comparisons.
Steps for Implementation
- Convert the string array into a numerical array using a prefix sum.
- Store the original string indexes to maintain mapping.
- Sort the numerical values while maintaining the original order.
- Output the sorted strings.
Implementation Code:
#include<bits/stdc++.h> using namespace std; int main() { vector<string> strArray = {"8.88", "7.77", "9.99", "5.55", "6.66"}; int n = strArray.size(); vector<pair<double, string>> mappedArray; for (int i = 0; i < n; i++) { mappedArray.push_back({stod(strArray[i]), strArray[i]}); } sort(mappedArray.begin(), mappedArray.end()); cout << "Sorted strings: "; for (auto &p : mappedArray) { cout << p.second << " "; } return 0; }
Output:
Sorted strings: 5.55 6.66 7.77 8.88 9.99
Time Complexity: O(N log N)
Space Complexity: O(N)