Categories
Algorithm Algorithms & Design Sort

Insertion Sort

public class InsertionSort {

	public static void main(String[] args) {
	
		int arr [] = {1,11,2,8,19,4,3,9}; 
		
		int temp;
		for(int i = 1; i < arr.length;i++){
			
			int j = i-1;
			while(j>=0 && arr[j]>arr[j+1]){
				temp = arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=temp;
				j--;
			}
			
		}
		
		printArray(arr);

	}

	public static void printArray(int arr[]){
		for(int k=0;k<arr.length;k++)
			System.out.println(arr[k]);
	}
}

Output :

Leave a comment

Design a site like this with WordPress.com
Get started