According to the error status, it shows that the error is on
a[j] = term
So, if you look closely at this, you will see that while loop throws an ArrayIndexOutofBoundsException. Thus, you can write the code as follows:
public static int[] Sort(int a[]){
for(int i=1;i<a.length;i++){
int term=a[i];
int j=i-1;
while(j>=0 && term<a[j]){
a[j+1]=a[j];
j--;
}
a[j+1]=term;
}
return a;
}
source
share