#Java Algorithm Code Problem

36 messages · Page 1 of 1 (latest)

vivid jacinth
#

This is the algorithm I'm supposed to make:

“Here is how a garden gnome sorts a line of flower pots. Basically, he looks at the flower pot next to him and the previous one; if they are in the right order he steps one pot forward, otherwise he swaps them and steps one pot backwards. If there is no previous pot, he steps forwards; if there is no pot next to him, he is done.”

So far I wrote this code:

//import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;


public class MyProgram {

    public static void main(String[] args) {

         Scanner input = new Scanner(System.in);
            System.out.println("Enter 10 numbers: ");
            
            
            int[] array = new int[10];
            
            for(int i = 0; i < array.length; i++)
            {
                array[i] = input.nextInt();
            }
            input.close();
            int counter = 0;
            boolean nowDone = true;
            do {
                
                for(int i = 1; i < array.length; i++)
                {
                    int str = array[i];
                    int newNum = i - 1;
                    while(newNum > -1 && array[newNum] > str)
                    {
                        array[newNum + 1] = array[newNum];
                        counter++;
                        newNum--;
                    }
            
                    array[newNum + 1] = str;
                   
                }
                nowDone = false;
                
            } while(nowDone = true);
            
            System.out.println("The Gnome moved " + counter + " times to organize the pots");

    }

}

For the problem, it's not stopping at the limit of 10 numbers from the user for input. One other error is that it shows an index out of bounds exception error. How would I resolve this?

Thanks

#

Regarding that algorithm, does it follow the correct steps of it? Or is it off in some number?

dusk spade
#

why so many loops?

vivid jacinth
#

Btw, did I write the correct algorithm according to the description?

vivid jacinth
#

I changed nowDone = true to nowDone == true for the while loop condition

#

But its not following the algorithm description

#

Which is that if I entered 1 - 10 as input, it should print out 10 as a result

#

Of how many times it moved

#

To scan through everything

#

And sort

dusk spade
#

and what's the purpose of the for(int i = 1; i < array.length; i++) inside the do{}?

vivid jacinth
#

It does

vivid jacinth
dusk spade
#

so what's the problem?

vivid jacinth
#

Now the problem is that the counter is not recording the number of steps the gnome took to scan through the numbers to see if its organized or not

#

So for the result it should follow this output results:

1 2 3 4 5 6 7 8 9 10 - 10
10 9 8 7 6 5 4 3 2 1 - 100
4 - 1 25 13 19 -5 0 21 19 1 - 52

#

It follows the first

#

But not the rest

dusk spade
#

and why should it be exactly that?

vivid jacinth
vivid jacinth
#

This my new code:

 import java.util.Scanner; //enables user input
import java.util.Arrays; //enables the ability to create arrays


public class MyProgram {

    public static void main(String[] args) {

         Scanner input = new Scanner(System.in);
            System.out.println("Enter 10 numbers: ");
            
            //creates an array for 10 numbers
            int[] array = new int[10];
            
            //recieves 10 input numbers from the user
            for(int i = 0; i < array.length; i++)
            {
                array[i] = input.nextInt();
            }
            input.close();
            //closes the user input 
            int counter = 0;
            //records how many steps the gnome took
            boolean nowDone = true;
            //a boolean for the do-while loop
            do {
                int i = 0;
                boolean exitLoop = true;
                
               do {
                   if(i==0 || array[i-1] <= array[i])
                   {
                        i++;
                        counter++;
                        System.out.println(" " + i);
                   } else {
                       int str = array[i];
                       array[i] = array[i-1];
                       array[i-1] = str;
                       counter++;
                       System.out.println(str);
                   }
                       
               } while(i < array.length) && exitLoop = true);
                    
                
    
                nowDone = false;
                
                //turns false, ending the do-while loop
            } while(nowDone == true);
            
            System.out.println("It took the gnome " + counter + " steps to organize the pots");
            //prints out how many steps the gnome took to organize the pots
    }

}

#

However, it doesn't organize the 10 input numbers to ascending order

#

It's all random

#

And its not following the gnome sorting algorithm

#

Is there an error I'm making in the code?

unreal pivotBOT
dusk spade
#

what's tze purpose of the outer do-while loop?

#

you forget to step one backwards

#

if the ordering is wrong

vivid jacinth
#

Thx for the help!