Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How would I alter my code?

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How would I alter my code?

    If my code:

    public void sortArray () {
    int beginP = 0;
    int endP = myArray.length-1;
    while(beginP<endP){
      if((myArray[beginP]!=GREEN) && (myArray[endP]!=BLUE)){
        swap(beginP, endP);
      }
      while((beginP < myArray.length) && (myArray[beginP]==GREEN)){
        beginP++;
      }
      while(myArray[endP]==BLUE){
        endP--;
      }
    }

    produced: G G G B B B B

    how would I alter it so that it would produce: B B B B G G G?



    I feel as though I am missing one step, but I cannot put my finger on it.


  2. #2
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Re: How would I alter my code?

    Alter it how? Why can't you just use the same sorting algorithm and change every BLUE with a GREEN and vice versa?

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How would I alter my code?

    I tried that before, I don't know why that didn't work.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How would I alter my code?

    Have you tried debugging the code by adding some println()s to show what is happening?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How would I alter my code?

    I would recommend implementing a compareTo() method that determines which color comes before which.

    Something like:

    /**
     * Blue comes before green.
     *
     * Returns less than 0 if item1 comes before item2
     * Returns 0 if item1 comes at the same place as item2
     * Returns greater than 0 if item1 comes after item2
     */
    public static int compareTo(ColorItem item1, ColorItem item2)
    {
        int val1 = 0;
        int val2 = 0;
        if(item1 == BLUE)
        {
            val1 = 1;
        }
        else
        {
            val1 = 2;
        }
     
        if(item2 == BLUE)
        {
            val2 = 1;
        }
        else
        {
            val2 = 2;
        }
     
        return val1 - val2;
    }

    Then, somewhere in your code implement a standard sorting algorithm (say, selection sort because it's very easy).