0
This is a program for bubble sorting in java ,Why output is showing as compilation error?
Suggest baby correction in program public class Myprogram{ static void bubbleSorting(int arr){ for(i=0;i<arr.length-1;i++){ for(j=0;j<arr.length-1-i;j++){ int temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } void printArrays(int arr){ for(i=0;i<arraylength;++i){ System.out.println(arr[j]+ " "); } } public static void main(String []args){ Myprogram obj=new Myprogram(); obj.bubbleSorting(arr); int arr[]={54,65,5,78,65}; System.out.println("Sorted array list:"); obj.printArrays(arr); } }
1 Odpowiedź
+ 1
yes, add int to loops (3x):
for(int i=0; ..
for(int j=0; ..
parameters to methods are type int[] arr :
void bubbleSorting(int[] arr){ //int arr
void printArrays(int[] arr){ //int arr
in main():
change order of lines,
you must declare arr before you use it
int arr[]={54,65,5,78,65}; //1 ok
obj.bubbleSorting(arr); //2 ok
in printArrays():
add dot to arr.length
and better is print instead println here
arr[j] change to arr[i]
for(i=0;i<arr.length;++i){ //arraylength
System.out.print(arr[i]+ " "); //println(arr[j]+ " ")
in bubbleSorting():
add if for compare
for(i=0 ..
for(j=0 ..
if (arr[j]>arr[j+1]) {
int temp ..
}
and delete static before method name (because you call it non-static way)
void bubbleSorting( ...