Skip to main content
2 of 2
added 334 characters in body; edited tags; edited title
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Insertion Sort implementation in Java

I have implemented Insertion sort in Java. Please review it.

public class InsertionSort {

public static void main(String[] args) {
        int[] values = new int[args.length];
        for (int i = 0; i < args.length; i++) {
            values[i] = Integer.parseInt(args[i]);
        }

        for (int i = 0; i < values.length; i++) {
            if (i!=0 && values[i] < values[i - 1]) {
                for (int j = i; j >= 1; j--) {
                    System.out.println(" j"+j);
                    if (values[j] < values[j-1]) {
                        int temp = values[j - 1];
                        values[j - 1] = values[j];
                        values[j] = temp;
                    } else {
                        break;
                    }
                }
            }
        }
        System.out.println(" sorted numbers");
        for (int i = 0; i < values.length; i++) {
            System.out.println(" "+values[i]);
        }
    }
}
VIckyb
  • 665
  • 1
  • 10
  • 18