Unilag Csc Forum > Programming
JAVA - Program to implement Selection Sort
Page 1 / 1
JAVA - Program to implement Selection Sort
04/05/2011 6:10 pm

Administrator
Cool Senior Member


Regist.: 04/04/2011
Topics: 14
Posts: 15
OFFLINE
Hello there, you've finally made it to the section you've been waiting for.Note that the codes below are the solution to the problem above.

The java codes

public class Selection
{
    /** Sort the array a using selection sort
     Precondition: a is not null
     Postcondition: a is sorted in increasing order
    */
    public static void selectionSort(int[] a) {
     for (int current = 0; current < a.length; current++) {
         // content of a for positions to the left of
         // current are in their final sorted position
         int where = current; // it will contain position of smallest value
               // in a[current], .., a[a.length-1]
         for (int k = current+1; k < a.length; k++) {
          // a[where] is less or equal to a[current] .. a[k-1]
          if (a[k] < a[where])
              where = k;
         }
         // swap a[where] with a[current]
         int temp = a[current];
         a[current] = a[where];
         a[where] = temp;
     }
    }

    /** Print out on a line the content of a */
    public static void printArray(int[] a) {
     for (int x: a)
         System.out.print(x + "  ");
     System.out.println();
    }

    public static void main(String[] args) {
     int[] joe = { 7,1,8,3,2,5,4};
     selectionSort(joe);
     printArray(joe);     
    }
}


It would be best you understand what each line of code does by referring to the textbook.
Quote   
04/05/2011 6:16 pm

Administrator
Cool Senior Member


Regist.: 04/04/2011
Topics: 14
Posts: 15
OFFLINE

Forum's a very good platform for helping ourselves. Please post your topics and comments

Endevour to check out our page Unilag Csc Page
http://www.facebook.com/pages/Unilag-Csc-Page/199688396730767 and like it.

Also join our new group Unilag Csc Group http://www.facebook.com/home.php?sk=group_173783495983164&ap=1 strictly computer scientists and aspirants too.
Quote   
Page 1 / 1
Login with Facebook to post
Preview