This program is basic insertion sort. I am getting ArrayIndexOutOfBoundsException
at line numbers 22 and 44. Help me in solving this issue.How to return sorted whole array from insertion method.
return a[] is not working..............................................
package analysis;
import java.util.*;
class Sort
{
int i,j,n,temp;
int a[] = new int[n];
int insertion(int a[],int n)
{
for (i=1;i<n;i++) {
j=i-1;
while (i>0 && a[j]>a[i]) {
temp = a[j];
a[j] = a[i];
a[i] = temp;
i--;
j--;
}
}
return a[n];
}
}
class Recurse
{
private static Scanner sc;
public static void main(String[] args)
{
System.out.println("enter array size");
int l,s;
sc = new Scanner(System.in);
s = sc.nextInt();
int b[] = new int[s];
int c[] = new int[s];
for (l=0;l<s;l++) {
b[l] = sc.nextInt();
}
Sort so = new Sort();
c[s] = so.insertion(b,s);
System.out.println("Sorted array is ");
for (l=0;l<s;l++) {
System.out.println(c[l]);
}
}
}