pacifistprogrammer
pacifistprogrammer

Reputation: 3

Characters in array are being removed when sorting a string

I am writing a class to print the given string to lexicographical order but in the final method 'reverseOrder()' the array is automatically deleting values from it (I suppose). The method reverseOrder is deleting some characters for selected string such as "dkhc". Thanks

Debugging shows the values are correctly added to the array.

public class LexicalOrder {
    static String biggerIsGreater(String w) {
        String finalString = "";
        char[] charArr = w.toCharArray();
        int largestX = -1;
        // Find the largest x such that P[x]<P[x+1].
        // (If there is no such x, P is the last permutation.)
        for (int i = 0; i < charArr.length - 1; i++) {
            if (charArr[i] < charArr[i + 1]) {
                largestX = i;
            }
        }
        if (largestX == -1) {
            finalString = "no answer";
        }
        int largestY = -1;
        if (largestX != -1) {
            for (int j = 0; j < charArr.length; j++) {
                if (charArr[largestX] < charArr[j]) {
                    largestY = j;
                }
            }
            charArr = swap(charArr, largestX, largestY);
            charArr = reverseOrder(charArr, largestX + 1);
            finalString = new String(charArr);
        }
        return finalString;
    }

    // Method to swap characters in index largestX and largestY
    public static char[] swap(char[] a, int largestX, int largestY) {
        char temp = a[largestY];
        a[largestY] = a[largestX];
        a[largestX] = temp;
        return a;
    }

    // Method to reverse the order of the array from the index
    // largestX + 1 to n (n being last element of array)
    public static char[] reverseOrder(char[] a, int index) {
        int step = 0;
        char[] newArr = new char[a.length];
        for (int j = 0; j < index; j++) {
            System.out.println(j);
            newArr[j] = a[j]; // adding elements to new arr till index=largestX
            System.out.println(newArr[j] = a[j]);
        }
        for (int i = index; i < a.length; i++) {
            System.out.println(i);
            newArr[index] = a[a.length - step - 1]; // adding remaining values but with reversing order
            System.out.println(newArr[index] = a[a.length - step - 1]);
            step++;
        }
        for (char c : newArr)
            System.out.println(c);
        return newArr;
    }

    public static void main(String[] args) {
        System.out.println(biggerIsGreater("dkhc"));
    }
}

What is expected is = hcdk.

But what I get is = hk

I ran the method to check and it clearly adds those elements to the "newArr" array in method 'reverseOrder()' as shown below.

0
h
1
c
2
d
3
k

Output - hk (and two spaces)

The two characters are replaced by two spaces for some reason. P.S : I am following the steps mentioned here link

Note: It works for some words such as "lmno,dcba,etc"

Upvotes: 0

Views: 92

Answers (1)

Rmahajan
Rmahajan

Reputation: 1361

What you have missed is index++. In reverseOrder method increment index++ will give you hcdk expected output.

public static char[] reverseOrder(char[] a, int index) {
        int step = 0;
        char[] newArr = new char[a.length];
        for (int j = 0; j < index; j++) {
            System.out.println(j);
            newArr[j] = a[j]; // adding elements to new arr till index=largestX
            System.out.println(newArr[j] = a[j]);
        }
        for (int i = index; i < a.length; i++) {
            System.out.println(i);
            newArr[index] = a[a.length - step - 1]; // adding remaining values
                                                    // but with reversing order
            System.out.println(newArr[index] = a[a.length - step - 1]);
            step++;index++;
        }
        for (char c : newArr)
            System.out.println(c);
        return newArr;
    }

In your case index value is not getting incremented due to which 2 and 3 item in an array are blank.

Upvotes: 1

Related Questions