+ 2
Time limit exceeded(Bubble Sort)
the code is compiled in my smartphone, and it says time limit exceeded. is there something wrong with the code? import java.util.*; public class Main { public static void main(String[] args) { int a[] = {6,5,4,7,3}; int i,x,temp; for(i=0;i<a.length;i++){ for(x=0;x<a.length-1;x--){ if(a[i]>a[i+1]){ temp = a[i]; a[i]= a[i+1]; a[i+1] = temp; } } } for(i=0;i<a.length;i++){ System.out.println(a[i]); } } }
3 Answers
+ 1
Because this is not bubble sort :3 You should check your algorithm. I think it should be "if (a[x]>a[x+1])" and swap a[x], not a[i].
+ 3
Thanks already got it, its a[x] instead of a[i] and instead of x-- its x++
0
i cant really tell your doing here but..
import. java.util.*; << not sure if this plays up somtimes i had a problem with it once
int [] a = << i usally put this before symbol
try putting ; at the end of ++ --
hope it helps