Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

InsertionSort Example

import java.util.Arrays;

public class InsertionSort 
{ 
	void sort(int array[]) 
	{ 
		int l = array.length; 
		for (int i = 1; i < l; ++i) 
		    { 
			int val = array[i]; 
			int j = i - 1; 

			while (j >= 0 && array[j] > val) 
			    { 
				array[j + 1] = array[j]; 
				j = j - 1; 
			} 
			array[j + 1] = val; 
		} 
	} 

	public static void main(String args[]) 
	{ 
		int[] array1 = { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; 
        int[] array2 = { 9, 3, 8, 1, 6, 5, 2 }; 

        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));

		InsertionSort ob = new InsertionSort(); 
		ob.sort(array1); 
		ob.sort(array2); 
		
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
	} 
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.