Monday, 12 September 2011

Array Tutorial in Java

In this section you will be introduced to the concept of Arrays in Java Programming language. You will learn how the Array class in java  helps the programmer to organize the same type of data into easily manageable format.
Program data is stored in the variables and takes the  memory spaces, randomly. However, when we need the data of the same type  to store in the contiguous memory allocations we use the data structures like arrays. To meet this feature java has provided an Array class which abstracts the array data-structure.
The java array enables the user to store the values of the same type in contiguous memory allocations. Arrays are always a fixed length abstracted data structure  which can not be altered when required.
The Array class implicitly extends java.lang.Object so an array is an instance of Object.
Advantages of Java Array:
  1. An array can hold primitive types data.
  2. An array has its size that is known as array length.
  3. An array knows only its type that it contains. Array type is checked at the compile-time.
Disadvantages of Java Array:
  1. An array has fixed size.
  2. An array holds only one type of data (including primitive types). 
  3. Now lets study the structure of Arrays in java. Array is the most widely used data structure in java. It can contain multiple values of the same type. Moreover, arrays are always of fixed length i.e. the length of an array cannot be increased or decreased.
    Lets have a close look over the structure of Array. Array contains the values which are implicitly  referenced through the index values. So to access the stored values in an array we use indexes. Suppose an array contains "n"  integers. The first element of this array will  be indexed with the "0" value and the last integer will be referenced by "n-1" indexed value.
    Presume an array  that contains 12 elements as shown  in the figure. Each element is holding a distinct value. Here the first element is refrenced by a[0] i.e. the first  index value. We have filled the 12 distinct values in the array each referenced as:
    a[0]=1
    a[1]=2
    ...
    a[n-1]=n
    ...
    a[11]=12
    The figure below shows the structure of an Array more precisely.


    As we declare a variable in Java, An Array variable is declared the same way. Array variable has a type and a valid Java identifier i.e. the array's type and the array's name. By type we mean the type of elements contained in an  array. To represent the variable as an Array, we use [] notation. These two brackets are used to hold the array of a variable.
    By array's name, we mean that we can give any name to the array, however it should follow the predefined conventions. Below are the examples which show how to declare an array :-
    int[] array_name;   //declares an array of integers
    String[] names;
    int[][] matrix;  //this is an array of arrays
    It is essential to assign memory to an array when we declare it. Memory is assigned to set the size of the declared array. for example:
    int[] array_name = new int[5];
    Here is an example that creates an array that has 5 elements.
    public class Array
    {
     public static void main(String[] args)
     {
      int[] a = new int[5];
     }
    }
  4. After declaring an array variable, memory is allocated to it. The "new" operator is used for the allocation of  memory to the array object. The correct way to use the "new" operator is
     String names[];
     names = new String[10];
    Here, the new operator is followed by the type of variable and the number of elements to be allocated. In this example [] operator has been used to place the number of elements to be allocated.
    Lets see a simple example of an array,
    public class Sum 
    {
      public static void main(String[] args) 
      {
      int[] x = new int [101];
      for (int i = 0; i<x.length; i++ )
      x[i] = i;
      int sum = 0;
      for(int i = 0; i<x.length; i++)
      sum += x[i];
      System.out.println(sum);
      }
    }
    In this example, a variable 'x' is declared which  has a type array of int, that is, int[]. The variable x is initialized to reference a newly created array object. The expression 'int[] = new int[50]' specifies that the array should have 50 components. To know the length of the Array, we use field length, as shown.
    Output for the given program:
    C:\tamana>javac Sum.java

    C:\tamana>java Sum
    5050

    C:\tamana>
    Download this example.
    We have already discussed that to refer an element within an array, we use the [] operator. The [] operator takes an "int" operand and returns the element at that index. We also know that the array indices start with zero, so the first element will be held by the 0 index. For Example :-
    int month = months[4];  //get the 5th month (May)
    Most of the times it is not known in the program that which elements are of interest in an array. To find the elements of interest in the program, it is required that the program must run a loop through the array. For this purpose "for" loop is used to examine each element in an array. For example :-
    String months[] = 
             {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 
              "July", "Aug", "Sep", "Oct", "Nov", "Dec"};
               //use the length attribute to get the number
              //of elements in an array
              for (int i = 0; i < months.length; i++ ) {
              System.out.println("month: " + month[i]);
    Here, we have taken an array of months which is,
       String months[] =
     {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
      "July", "Aug", "Sep", "Oct", "Nov", "Dec"};

    Now, we run a for loop to print each element individually starting from the month "Jan".
      for (int i = 0; i < months.length; i++ )
    In this loop int i = 0; indicates that the loop starts from the 0th position of an array and goes upto the last position which is length-1, i < months.length; indicates the length of the array and i++ is used for the increment in the value of i which is i = i+1.
    After learning all about arrays, there is still one interesting thing left to learn i.e. copying arrays. It means to copy data from one array to another. The precise way to copy data from one array to another is
    public static void arraycopy(Object source,
                       int srcIndex,
                       Object dest,
                       int destIndex,
                       int length)
    Thus apply system's arraycopy method for copying arrays.The parameters being used are :-
    src  the source array
    srcIndex start position (first cell to copy) in the source array
    dest  the destination array
    destIndex start position in the destination array
    length the number of array elements to be copied
    The following program, ArrayCopyDemo(in a .java source file), uses arraycopy to copy some elements from the copyFrom array to the copyTo array.
    public class ArrayCopyDemo{
      public static void main(String[] args){
      char[] copyFrom = {'a','b','c','d','e','f','g','h','i','j'};
      char[] copyTo = new char[5];
      System.arraycopy(copyFrom, 2, copyTo, 05);
      System.out.println(new String (copyTo));
      }
    }
    Output of the program:
    C:\tamana>javac ArrayCopyDemo.java
    C:\tamana>java ArrayCopyDemo
    cdefg
    C:\tamana>
    In this example the array method call begins the copy of elements from element number 2. Thus the copy begins at the array element 'c'. Now, the arraycopy method takes the copied element and puts it into the destination array. The destination array begins at the first element (element 0) which is the destination array copyTo. The copyTo copies 5 elements : 'c', 'd', 'e', 'f', 'g'. This method will take "cdefg" out of "abcdefghij", like this :
    Following image illustrates the procedure of copying array from one to another.

    Donwload this example.
    Two-dimensional arrays are defined as "an array of arrays". Since an array type is a first-class Java type, we can have an array of ints, an array of Strings, or an array of Objects. For example, an array of ints will have the type int[]. Similarly we can have int[][], which represents an "array of arrays of  ints". Such an array is said to be a two-dimensional array.
    The command
      int[][] A = new int[3][4];
    declares a variable, A, of type int[][], and it initializes that variable to refer to a newly created object. That object is an array of arrays of ints. Here, the notation int[3][4] indicates that there are 3 arrays of ints in the array A, and that there are 4 ints in each of those arrays.
    To process a two-dimensional array, we use nested for loops. We already know about for loop. A loop in a loop is called a Nested loop. That means we can run another loop in a loop.
    Notice in the following example how the rows are handled as separate objects.
    Code: Java
    int[][] a2 = new int[10][5];
     // print array in rectangular form
     for (int i=0; i<a2.length; i++) {
         for (int j=0; j<a2[i].length; j++) {
             System.out.print(" " + a2[i][j]);
         }
         System.out.println("");
     }
    In this example, "int[][] a2 = new int[10][5];" notation shows a two-dimensional array. It declares a variable a2 of type int[][],and it initializes that variable to refer to a newly created object. The notation int[10][5] indicates that there are 10 arrays of ints in the array a2, and that there are 5 ints in each of those arrays.
    Here is the complete code of the example:
    public class twoDimension{
      public static void main(String[] args) {
      int[][] a2 = new int[10][5];
      for (int i=0; i<a2.length; i++) {
      for (int j=0; j<a2[i].length; j++) {
      a2[i][j= i;
      System.out.print(" " + a2[i][j]);
      }
      System.out.println("");
      }
      }
    }
    Here is the output for the program:
    C:\tamana>javac twoDimension.java
    C:\tamana>java twoDimension
     0 0 0 0 0
     1 1 1 1 1
     2 2 2 2 2
     3 3 3 3 3
     4 4 4 4 4
     5 5 5 5 5
     6 6 6 6 6
     7 7 7 7 7
     8 8 8 8 8
     9 9 9 9 9
    C:\tamana>_
    Download this program
    So far we have studied about the one-dimensional and two-dimensional arrays. To store data in more dimensions a multi-dimensional array is used. A multi-dimensional array of dimension n is a collection of items. These items are accessed via n subscript expressions. For example, in a language that supports it, the element of the two-dimensional array x is denoted by x[i,j].
    The Java programming language does not really support multi-dimensional arrays. It does, however, supports an array of arrays. In Java, a two-dimensional array 'x' is an array of one-dimensional array. For instance :-
      int[][] x = new int[3][5];
    The expression x[i] is used to select the one-dimensional array; the expression x[i][j] is ued to select the element from that array. The first element of this array will  be indexed with the "0" value and the last integer will be referenced by "length-1" indexed value. There is no array assignment operator.





     

Selection Sort In Java

Introduction
In this example we are going to sort the values of an array  using selection sort.
In selection sorting algorithm, find the minimum value in the array then swap it first position. In next step leave the first value and find the minimum value within remaining values. Then swap it with the value of minimum index position. Sort the remaining  values by using same steps. Selection sort  is probably the most intuitive sorting algorithm to invent.

The complexity of selection sort algorithm is in worst-case, average-case, and best-case run-time of Θ(n2), assuming that comparisons can be done in constant time. 

Code description:
In selection sort algorithm to find the minimum value in the array. First assign minimum index in key (index_of_min=x). Then find the minimum value and assign the index of minimum value in key (index_of_min=y). Then swap the minimum value with the value of minimum index.
At next iteration leave the value of minimum index position and sort the remaining values by following same steps.

Working of the selection sort :
Say we have an array unsorted A[0],A[1],A[2]................ A[n-1] and A[n] as input. Then the following steps are followed by selection sort algorithm to sort the values of an array . (Say we have a key index_of_min that indicate the position of minimum value)
1.Initaily varaible  index_of_min=0;
2.Find the minimum value in the unsorted array.
3.Assign the index of the minimum value into index_of_min variable.
4.Swap minimum value to first position.
5.Sort the remaining values of array (excluding the first value).
The code of the program :
public class selectionSort{
  public static void main(String a[]){
  int i;
  int array[] {12,9,4,99,120,1,3,10};
  System.out.println("\n\n RoseIndia\n\n");
  System.out.println(" Selection Sort\n\n")
  System.out.println("Values Before the sort:\n");  
  for(i = 0; i < array.length; i++)
  System.out.printarray[i]+"  ");
  System.out.println();
  selection_srt(array, array.length);  
  System.out.print("Values after the sort:\n");  
  for(i = 0; i <array.length; i++)
  System.out.print(array[i]+"  ");
  System.out.println();
  System.out.println("PAUSE");
  }

  public static void selection_srt(int array[]int n){
  for(int x=0; x<n; x++){
  int index_of_min = x;
  for(int y=x; y<n; y++){
  if(array[index_of_min]<array[y]){
  index_of_min = y;
  }
  }
  int temp = array[x];
  array[x= array[index_of_min];
  array[index_of_min= temp;
  }
  }
}
Output of the example:
C:\array\sorting>javac selectionSort.java
C:\array\sorting>java selectionSort
       RoseIndia
       Selection Sort
Values Before the sort:
12  9  4  99  120  1  3  10
Values after the sort:
120  99  12  10  9  4  3  1
PAUSE
C:\array\sorting>_

Quick Sort In Java

Introduction
In this example we are going to sort integer values of an array using quick sort.

Quick sort algorithm is developed by C. A. R. Hoare. Quick sort is a comparison sort. The working of  quick sort algorithm is depending on a divide-and-conquer strategy. A divide and conquer strategy is  dividing  an array  into two sub-arrays. Quick sort is one of the fastest and simplest sorting algorithm. The complexity of quick sort in the average case is  Θ(n log(n)) and in the  worst case is Θ(n2).
Code description:
In quick sort algorithm pick an element from array of elements. This element is called the pivot. Then compare the the values from left to right until a greater element is find then swap the values. Again start comparison from right with pivot. When lesser element is find then swap the values. Follow the same steps until  all elements which are less than the pivot come before the pivot and all elements greater than the pivot come after it. After this partitioning, the pivot is in its last position. This is called the partition operation. Recursively sort the sub-array of lesser elements and the sub-array of greater elements.

Working of quick sort algorithm:
Input:
12 9 4 99 120 1 3 10 13




Output:
1 3 4 10 12 13 99 120
The code of the program :
public class QuickSort{
  public static void main(String a[]){
  int i;
  int array[] {12,9,4,99,120,1,3,10,13};

  System.out.println("\n\n RoseIndia\n\n");
  System.out.println(" Quick Sort\n\n");
  System.out.println("Values Before the sort:\n");
  for(i = 0; i < array.length; i++)
  System.out.printarray[i]+"  ");
  System.out.println();
  quick_srt(array,0,array.length-1);
  System.out.print("Values after the sort:\n");
  for(i = 0; i <array.length; i++)
  System.out.print(array[i]+"  ");
  System.out.println();
  System.out.println("PAUSE");
  }

  public static void quick_srt(int array[],int low, int n){
  int lo = low;
  int hi = n;
  if (lo >= n) {
  return;
  }
  int mid = array[(lo + hi2];
  while (lo < hi) {
  while (lo<hi && array[lo< mid) {
  lo++;
  }
  while (lo<hi && array[hi> mid) {
  hi--;
  }
  if (lo < hi) {
  int T = array[lo];
  array[lo= array[hi];
  array[hi= T;
  }
  }
  if (hi < lo) {
  int T = hi;
  hi = lo;
  lo = T;
  }
  quick_srt(array, low, lo);
  quick_srt(array, lo == low ? lo+: lo, n);
  }
}
Output of the example:
C:\array\sorting>javac QuickSort.java
C:\array\sorting>java QuickSort
       RoseIndia
       Quick Sort
Values Before the sort:
12  9  4  99  120  1  3  10  13
Values after the sort:
1  3  4  9  10  12  13  99  120
PAUSE
C:\array\sorting>_

Odd Even Transposition Sort In Java

Introduction
In this example we are going to sort integer values of an array using odd even transposition sort.

Odd even transposition sort is a parallel sorting algorithm. Odd Even is based on the Bubble Sort technique of comparing two numbers and swapping them and put higher value at larger index .In each parallel computational steps can pair off either the odd or even neighboring pairs. Each number (In Processing Element-PE) would look to it's right neighbor and if it were greater, it would swap them.

Code description:
The odd even transposition sort is a parallel sorting algorithm. That mean more than one compression can made at one iteration. The comparison is same as bubble sort.
Working of odd even transposition sort:

The code of the program :

public class OddEvenTranspositionSort{
  public static void main(String a[]){
  int i;
  int array[] {12,9,4,99,120,1,3,10,13};
  
  System.out.println("\n\n RoseIndia\n\n");
  System.out.println(" Odd Even Transposition Sort\n\n");
  System.out.println("Values Before the sort:\n");
  for(i = 0; i < array.length; i++)
  System.out.printarray[i]+"  ");
  System.out.println();
  odd_even_srt(array,array.length);
  System.out.print("Values after the sort:\n");
  for(i = 0; i <array.length; i++)
  System.out.print(array[i]+"  ");
  System.out.println();
  System.out.println("PAUSE");
  }

  public static void odd_even_srt(int array[],int n){
  for (int i = 0; i < n/2; i++ ) {
  for (int j = 0; j+< n; j += 2)
  if (array[j> array[j+1]) {
  int T = array[j];
  array[j= array[j+1];
  array[j+1= T;
  }
  for (int j = 1; j+< array.length; j += 2)
  if (array[j> array[j+1]) {
  int T = array[j];
  array[j= array[j+1];
  array[j+1= T;
  }
  }
  }
}
Output of the example:
C:\array\sorting>javac OddEvenTranspositionSort.java
C:\array\sorting>java OddEvenTranspositionSort
       RoseIndia
       Odd Even Transposition Sort
Values Before the sort:
12  9  4  99  120  1  3  10  13
Values after the sort:
1  3  4  9  10  12  13  99  120
PAUSE
C:\array\sorting>_

Extra Storage Merge Sort in Java

Introduction
In this example we are going to sort integer values of an array using extra storage merge sort.

In extra storage merge sorting algorithm  the unsorted values divide into two equal parts iteratively and create an array for store data value in extra storage. Then merge the two parts , sort it and store into an array .Then again merge the next part , sort it and store into an array. Do it iteratively until  the values are not in sorted order. In this sorting the number of elements must be even.

Code description:
In extra storage  is similar to merge sort .But in extra storage merge sort the sorted values are stored in an other array.

Working of extra storage merge sort algorithm:
Say we have an array unsorted  A[0],A[1],A[2]................ A[n-1] and A[n] as input. Then the following steps are followed by storage merge sort algorithm to sort the values of an array.

Step1:Spliting the values of array
Divide the values into two equal 1/2
A[0],A[1],A[2].........A[n/2-1]   &  A[n/2]....... .................A[n-1], A[n]

 Again divide two equal 1/2
A[0] a[1]A[2]..............A[(n/2-1)/2-1] &  A[(n/2-1)/2]............A[n/2-1],
A[n/2].............A[(2n-1)/2-1]  & a[(2n-1)/2].............A[n-1],A[n]
..........................................................................................................................
..........................................................................................................................
........................................................................................................................
A[0] & A[1] & A[2]& A[3],..............................................................A[n-1]& A[n]

Step2:Mergesets of  two values, sort the values and store into a different array

A[0],A[1] & A[2],A[3]&..................................................................&A[n-1],A[n]
If A[1]<A[0],A[]<A[3]........................................................................A[n-1]>A[n]
then
A[1]A[0],A[2]A[3],...............................................................................A[n]A[n-1]
Step3:Merge sets of four values, sort the values and store into a different array
A[2] A[1] A[0] A[3],...................................................................................A[n-1]
..................................................................................................................
..................................................................................................................
.................................................................................................................
Step3:Merge n values, sort the values and store into a different array
A[2]A[6]......................................................................................................A[n-5]

Where n must be even number.

Steps of Merge Sort:

Say unsorted  an array values are:
12,9,4,99,120,1,3,10



The code of the program :
public class ExtraStorageMergeSort{
  public static void main(String a[]){
  int i;
  int array[] {12,9,4,99,120,1,3,10};
  int array1[] new int[array.length];
  System.out.println("\n\n RoseIndia\n\n");
  System.out.println(" Extra Strorage Space Merge Sort\n\n");
  System.out.println("Values Before the sort:\n");
  for(i = 0; i < array.length; i++)
  System.out.printarray[i]+"  ");
  System.out.println();
  mergeSort_srt(array,0, array.length-1,array1);
  System.out.print("Values after the sort:\n");
  for(i = 0; i <array.length; i++)
  System.out.print(array1[i]+"  ");
  System.out.println();
  System.out.println("PAUSE");
  }
  
  public static void mergeSort_srt(int array[]int low, int high, int array1[]){
  if(low >= high) {
  return;
  }

  int middle = (low+high2;
  mergeSort_srt(array, low, middle, array1);
  mergeSort_srt(array, middle+1, high, array1);
  int k, t_low = low, t_high = middle+1;
  for(k = low; k <= high; k++)
  if ((t_low <= middle&& ((t_high > high|| (array[t_low<
array
[t_high]))) {
  array1[k= array[t_low++];
  }
  else {
  array1[k= array[t_high++];
  }
  for(k = low; k <= high; k++) {
  array[k= array1[k];
  }
  }
}
Output of the example:
C:\array\sorting>javac ExtraStorageMergeSort.java
C:\array\sorting>java ExtraStorageMergeSort
       RoseIndia
       Extra Strorage Space Merge Sort
Values Before the sort:
12  9  4  99  120  1  3  10
Values after the sort:
1  3  4  9  10  12  99  120
PAUSE
C:\array\sorting>_

Merge Sort in Java

Introduction
In this example we are going to sort integer values of an array using merge sort.

In merge sorting algorithm unsorted values are divided into two equal parts iteratively. Then merge both parts and sort it. Then again merge the next part and sort it. Do it iteratively until  the values are not in sorted order. In merge sorting the number of elements must be even. The merge sorting is invented by John von Neumann in 1945 .
The complexity of the merge sorting is in worst-case O(n log n) and in average case O(n log n).
  
Code description:
In merge sort split the array values in halves recursively until each half has only single  element. Merge the two 1/2 values together and sort the values. Do same steps iteratively until the values are not sorted.

Working of merge sort algorithm:
Say we have an array unsorted  A[0],A[1],A[2]................ A[n-1] and A[n] as input. Then the following steps are followed by merge sort algorithm to sort the values of an array.

Step1:Spliting the values of array
Divide the values into two equal 1/2
   A[0],A[1],A[2].........A[n/2-1]   &  A[n/2]....... .................A[n-1], A[n]

Again divide two equal 1/2
A[0] a[1]A[2]..............A[(n/2-1)/2-1] &  A[(n/2-1)/2]............A[n/2-1],
A[n/2].............A[(2n-1)/2-1]  & a[(2n-1)/2].............A[n-1],A[n]
..........................................................................................................................
..........................................................................................................................
........................................................................................................................
A[0] & A[1] & A[2]& A[3],..............................................................A[n-1]& A[n]

Step2:Merge two values  and sort the values

A[0],A[1] & A[2],A[3]&..................................................................&A[n-1],A[n]
If A[1]<A[0],A[]<A[3]........................................................................A[n-1]>A[n]
then
A[1]A[0],A[2]A[3],...............................................................................A[n]A[n-1]
Step3:Merge four values and sort the values
A[2] A[1] A[0] A[3],...................................................................................A[n-1]
..................................................................................................................
..................................................................................................................
.................................................................................................................
Step3:Merge n values and sort the values
A[2]A[6]......................................................................................................A[n-5]

Where n must be even number.

Steps of Merge Sort:

Say unsorted  an array values are:
12,9,4,99,120,1,3,10


The code of the program :
public class mergeSort{
  public static void main(String a[]){
  int i;
  int array[] {12,9,4,99,120,1,3,10};
  System.out.println("\n\n RoseIndia\n\n");
  System.out.println(" Selection Sort\n\n");
  System.out.println("Values Before the sort:\n");
  for(i = 0; i < array.length; i++)
  System.out.printarray[i]+"  ");
  System.out.println();
  mergeSort_srt(array,0, array.length-1);
  System.out.print("Values after the sort:\n");
  for(i = 0; i <array.length; i++)
  System.out.print(array[i]+"  ");
  System.out.println();
  System.out.println("PAUSE");
  }

  public static void mergeSort_srt(int array[],int lo, int n){
  int low = lo;
  int high = n;
  if (low >= high) {
  return;
  }

  int middle = (low + high2;
  mergeSort_srt(array, low, middle);
  mergeSort_srt(array, middle + 1, high);
  int end_low = middle;
  int start_high = middle + 1;
  while ((lo <= end_low&& (start_high <= high)) {
  if (array[low< array[start_high]) {
  low++;
  else {
  int Temp = array[start_high];
  for (int k = start_high- 1; k >= low; k--) {
  array[k+1= array[k];
  }
  array[low= Temp;
  low++;
  end_low++;
  start_high++;
  }
  }
  }  
}
Output of the example:
C:\array\sorting>javac mergeSort.java
C:\array\sorting>java mergeSort
       RoseIndia
       Selection Sort
Values Before the sort:
12  9  4  99  120  1  3  10
Values after the sort:
1  3  4  9  10  12  99  120
PAUSE
C:\array\sorting>_

Insertion Sort In Java

Introduction
In this example we are going to sort integer values of an array using insertion sort.

Insertion sorting algorithm is similar to bubble sort. But insertion sort is more  efficient than bubble sort because in insertion sort the elements comparisons are less as compare to bubble sort. In insertion sorting algorithm compare the value until  all the prior elements are lesser than compared value is not found. This mean that the all previous values are lesser than compared value. This algorithm is more efficient than the bubble sort .Insertion sort is a good choice for small values and for nearly-sorted values. There are more efficient algorithms such as quick sort, heap sort, or merge sort for large values .
Positive feature of insertion sorting: 
1.It is simple to implement
2.It is efficient on (quite) small data values
3.It is efficient on data sets which are already nearly sorted.

The complexity of insertion sorting is O(n) at best case of an already sorted array and  O(n2) at worst case .
  
Code description:
In insertion sorting take the element form left assign value into a variable. Then compare the  value with  previous values. Put  value so that values must be lesser than the previous values. Then assign  next  value to a variable and follow the same steps relatively until the comparison not reached to end of array.  
Working of insertion sorting:

The code of the program :
public class InsertionSort{
  public static void main(String a[]){
  int i;
  int array[] {12,9,4,99,120,1,3,10};
  System.out.println("\n\n RoseIndia\n\n");
  System.out.println(" Selection Sort\n\n")
  System.out.println("Values Before the sort:\n");  
  for(i = 0; i < array.length; i++)
  System.out.printarray[i]+"  ");
  System.out.println();
  insertion_srt(array, array.length);  
  System.out.print("Values after the sort:\n");  
  for(i = 0; i <array.length; i++)
  System.out.print(array[i]+"  ");
  System.out.println()
  System.out.println("PAUSE")
  }

  public static void insertion_srt(int array[]int n){
  for (int i = 1; i < n; i++){
  int j = i;
  int B = array[i];
  while ((j > 0&& (array[j-1> B)){
  array[j= array[j-1];
  j--;
  }
  array[j= B;
  }
  }
}
Output of the example:
C:\array\sorting>javac InsertionSort.java
C:\array\sorting>java InsertionSort
       RoseIndia
       Selection Sort
Values Before the sort:
12  9  4  99  120  1  3  10
Values after the sort:
1  3  4  9  10  12  99  120
PAUSE
C:\array\sorting>_