1

So I have an arraylist that I want that I need to sort with an insertion sort algorithm for a programming class. I have this and ArrayList containing these String's = (Andrew, Felip, Juan, Camil, Jhon, William) and I have this Java code:

public void insertionSort( )
    {

        ArrayList<Reserve> array = giveReserves();

        for(int i = 1   ; i < array.size()-1; i++)
        {
            Reserve element = array.get(i);
            String nomI = element.giveNameClient();
            int j = i;
            String nomJM = array.get(j-1).giveNameClient();
            String nomJ = array.get(j).giveNameClient();
            while(j > 0 && (nomJM.compareTo(nomJ) > 0))
            {
                Reserve temp = array.get(j);
                array.set(j, array.get(j-1));
                array.set(j-1, temp);
                j = j-1;
            }
        }
    }

So I have an hotel that has an ArrayList of reserve's, each reserve has the name of the client that did it. What I want to do, is to sort the Reserve ArrayList by client name.

So I have a method that prints each name client, like this:

public void showNames()
    {
        for(Reserve x: reserves)
        {
            System.out.print(x.giveNameClient() +" ");
        }
    }

in the main() method I print the names of the clients before sorting and then sorted. btw the arraylist of reserves is in a class named Test.

public static void main(String args[])
    {
        Test object = new Test();
        System.out.println("Names: ");
        object.showNames();
        object.insertionSort();
        System.out.println();
        System.out.println("after sorting: ");
        object.showNames();
    }

Now when I compile this I get the following:

Names:
Juan Jhon Camil William Andrew Felip
after sorting: 
Andrew Camil Jhon Juan William Felip

The thing is that the output should be Andrew Camil Felip Jhon Juan William

Thanks.

3
  • Check your loop condition. You are ignoring the last element in your list Commented Jan 30, 2016 at 2:27
  • Yes I know I am ignoring that last element, but I cant see why Commented Jan 30, 2016 at 2:34
  • Refer this for a Generic insertion sort Commented Mar 15, 2016 at 11:29

2 Answers 2

1

This should give you your desired output. There are numerous issues in your code. You're skipping elements in the array with your i < array.size() - 1 loop definition.

String[] inputArray = {"Juan", "Jhon", "Camil", "William", "Andrew", "Felip"};
for(int i = 1; i < inputArray.length; i++)  {
    String key = inputArray[i];
    int j = i - 1;

    while (j >= 0 && key.compareTo(inputArray[j]) < 0) {
        inputArray[j + 1] = inputArray[j];
        j--;
    }
    inputArray[j + 1] = key;
}
System.out.println(Arrays.toString(inputArray));
Sign up to request clarification or add additional context in comments.

Comments

1

Try to change the loop's condition to: i < array.size() instead of i < array.size()-1.

Using i < array.size()-1 makes sense when you're accessing elements at i and i+1 but you seems to access the elements at i and i - 1.

1 Comment

I changed the size()-1 to size() only and the output I got was Felip Andrew Camil Jhon Juan William. So Felip is still out of place, I dont know why.

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.