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

Insertion Sort implementation in javaJava

I have implemented Insertion sort in java Java.Please 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]);
        }
    }
}

}

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]);
    }
}

}

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]);
        }
    }
}
Source Link
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]);
    }
}

}