public class InsertionSort { public static void main(String a[]) { int[] input = { 1, 0, 77, 34, 97, 11, 98, 99 }; int _temp; for (int i = 1; i < input.length; i++) { for (int k = i; k > 0; k--) { if (input[k] < input[k - 1]) { _temp = input[k]; input[k] = input[k - 1]; input[k - 1] = _temp; } } } for (int i = 0; i < input.length; i++) { System.out.print(input[i]+" "); } } } Output: 0 1 11 34 77 97 98 99
Thanks for sharing this nice and useful post about Java Insertion Sort Algorithm .
ReplyDeleteThank you Anurag.
Delete