# Thread: Non conventional Sorting HELP!

1. Member
Join Date
Jan 2012
Posts
7
Rep Power
0

## 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>();

for(int i = 0; i < numbers.size(); i++)
{
if(numbers.get(i) < numbers.get(i+1))
i = i;
else

}

}
}

2. ## Re: Non conventional Sorting HELP!

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?

3. Member
Join Date
Jan 2012
Posts
7
Rep Power
0

## Re: Non conventional Sorting HELP!

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

4. Member
Join Date
Jan 2012
Posts
7
Rep Power
0

## 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>();

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);
}
}

}
}

5. ## Re: Non conventional Sorting HELP!

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

6. Member
Join Date
Jan 2012
Posts
7
Rep Power
0

## 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)

7. Member
Join Date
Jan 2012
Posts
7
Rep Power
0

## Re: Non conventional Sorting HELP!

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?

8. ## Re: Non conventional Sorting HELP!

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.

9. Member
Join Date
Jan 2012
Posts
7
Rep Power
0

## Re: Non conventional Sorting HELP!

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"

10. ## 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?

11. Member
Join Date
Jan 2012
Posts
7
Rep Power
0

## Re: Non conventional Sorting HELP!

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.

12. ## Re: Non conventional Sorting HELP!

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?

13. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

## Re: Non conventional Sorting HELP!

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

14. ## Re: Non conventional Sorting HELP!

@adjit - Did you look at the sorting rules?

15. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

## 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