0

What is the mistake in my code?

Given a string consisting of lowercase letters, arrange all its letters in ascending order.

Input: The first line of the input contains T, denoting number of testcases. Then follows description of each testcase. The first line of the testcase contains positive integer N denoting the length of string. The second line contains the string.

Output: For each testcase, output the sorted string.

Constraints:

1 <= T <= 100
1 <= N <= 100
import java.util.*;
class GFG {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int i = 1; i <= t; i++) {
            int n = sc.nextInt();
            sc.nextLine();
            String S = sc.nextLine();
            String sor = "";
            for (int j = 0; j < n; j++) {
                int min = j;
                for (int k = j + 1; k < n; k++) {
                    if (S.charAt(k) > S.charAt(min)) {
                        min = k;
                    }
                }
                sor += S.substring(min, min + 1);
            }
            System.out.println(sor);
        }
    }
}

For Input:

1
5
edcab

Output:

edcbb

Expected Output:

abcde

3 Answers3

0

You are not exchanging the place of the min character after finding it. But Strings in java are immutable so you can't swap the places of characters in it. I suggest you convert your String to a char[] so that you can swap the characters:


public static void main (String[] args){
     Scanner sc = new Scanner(System.in);
     int t = sc.nextInt();
     for(int i=1; i<=t; i++){
         int n = sc.nextInt();
         sc.nextLine();
         String S = sc.nextLine().toCharArray(); // convert it to char array
         char[] sor = new char[S.length];
         for(int j=0; j<n; j++){
             int min = j;
             for(int k =j+1; k<n; k++){
                 if(S[k]<S[min]){
                     min = k;
                 }
             }
             swap(S, min, j);
             sor[j] = S[min]
         }
         System.out.println(new String(sor));// reconvert to string
     }
}
public static void swap(char[] c,int x,int y){
    char temp= c[x];
    c[x] = c[y];
    c[y] = temp;
}

Omid.N
  • 824
  • 9
  • 19
0
import java.util.*;

class GFG {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int i = 1; i <= t; i++) {
            int n = sc.nextInt();
            sc.nextLine();
            
            String S = sc.nextLine();
            System.out.println("S: "+S);
            String sor = "";
            for (int j = 0; n > 0; j++) {
                int min = 0;
                for (int k = 0; k < n; k++) {
                    if (S.charAt(k) < S.charAt(min)) {
                        min = k;
                    }
                }
                sor += S.substring(min, min + 1);
                S = S.substring(0, min) + S.substring(min + 1);
                n--;
            }
            System.out.println(sor);
        }
    }
}

This code does what you want. I changed > to < and I removed every char that added to sorted String from unsorted String. This way we don't need to deal with the same char again and again.

Berdan Akyürek
  • 212
  • 1
  • 14
0

You can use String.toCharArray method to iterate over the array of characters char[] of this string, sort their decimal values and return back the string that contains the characters of the sorted array:

public static void main(String[] args) {
    String str = "edcab";
    String sorted = selectionSort(str.toCharArray());
    System.out.println(sorted); // abcde
}
public static String selectionSort(char[] arr) {
    // iterate over all subsets of the array
    // (0-last, 1-last, 2-last, 3-last, ...)
    for (int i = 0; i < arr.length; i++) {
        // assume the min is
        // the first element
        char min = arr[i];
        // index of the
        // min element
        int min_i = i;
        // check the elements
        // after i to find
        // the smallest
        for (int j = i + 1; j < arr.length; j++) {
            // if this element
            // is less, then it
            // is the new min
            if (arr[j] < min) {
                min = arr[j];
                min_i = j;
            }
        }
        // if min element is not
        // equal to the current
        // one, then swap them
        if (i != min_i) {
            char temp = arr[i];
            arr[i] = arr[min_i];
            arr[min_i] = temp;
        }
    }
    return String.valueOf(arr);
}

You can use String.codePoints method to iterate over int values of the characters of this string, sort them and collect another sorted string:

String str = "edcab";

String sorted = str.codePoints()
        .sorted()
        .mapToObj(Character::toString)
        .collect(Collectors.joining());

System.out.println(sorted); // abcde

See also:
How do I not take Special Characters in my Password Validation (without Regex)?
Java Selection Sort