Unilag Csc Forum > Programming
JAVA - Program to implement Binary Search
Page 1 / 1
JAVA - Program to implement Binary Search
04/05/2011 6:28 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 problem above.

The java codes


public class InsertionSort {

    /** It sorts in non-decreasing order the first N positions of A. It uses
        the insertion sort method.
    */
    public static void insertionSort(int a[]) {
        for (int limit = 1; limit < a.length; ++limit) {
        // In each iteration, the elements a[0].. a[limit-1] are in sorted order
           int who = a[limit];
         int k = limit-1;
         while (k>= 0 && a[k] > who) {
         // who is equal to a[limit] and a[k+1]..a[limit-1] are all
         // greater than who and have been shifted one place to the right
          a[k+1] = a[k];
          k--;
         }
         a[k+1] = who;
        }
    }

    /** Print out the array 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[] table = {7,3,5,8,2,1};
     printArray(table);
        insertionSort(table);
     printArray(table);
     System.exit(0);
    }
}


It would be best you understand what each line of code does by referring to the textbook.
Quote   
04/05/2011 6:30 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