Insertion Code Recursive:
insertion Sort(datatype A[], int n)
{ // Input array is A[0:n-1]
if (n <= 1)
return;
insertion Sort( A, n-1 );
last = A[n-1];
j = n-2;
while (j >= 0 && arr[j] > last)
{
A[j+1] = A[j];
j--;
}
A[j+1] = last;
}
Program
- public class InsertionSortExample {Â Â
- Â Â Â Â public static void insertionSort(int
array[]) {Â Â
- Â Â Â Â Â Â Â Â int n =
array.length;Â Â
- Â Â Â Â Â Â Â Â for (int j = 1;
j < n; j++) {Â Â
-
            int
key = array[j];Â Â
-
            int
i = j-1;Â Â
-
            while
( (i > -1) && ( array [i] > key ) )
{Â Â
-
                array
[i+1] = array [i];Â Â
-
                i--; Â
-
            } Â
-
            array[i+1]
= key;Â Â
-
        } Â
- Â Â Â Â }Â Â
- Â Â Â Â Â Â Â
- Â Â Â Â public static void main(String
a[]){Â Â Â Â
- Â Â Â Â Â Â Â Â int[] arr1 =
{9,14,3,2,43,11,58,22};Â Â Â Â
-
        System.out.println(\"Before
Insertion Sort\");Â Â Â Â
- Â Â Â Â Â Â Â Â for(int
i:arr1){Â Â Â Â
-
            System.out.print(i+\"
\");Â Â Â Â
-
        }   Â
-
        System.out.println();   Â
-
           Â
-
        insertionSort(arr1);//sorting
array using insertion sort   Â
-
          Â
-
        System.out.println(\"After
Insertion Sort\");Â Â Â Â
- Â Â Â Â Â Â Â Â for(int
i:arr1){Â Â Â Â
-
            System.out.print(i+\"
\");Â Â Â Â
-
        }   Â
- Â Â Â Â }Â Â Â Â
- }Â Â Â Â