0

Here's my java code. (Explanation at the end)

import java.util.Scanner;
import java.util.Arrays;
/**
 *
 * @author Laksh
 */
public class Sort4 {
    public static void Swap(int[] array,int Swap1,int Swap2){
        int temporarySwapper;
        temporarySwapper=array[Swap1];
        array[Swap1]=array[Swap2];
        array[Swap2]=temporarySwapper;
    }
    public static void Swap(String[] array,int Swap1,int Swap2){
        String temporarySwapper;
        temporarySwapper=array[Swap1];
        array[Swap1]=array[Swap2];
        array[Swap2]=temporarySwapper;
    }
    public static String[] alphasort(String[] original, int level, int start, int end ){
        int swaps;
        int[] array=new int[original.length];
        for(int i=0;i<original.length;i++){
            array[i]=(int)original[i].charAt(level);
        }
        do{
            swaps=0;
            for(int i=start;i<end;i++){
                if(array[i]>array[i+1]){
                    Swap(original,i,i+1);
                    Swap(array,i,i+1);
                    swaps++;
                }
            }
        }while(swaps != 0);
        return Arrays.copyOfRange(original, start, end+1);
    }
    public static String repeat(String s,int times){
        String returnString="";
        for(int i=0;i<times;i++){
            returnString+=s;
        }
        return returnString;
    }

        public static void main(String[] args) {
        Scanner input=new Scanner(System.in);        
        System.out.println("Enter 5 Names:");
        int max=Integer.MIN_VALUE;
        String[] name=new String[5];
        for(int i=0;i<5;i++){
            name[i]=input.next();
            if(name[i].length()>max){
                max=name[i].length();
            }
        }
        for(int i=0;i<5;i++){
            if(name[i].length()<max){
                name[i]=name[i]+repeat(" ",max-name[i].length());
            }
        }

        String[] sorted=alphasort(name,0,0,name.length-1);
        for(String c:sorted){
            System.out.println(c);
        }

    }

}

To Clarify the issues I am facing, my code relies on finding ascii integer value of a char, and then sorting an integer array. If the first letter of the string I am trying to is equal to the first of another, the code moves to the second letter, and so on. To avoid string index out of bounds, I used the "repeat" method to add enough spaces to the end of each string so that they are all the same length- as Strings of the same length would prevent this. The issue I am facing is that it sort correctly, but in some places, it fails.

for example, if I pass in "butter" and "butterfly", the two are outputed int he order they were entered, in relation to the other values, which are sorted properly! For example:

butter
cookie
butterfly
cookiemonster
ninja

sorts to

butter       
butterfly    
cookie       
cookiemonster
ninja  

whereas

butterfly
cookie
butter
cookiemonster
ninja

sorts to

butterfly       
butter    
cookie       
cookiemonster
ninja

Help me, as I am unable to find my error.

5
  • 1
    There is no error. The output that you posted shows that both inputs are correctly sorted to the same output with butter coming before butterfly. Commented Mar 8, 2019 at 1:18
  • 1
    can you explain a bit more what's wrong with the output(sorted list)? Commented Mar 8, 2019 at 1:18
  • Oh, I'm sorry. I pasted the output incorrectly. It's been edited, so now you can see the issue with the 2nd output (1st was in fact correct) Commented Mar 8, 2019 at 1:33
  • Why don't you use TreeSet<String>() or RadixSort or Arrays.sort(original)? Commented Mar 8, 2019 at 13:17
  • I guess it is some homework or practice? Commented Mar 9, 2019 at 1:22

1 Answer 1

1

You compared only first letters of each word and the order of words, butterfly and butter, didn't change.

You must compare each letter of each word with a function below:

  int size = myArray.length;

  for(int i = 0; i<size-1; i++) {
     for (int j = i+1; j<myArray.length; j++) {
        if(myArray[i].compareTo(myArray[j])>0) {
           String temp = myArray[i];
           myArray[i] = myArray[j];
           myArray[j] = temp;
        }
     }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.