Non conventional Sorting HELP!

I'm pretty new to Java and need some help with a project.

For this project, I need to sort an ArrayList of numbers, but not in the usual convention.

Example:

Here is the set of random numbers: <1,12,53,3,5,10,9,6>

I need the program to sort it like this: <1,12,3,53,5,10,6,9>

So, I need it to sort Low, High, Low High, etc...

I was initially going to use a merge sort, but couldn't figure out how to do that.

Then I thought maybe just doing something simple like if(a.get(i) < a.get(i+1)) then keep a.get(i) in its place. However if a.get(i) is greater that i+1, then I would have it swap.

I just need help swapping the values.

In any case, here is my current code:

import java.util.*;

public class JollySort

{

public static void main(String[] args)

{

ArrayList<Integer> numbers = new ArrayList<Integer>();

numbers.add(1);

numbers.add(5);

numbers.add(18);

numbers.add(2);

numbers.add(12);

numbers.add(10);

numbers.add(8);

numbers.add(7);

numbers.add(14);

numbers.add(32);

numbers.add(9);

numbers.add(4);

numbers.add(5);

numbers.add(19);

numbers.add(15);

numbers.add(65);

numbers.add(8);

numbers.add(41);

numbers.add(6);

numbers.add(13);

for(int i = 0; i < numbers.size(); i++)

{

if(numbers.get(i) < numbers.get(i+1))

i = i;

else

numbers.get(i+1).add(i);

}

}

}

Re: Non conventional Sorting HELP!

Quote:

I just need help swapping the values.

Have you tried using a temp variable to hold one value while you move the other value into its slot?

Re: Non conventional Sorting HELP!

No, but I will try that now and let you know how that goes.

Re: Non conventional Sorting HELP!

Ok, so this is what I did, but I get an error. Maybe you can check it out for me and let me know what I am doing wrong.

import java.util.*;

public class JollySort

{

public static void main(String[] args)

{

ArrayList<Integer> numbers = new ArrayList<Integer>();

numbers.add(1);

numbers.add(5);

numbers.add(18);

numbers.add(2);

numbers.add(12);

numbers.add(10);

numbers.add(8);

numbers.add(7);

numbers.add(14);

numbers.add(32);

numbers.add(9);

numbers.add(4);

numbers.add(5);

numbers.add(19);

numbers.add(15);

numbers.add(65);

numbers.add(8);

numbers.add(41);

numbers.add(6);

numbers.add(13);

int j = 0;

for(int i = 0; i < numbers.size(); i++)

{

if(numbers.get(i) < numbers.get(i+1))

i = i;

else if (numbers.get(i) > numbers.get(i+1))

{

j = numbers.get(i);

numbers.remove(i);

numbers.add(i+1, j);

}

}

}

}

Re: Non conventional Sorting HELP!

Please copy and paste the full text of the error message here.

Re: Non conventional Sorting HELP!

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 20, Size: 20

at java.util.ArrayList.RangeCheck(ArrayList.java:547)

at java.util.ArrayList.get(ArrayList.java:322)

at JollySort.main(JollySort.java:32)

Re: Non conventional Sorting HELP!

Quote:

Originally Posted by

**Norm** Please copy and paste the full text of the error message here.

Any idea what could be wrong with the code?

Re: Non conventional Sorting HELP!

Quote:

IndexOutOfBoundsException: Index: 20, Size: 20

at java.util.ArrayList.RangeCheck(ArrayList.java:547)

at java.util.ArrayList.get(ArrayList.java:322)

at JollySort.main(JollySort.java:32)

You have an index to an array that is Out Of Bounds/past the end of the array.

Look at line 32 and see what is the value of the index you are using there. Is its value past the end of the array?

Remember that indexes for arrays go from 0 to the array's length-1

If the array has 20 elements, the largest index is 19.

Re: Non conventional Sorting HELP!

Quote:

Originally Posted by

**Norm** You have an index to an array that is Out Of Bounds/past the end of the array.

Look at line 32 and see what is the value of the index you are using there. Is its value past the end of the array?

Remember that indexes for arrays go from 0 to the array's length-1

If the array has 20 elements, the largest index is 19.

Ok, so I added i--; to the end of the for loop, but now I am in an infinite loop, and it is only printing "1"

Re: Non conventional Sorting HELP!

I guess not letting the loop control value increase was not the answer.

What other ways do you have for keeping the index from going past the end of the array?

What about the terminating expression in the for statement?

Re: Non conventional Sorting HELP!

Quote:

Originally Posted by

**Norm** I guess not letting the loop control value increase was not the answer.

What other ways do you have for keeping the index from going past the end of the array?

What about the terminating expression in the for statement?

I honestly have no idea.

Re: Non conventional Sorting HELP!

Quote:

I honestly have no idea.

If you don't know how a for statement works, here's a link where you can read about it:

The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

You never posted the statement where the error occured. Have you looked at that statement to see why/how the index goes past the end of the array?

Re: Non conventional Sorting HELP!

It might be easier for you to create a linked list or a binary search tree for sorting

Re: Non conventional Sorting HELP!

@adjit - Did you look at the sorting rules?

Re: Non conventional Sorting HELP!

@norm - yes I did, so what I was thinking was maybe a binary search tree, only using the lower half and then just putting them into the array list one after the other and then just throwing the other numbers in the order that they were added into the 'blank' spaces