Created
November 9, 2011 05:12
-
-
Save ellbur/1350468 to your computer and use it in GitHub Desktop.
Insertion Sorting
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 Sorting { | |
| public static void main(String[] args) { | |
| int[] array = { 8, 7, 6, 5, 4, 3, 2, 1 }; | |
| for (int i=1; i<array.length; i++) { | |
| int j = i; | |
| int a = array[i]; | |
| /**/ // Printing out the current state of things | |
| /**/ for (int x=0; x<array.length; x++) { | |
| /**/ if (x == i) { | |
| /**/ System.out.print("[" + array[x] + "]" + " "); | |
| /**/ } | |
| /**/ else { | |
| /**/ System.out.print(" " + array[x] + " " + " "); | |
| /**/ } | |
| /**/ } | |
| /**/ System.out.println(); | |
| while (j > 0 && array[j-1] > a) { | |
| /**/ // Printing out the current state of things | |
| /**/ for (int x=0; x<array.length; x++) { | |
| /**/ if (x == i) { | |
| /**/ System.out.print("[" + array[x] + "]" + " "); | |
| /**/ } | |
| /**/ else if (x == j) { | |
| /**/ System.out.print("(" + array[x] + ")" + " "); | |
| /**/ } | |
| /**/ else { | |
| /**/ System.out.print(" " + array[x] + " " + " "); | |
| /**/ } | |
| /**/ } | |
| /**/ System.out.println(); | |
| array[j] = array[j-1]; | |
| j--; | |
| } | |
| array[j] = a; | |
| } | |
| System.out.println(java.util.Arrays.toString(array)); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment