I am trying to do insertion sort but it is not working properly.
I am doing as per the requirements and it is still giving error.
Any suggestions or any advice on what I am doing wrong.
kindly help, I am new to programming.
package practicepkg;
public class InsertionDemo {
    public static void main(String[] args) {
        int a[] = {10, 2, 3, 45, 66};
        int count = 1, temp = 0;
        int i, j, key;
        for (j = count; j < 4; j++) {
            key = j;
            if (key < 4 && temp < 4) {
                for (i = temp; i >= 0; i--) {
                    if (a[key] < a[i]) {
                        int n = a[key];
                        a[key] = a[i];
                        a[i] = n;
                    }
                    key++;
                }
                temp++;
                count++;
            } else {
                break;
            }
        }
        for (int k = 0; k < 4; k++) {
            System.out.println("Sorted array is " + a[k]);
        }
    }
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at practicepkg.InsertionDemo.main(InsertionDemo.java:17)
 
     
     
    