0

I need to change this from a bubble sort to a selection sort but i don't know where to start.
Also I don't understand the difference between the two sorting methods, and which one would be more efficient.


Thanks

import java.io.*;
import java.util.Scanner;

public class phase1 {

    public static void main(String[] args) throws Exception {
       File fileobject = new File("indata.txt.rtf");
       Scanner input = new Scanner(fileobject);
       int size = input.nextInt();
       int [] numberarray = new int[size];


       int index = 0;
       do {
           numberarray[index] = input.nextInt();
                    if (index <= (numberarray.length - 1)){
                        index = index + 1;
                                           }
          } while (index <= (numberarray.length - 1));

      numberarray = phase1.sort(numberarray);

      File fileobject2 = new File("output.txt.");
      PrintWriter output = new PrintWriter(fileobject2);
      output.println(size);

      index = 0;
      do{
          int fprt = numberarray[index];
          output.println(fprt);
          index = index + 1;
      }while (index <= (numberarray.length - 1));

      index = 0;
      double avg = 0;
      do{
           avg = avg + numberarray[index];
           index = index +1;

      }while (index < (numberarray.length));

      avg = (avg / size);
      output.println(avg);
      output.close();

      /**/

       index = 0;
        do{
                 System.out.println(numberarray[index]);
                    if (index <= (numberarray.length - 1)){
                        index = index + 1;}
        } while(index <= (numberarray.length - 1));
     /**/
    }

public static int [] sort(int [] da)throws Exception{

        for (int ab = 0; ab < (da.length - 1); ab = ab + 1) {
        for (int ab2 = ab + 1; ab2 < da.length; ab2 = ab2 + 1) {
            if (da[ab] > da[ab2]) {
                int temp = da[ab];
                da[ab] = da[ab2];
                da[ab2] = temp;
                                    }
                                    }
                                    }
            return da;
    }
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by group256
0

Please use google, there are very good explanation and examples online. I also have noticed there are very good videos available on Youtube regarding sorting.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.