Thu Sep 30 2021

Insertion Sort

File Name: insertion-sort.java

import java.io.*;

class sort {

	/* Constructor of the class */
	sort(int ... array) {
		for(int i = 1; i < array.length; i++) {
			for(int j = i; j >= 1; j--) {
				if(array[j] < array[j-1]) {
					int temp = array[j-1];
					array[j-1] = array[j];
					array[j] = temp;
				}
			}
		}
		System.out.println("Sorted array:");
		for(int k:array)
			System.out.println(k);
	}
}
										
class insertion {
	public static void main(String args[ ]) {
		new sort(6,2,4,1,3,0,5,8,7,9);
	}
}



/* Output */
Sorted array:
0
1
2
3
4
5
6
7
8
9
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.