Non conventional Sorting HELP!

Printable View

• 01-02-2012, 10:39 PM
csicj
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);

}

}
}
• 01-02-2012, 10:41 PM
Norm
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?
• 01-02-2012, 10:43 PM
csicj
Re: Non conventional Sorting HELP!
No, but I will try that now and let you know how that goes.
• 01-02-2012, 10:51 PM
csicj
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);
}
}

}
}
• 01-02-2012, 11:00 PM
Norm
Re: Non conventional Sorting HELP!
Quote:

I get an error.
Please copy and paste the full text of the error message here.
• 01-02-2012, 11:04 PM
csicj
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)
• 01-02-2012, 11:22 PM
csicj
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?
• 01-02-2012, 11:24 PM
Norm
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.
• 01-02-2012, 11:39 PM
csicj
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"
• 01-02-2012, 11:42 PM
Norm
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?
• 01-02-2012, 11:45 PM
csicj
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.
• 01-02-2012, 11:48 PM
Norm
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?
• 01-03-2012, 02:11 AM
adjit
Re: Non conventional Sorting HELP!
It might be easier for you to create a linked list or a binary search tree for sorting
• 01-03-2012, 02:32 AM
Norm
Re: Non conventional Sorting HELP!
@adjit - Did you look at the sorting rules?
• 01-03-2012, 05:58 AM
adjit
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