Created
April 10, 2014 17:22
-
-
Save jayeshsolanki93/10404017 to your computer and use it in GitHub Desktop.
InsertionSort in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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