Results 1 to 15 of 15
Thread: Non conventional Sorting HELP!
 01022012, 10:39 PM #1Member
 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>();
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);
}
}
}
 01022012, 10:41 PM #2
Re: Non conventional Sorting HELP!
I just need help swapping the values.
 01022012, 10:43 PM #3Member
 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.
 01022012, 10:51 PM #4Member
 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>();
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);
}
}
}
}
 01022012, 11:00 PM #5
Re: Non conventional Sorting HELP!
I get an error.
 01022012, 11:04 PM #6Member
 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)
 01022012, 11:22 PM #7Member
 Join Date
 Jan 2012
 Posts
 7
 Rep Power
 0
 01022012, 11:24 PM #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)
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 length1
If the array has 20 elements, the largest index is 19.
 01022012, 11:39 PM #9Member
 Join Date
 Jan 2012
 Posts
 7
 Rep Power
 0
 01022012, 11:42 PM #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?
 01022012, 11:45 PM #11Member
 Join Date
 Jan 2012
 Posts
 7
 Rep Power
 0
 01022012, 11:48 PM #12
Re: Non conventional Sorting HELP!
I honestly have no idea.
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?
 01032012, 02:11 AM #13Senior 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
 01032012, 02:32 AM #14
Re: Non conventional Sorting HELP!
@adjit  Did you look at the sorting rules?
 01032012, 05:58 AM #15Senior 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
Similar Threads

Sorting by Last Name
By burrish in forum New To JavaReplies: 4Last Post: 10152011, 06:39 PM 
Sorting
By amzers in forum NetBeansReplies: 1Last Post: 10042011, 07:06 PM 
little help with sorting
By drgnfire25 in forum New To JavaReplies: 3Last Post: 01152011, 10:23 PM 
Help in sorting
By nn12 in forum New To JavaReplies: 3Last Post: 01082011, 07:15 PM 
sorting
By jot321 in forum New To JavaReplies: 18Last Post: 10022008, 11:30 AM
Bookmarks