Sunday, September 4, 2016

Java Program For Insertion Sort

 
public class MyInsertionSort {
 
    public static void main(String[] args) {
         
        int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
        insertionSort(input);
    }
     
    private static void printNumbers(int[] input) {
         
        for (int i = 0; i < input.length; i++) {
            System.out.print(input[i] + ", ");
        }
        System.out.println("\n");
    }
 
    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;
            printNumbers(array);
        }
    }
}

Output:
2, 4, 9, 6, 23, 12, 34, 0, 1, 

2, 4, 9, 6, 23, 12, 34, 0, 1, 

2, 4, 6, 9, 23, 12, 34, 0, 1, 

2, 4, 6, 9, 23, 12, 34, 0, 1, 

2, 4, 6, 9, 12, 23, 34, 0, 1, 

2, 4, 6, 9, 12, 23, 34, 0, 1, 

0, 2, 4, 6, 9, 12, 23, 34, 1, 

0, 1, 2, 4, 6, 9, 12, 23, 34, 

Bhanu Namikaze

Author & Editor

Bhanu Namikaze is an Ethical Hacker, Passionate Blogger, Web Developer, Student and Mechanical Engineer. He Enjoys writing articles, Blogging, Solving Errors, Surfing and Social Networking. Feel Free To Contact Me @ Bhanu.

0 comments:

Post a Comment

Popular Posts

Copyrights @ Interview Questions.Me - Blogger Templates Designed by Templateism - Distributed By Protemplateslab

  • (91) 5544 654942
  • Bhanu@HackingDream.net
  • Interview Questions

Copyrights @ Interview Questions.Me -A Site By Bhanu In Association With Hacking Dream