I want to make bubble sort program but without using any function
public static void main(String[] args) {
int [] a={98,87,42,12,42,63,56,11};
for(int i=0;i<a.length-1;i ){
for(int j=0;j<a.length-i-1;j ){
if(a[j]>a[j 1]){
int temp=a[j 1];
a[j]=a[j 1];
a[j 1]=temp;
}
}
System.out.println(a[i]);
}
}
}
anyone guide me what I'm doing wrong?
CodePudding user response:
you want to switch a[j] and a[j 1] so tempt should be a[j]
System.out.println(a[i]);
the pringing should be outside of the loops after all the switching is done
this should work
public static void main(String[] args) {
int [] a={98,87,42,12,42,63,56,11};
for(int i=0;i<a.length;i ){
for(int j=0;j<a.length-i-1;j ){
if(a[j]>a[j 1]){
int temp=a[j];
a[j]=a[j 1];
a[j 1]=temp;
}
}
}
for(int i=0;i<a.length;i ){
System.out.print(a[i] " ");
}
}