I did insertion sorting methon, Eclipse gives me error message that says \"The m
ID: 3846675 • Letter: I
Question
I did insertion sorting methon, Eclipse gives me error message that says "The method insertion(int[]) in the type InsertionSorting is not applicable for the arguments (int)"
How can i fix it and why did it complaine in the first place; how to print the output ;
public class InsertionSorting {
// insertion sorting Algorithm
public static int[] insertion(int[] array){
// variable declartion
int i,j,key,temp;
for(i=1; i<array.length;i++){
key=array[i];
j=i-1;
while(j>=0 && key<array[j]){
// swap
temp=array[j];
array[j]=array[j+1];
array[1+j]=temp;
j--;
}
}
return array;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arrayOfItems={8,3,1,9,2};
//System.out.print(insertion(arrayOfItems)+" ," );
for(int i=0;i<arrayOfItems.length;i++){
System.out.print(insertion(arrayOfItems[i])+" ," );
}
}
}
Explanation / Answer
public class InsertionSorting {
// insertion sorting Algorithm
public static int[] insertion(int[] array){
// variable declartion
int i,j,key,temp;
for(i=1; i<array.length;i++){
key=array[i];
j=i-1;
while(j>=0 && key<array[j]){
// swap
temp=array[j];
array[j]=array[j+1];
array[1+j]=temp;
j--;
}
}
return array;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arrayOfItems={8,3,1,9,2};
//System.out.print(insertion(arrayOfItems)+" ," );
for(int i=0;i<arrayOfItems.length;i++){
System.out.print(insertion(arrayOfItems[i])+" ," );
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.