Skip to main content
1 of 2
VIckyb
  • 665
  • 1
  • 10
  • 18

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