1

Here is the given question: Given a string S you have to sort the characters of the string in lexicographically decreasing order. Note - Do not use the inbuilt sorting function.

#include <bits/stdc++.h>
#include <string>
  using namespace std;
  
  int main()
  {
    //write your code here
    int t;
    cin>>t;
    while(t--)
    {
      int n,temp,min,i,j;
      string s,text;
      cin>>s;
      n=s.length();
      int arr[n];
      for(int i=0;i<n;i++)
      {
        arr[i]=int(s[i]);
      }
      for(i=0;i<n;i++)
        {       
            for(j=i+1;j<n;j++)
            {
                if(arr[i]<arr[j])
                {
                    temp  =arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
                }
            }
        }
      for(int l=0;l<n;l++)
      {
        text[l]=arr[l];
        cout<<text[l];
      }
      
      cout<<endl;
    }
    return 0;
  }

Input

1

algorithm

Output

tromlihga

  • 1
    Tip: Use `std::vector` and `push_back()` rather than non-standard Variable Length Arrays like `int arr[n]`. – tadman May 05 '21 at 16:41
  • 2
    What is `s[i]-0` supposed to be doing? Is this sorting individual characters in an array? If so, [solved problem](https://stackoverflow.com/questions/9107516/sorting-characters-of-a-c-string). Just `std::sort` the string itself. Easy. Done. – tadman May 05 '21 at 16:42
  • [Why should I **not** `#include `?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) – Ted Lyngmo May 05 '21 at 16:45
  • `text` is an empty string, anything but `text[0]` is out-of-bounds. And again you are doing much more than necessary. Instead of copying from `arr[l]` to `text[l]` and then print `text[l]` in a loop, just do `std::cout << arr;` – 463035818_is_not_an_ai May 05 '21 at 16:49
  • why did you replace the call to `sort` with that? The code was unnecessarily complicated before, now its a bit worse... – 463035818_is_not_an_ai May 05 '21 at 16:54
  • Are you sure that none of the strings have spaces in them? – Ted Lyngmo May 05 '21 at 16:55
  • @largest_prime_is_463035818 your suggestion worked Thank you. – Sasi Kamalesh Vadlani May 05 '21 at 16:59

1 Answers1

2

For starters the variable t should be declared as having an unsigned integer type. Otherwise the user can enter a negative number.

Variable length arrays like this array declared in your program

int arr[n];

is not a standard C++ feature.

This for loop

  for(int l=0;l<n;l++)
  {
    text[l]=arr[l];
    cout<<text[l];
  }

invokes undefined behavior because the object text is empty. So you may not use the subscript operator to change the object.

To sort a string in the lexicographical descending order there is no need to use an auxiliary array,

Sort elements of the string s in place.

Your approach of sorting a string but without auxiliary arrays can look the following way as it is shown in the demonstrative program below.

#include <iostream>
#include <string>

int main() 
{
    std::string s( "ABCDEFGHIJ" );
    
    std::cout << s << '\n';

    for ( std::string::size_type i = 0, n = s.length(); i < n; i++ )
    {
        for ( std::string::size_type j = i + 1; j < n; j++ )
        {
            if ( s[i] <  s[j] )
            {
                char c = s[i];
                s[i] = s[j];
                s[j] = c;
            }
        }
    }

    std::cout << s << '\n';
    
    return 0;
}

The program output is

ABCDEFGHIJ
JIHGFEDCBA
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335