Friday, 28 February 2014

JAVA Code to sort an array

import java.util.Arrays;

public class Main {
   public static void main(String args[]) throws Exception {
      int array[] = { 1, 4, -3, 6, -2, 8, 0, -7, -9, 2 };
      Arrays.sort(array);
      myPrint("Sorted array", array);
      int index = Arrays.binarySearch(array, 2);
      System.out.println("Found 2 @ " + index);
   }
   private static void myPrint(String message, int array[]) {
      System.out.println(message
      + ": [length: " + array.length + "]");
      for (int i = 0; i < array.length; i++) {
         if(i != 0){
            System.out.print(", ");
         }
         System.out.print(array[i]);                     
      }
      System.out.println();
   }
}

No comments:

Post a Comment