Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort array of strings which contains both negative and positive numbers in c++.?

String str[]={"-123","89","-10","456"};

str is an array of strings, with each string in the format of an integer, and you have to perform sorting on this array in O(n log n) time.

The strings in str can represent both positive and negative integers. The maximum length of these strings is 1024 characters.

I know one solution of this problem is to convert the strings into numbers, then compare them apart from this; is there any other solution to this problem?

like image 414
Emp1 Avatar asked Oct 23 '19 05:10

Emp1


2 Answers

Another solution is to implement your own comparison function:

  • Check the first character of both strings. If one starts with a digit and the other starts with a -, then the string that starts with - is the smaller number.
  • If both strings start with a digit, then compare the length of the strings. The shorter string is the smaller number. If both strings are the same length, perform a standard string compare.
  • If both strings start with -, then compare the length of the strings. The longer string is the smaller number. If both strings are the same length, perform a standard string compare, but negate the result.
like image 137
user3386109 Avatar answered Oct 13 '22 18:10

user3386109


Here's a minimal and potentially insufficient (doesn't handle leading zeroes, whitespace, etc.) example that does what you'd like.

The comments explain what it's doing. :)

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

int main() {
  std::vector<std::string> strings = {
      "-1", "-1", "-20", "-4", "3", "0", "-0", "1", "20", "20", "44020",
  };

  // Assumes everything in "strings" has no whitespace in it.
  // Assumes everything in "strings" does not have leading zeroes.
  // Assumes everything in "strings" is an ascii representaion of an integer.
  // Assumes everything in "strings" is nonempty.
  std::sort(strings.begin(), strings.end(),
            [](const std::string &a, const std::string &b) {
              const bool a_is_negative = a[0] == '-';
              const bool b_is_negative = b[0] == '-';
              if (a_is_negative != b_is_negative) {
                // If they have different signs, then whichever is negative is
                // smaller.
                return a_is_negative;
              } else if (a.length() != b.length()) {
                // If they have the same sign, then whichever has more
                // characters is larger in magnitude. When the sign is negative,
                // the longer (more digits) number is "more negative". When
                // positive, the longer (more digits) number is "more positive".
                return (a.length() < b.length()) != a_is_negative;
              } else {
                // Otherwise a lexicographic comparison of "a" and "b" will
                // determine which string is larger in magnitude. Using the same
                // logic above, we account for the "negative vs. positive"
                // comparison.
                return (a < b) != a_is_negative;
              }
            });

  for (const auto &str : strings) {
    std::cout << str << " ";
  }
  std::cout << std::endl;
}
like image 26
druckermanly Avatar answered Oct 13 '22 19:10

druckermanly