// Insertion Sort
class InsertionSort {
/*Function to sort array using insertion sort*/
void insertionsort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int r = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (r >= 0 && arr[r] > key) {
arr[r + 1] = arr[r];
r = r - 1;
}
arr[r + 1] = key;
}
}
/* A utility function to print array of size n*/
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
public static void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6 , 7, 90, 60, 50, 30, 54, 2, 1, 6 };
//creates new instance of insertin sort and runs it with a given arry
InsertionSort ob = new InsertionSort();
ob.insertionsort(arr);
//prints the sorted array
printArray(arr);
}
}
// Java program for implementation of Insertion Sort
class InsertionSort {
/*Function to sort array using insertion sort*/
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
/* A utility function to print array of size n*/
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
public static void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6 };
InsertionSort ob = new InsertionSort();
ob.sort(arr);
printArray(arr);
}
} /* This code is contributed by Rajat Mishra. */
// Insertion sort in Java
import java.util.Arrays;
class InsertionSort {
void insertionSort(int array[]) {
int size = array.length;
for (int step = 1; step < size; step++) {
int key = array[step];
int j = step - 1;
// Compare key with each element on the left of it until an element smaller than
// it is found.
// For descending order, change key<array[j] to key>array[j].
while (j >= 0 && key < array[j]) {
array[j + 1] = array[j];
--j;
}
// Place key at after the element just smaller than it.
array[j + 1] = key;
}
}
// Driver code
public static void main(String args[]) {
int[] data = { 9, 5, 1, 4, 3 };
InsertionSort is = new InsertionSort();
is.insertionSort(data);
System.out.println("Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(data));
}
}
// Java Insertion Sort
// -------------------
/*
Time Complexity
Best Time Complexity:O(n)
Average Time Complexity:O(n^2)
Worst Time Complexity:O(n^2)
Space Complexity
No auxiliary space is required in Linear Search implementation.
Hence space complexity is:O(1)
*/
import java.util.Arrays;
import java.util.Scanner;
public class InsertionSort {
public static int[] sort(int arr[]) {
for (int i = 0; i < arr.length; i++) {
int j = i - 1; // j will point to previous element
int k = arr[i]; // k holds the value being sorted
while (j >= 0 && k <= arr[j]) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = k;
}
return arr; //returns the sorted array
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = new int[10];
System.out.println("Enter Array Values: ");
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
int a[] = new int[arr.length];
a = Arrays.copyOf(sort(arr), arr.length); //copies the sorted array to another array
//sorted in ascending order
System.out.println("Sorted Array:");
for (int i = 0; i < a.length; i++) {
System.out.print(+a[i] + " ");
}
}
}