Sun Dec 16, 2012 11:05 pm
public class InsertionSort {
public static void main(String a[]) {
int array[] = {13, 5, -42, 61, 121, 51, 5, 6};
// Printing the array before sort
printArray(array);
for (int i = 1; i < array.length; i++) {
int j = i;
int currentElement = array[i];
while ((j > 0) && (array[j - 1] > currentElement)) {
array[j] = array[j - 1];
j--;
}
array[j] = currentElement;
}
// Printing the array after sort
System.out.println();
printArray(array);
}
public static void printArray(int array[]) {
for (int z = 0; z < array.length; z++) {
System.out.print(array[z] + " ");
}
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.