0

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)

greybeard
  • 2,249
  • 8
  • 30
  • 66

1 Answers1

0

You are assigning key to the value of j which should be assigned the value of a[j]. Further replace the sizes with 5 or size of array along with elimination of count and temp variables. Your corrected code is here:

public class InsertionDemo {
    public static void main(String[] args) {
        int a[] = {10, 2, 3, 66, 45};
        int i, j, key;
        for (j = 1; j < a.length; j++) {
            key = a[j];
            for (i = j - 1; (i >= 0 && a[i] > key); i--) {
                a[i + 1] = a[i];
            }
            a[i + 1] = key;
        }
        for (int k = 0; k < a.length; k++) {
            System.out.println("Sorted array is " + a[k]);
        }
    }
}
Community
  • 1
  • 1
medgai
  • 82
  • 4
  • 13
  • Hey , thanks for helping . in this line `f(key – BiggestNoob Dec 10 '20 at 07:59
  • Hey @user9685761 The array elements are sorted only, in case you add more elements to array, you'll have to increment the size of array in code. So better use **a.length** to get the size. Rest of your code is fine. In case it is correct answer, please accept the answer. – medgai Dec 10 '20 at 08:11
  • Hi , I tried your code. It is still not sorting anything. It is printing as it is. I am stuck on it from past two days. Kindly help. – BiggestNoob Dec 10 '20 at 08:13
  • Dear @user9685761, in no way it is my code. It was about the mistakes you were doing. Also there is no need of count and temp variable, you're using count in j and incrementing both count and j, so j is incremented with it's own. Similar is the case with temp. So, better to do it without them. Check my edited answer, and you're doing good. Keep it up. – medgai Dec 10 '20 at 10:15
  • hey,Thankyou for the help and for your kind words. – BiggestNoob Dec 10 '20 at 12:38