0

I don't understand what is happening in this code. Re-post from before with code included. Can someone please explain what is happening here? I understand conceptually that the list is being re-ordered one item at a time, but I just cant grasp this code.

import java.io*;

public class Example {

   public static void main(String[] args) throws IOException {

      int age[] = new int[10];
      int i, j;
      int smallest;
      int temp;
      String line; 
      BufferedReader in;
      in = new BUfferedReader(new InputStreamReader(System.in));
      for(i = 0; i<= 9; i++)
      {
       System.out.println("Enter an age: ");
       line = in.readline();
       age[i] = Integer.valueOf(line).intValue();
      }
      for(i = 0; i<= 9, i++) {
         smallest = i;
           for(j = 1; j<=9; j++)
             {
               if(age[j] < age[smallest]) {
                   smallest = j;
               }
             }
           for (i = 0; i<=9; i++)
           {
            System.out.println(age[i]);
           }
        }
    }
  }
6
  • This looks like you are trying to do a BubbleSort. Is this the exact code? (I notice for instance that temp and line are never used). Commented Jan 28, 2014 at 18:22
  • Can you narrow down your question to something more specific, such as I don't understand why this code is doing x or perhaps I don't understand what this particular loop is doing? Commented Jan 28, 2014 at 18:24
  • It would be selection sort if you would swap smallest and i after the for(j loop (and fixing that one to start at i instead of 1) It's nonsense this way. Commented Jan 28, 2014 at 18:25
  • Code was edited, left a block out. @BlackVegetable sure. Specifically: I understand that the first for loop is assigning an age to the different indexes, but I dont understand what the blocks below it are doing. Commented Jan 28, 2014 at 18:26
  • 1
    The "smallest" variable holds the index of the smallest value in the array (lowest value, not index). The code then proceeds to move the smallest unmoved item to the beginning of the array, one step at a time. Please view the Bubble Sort link provided to learn more, and consider it as a valid answer to your question. Commented Jan 28, 2014 at 18:40

2 Answers 2

2

This looks like an implementation of the Bubble Sort. There is a wide wealth of information on the topic of this classic (and inefficient!) algorithm both on The Internet and in books on the subject of fundamental algorithms.

Sign up to request clarification or add additional context in comments.

Comments

0

import java.io*;

public class Example {

public static void main(String[] args) throws IOException {

  int age[] = new int[10];
  int i, j;
  int smallest;
  int temp;
  String line; 
  BufferedReader in;
  in = new BUfferedReader(new InputStreamReader(System.in));
  for(i = 0; i<= 9; i++)
  {
   System.out.println("Enter an age: ");
   line = in.readline();
   age[i] = Integer.valueOf(line).intValue();
  }
  for(i = 0; i<= 9, i++) {
     smallest = i;
       for(j = 1; j<=9; j++)// MISTAKE IS IN THIS LINE; YOU SHOULD START THE VALUE OF J FROM I+1(for(j = i+1; j<=9; j++)))
         {
           if(age[j] < age[smallest]) {
               smallest = j;
           }

         }
             //ALSO AFTER FINDING THE SMALLEST ELEMENT YOU HAVE TO SWAP THE SMALLEST ELEMENT WITH I ELEMENT
             /*int temp=age[i];
             age[i]=age[smallest];
             age[smallest]=temp*/
       for (i = 0; i<=9; i++)
       {
        System.out.println(age[i]);
       }
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.