Skip to content

Instantly share code, notes, and snippets.

@jayeshsolanki93
Created April 10, 2014 17:22
Show Gist options
  • Save jayeshsolanki93/10404017 to your computer and use it in GitHub Desktop.
Save jayeshsolanki93/10404017 to your computer and use it in GitHub Desktop.
InsertionSort in Java
import java.util.Arrays;
import java.util.Scanner;
class InsertionSort {
private static Scanner sc;
public static void main(String args[]) {
sc = new Scanner(System.in);
System.out.println("Enter no of terms");
int n = sc.nextInt();
System.out.println("Enter the terms");
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
System.out.println("The unsorted array is:");
System.out.println(Arrays.toString(arr));
sort(arr);
System.out.println("The sorted array is:");
System.out.println(Arrays.toString(arr));
}
static void sort(int arr[]) {
for (int i = 1, j; i < arr.length; i++) {
int temp = arr[i];
for (j = i; j > 0 && temp < arr[j - 1]; j--)
arr[j] = arr[j - 1];
arr[j] = temp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment