Created
February 18, 2013 06:16
-
-
Save apremalal/4975407 to your computer and use it in GitHub Desktop.
Insertion sort implementation in java.
This file contains hidden or 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
| public class InsertionSort { | |
| public static void main(String a[]) { | |
| int data[] = { 9, 2, 6, 1, 8, 10, 1, 0 }; | |
| int sortedarr[] = insertionSort(data); | |
| for (int x : sortedarr) { | |
| System.out.print(x + " "); | |
| } | |
| } | |
| public static int[] insertionSort(int array[]) { | |
| int key, j; | |
| for (int i = 1; i < array.length; i++) { | |
| key = array[i]; | |
| j = i - 1; | |
| while ((j >= 0) && (array[j] > key)) { | |
| array[j + 1] = array[j]; | |
| j--; | |
| } | |
| array[j + 1] = key; | |
| } | |
| return array; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment