Results 1 to 15 of 15
  1. #1
    csicj is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

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

    }

    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default 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. #3
    csicj is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: Non conventional Sorting HELP!

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

  4. #4
    csicj is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

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

    }
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Non conventional Sorting HELP!

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

  6. #6
    csicj is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default 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. #7
    csicj is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: Non conventional Sorting HELP!

    Quote Originally Posted by Norm View Post
    Please copy and paste the full text of the error message here.
    Any idea what could be wrong with the code?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default 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. #9
    csicj is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: Non conventional Sorting HELP!

    Quote Originally Posted by Norm View Post
    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. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default 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. #11
    csicj is offline Member
    Join Date
    Jan 2012
    Posts
    7
    Rep Power
    0

    Default Re: Non conventional Sorting HELP!

    Quote Originally Posted by Norm View Post
    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. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default 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. #13
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Non conventional Sorting HELP!

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

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,587
    Rep Power
    25

    Default Re: Non conventional Sorting HELP!

    @adjit - Did you look at the sorting rules?

  15. #15
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default 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

  1. Sorting by Last Name
    By burrish in forum New To Java
    Replies: 4
    Last Post: 10-15-2011, 06:39 PM
  2. Sorting
    By amzers in forum NetBeans
    Replies: 1
    Last Post: 10-04-2011, 07:06 PM
  3. little help with sorting
    By drgnfire25 in forum New To Java
    Replies: 3
    Last Post: 01-15-2011, 10:23 PM
  4. Help in sorting
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 07:15 PM
  5. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 11:30 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •