Time limit exceeded(Bubble Sort) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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]); } } }

24th Jun 2017, 12:09 PM
Joe McMillan
Joe McMillan - avatar
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].
24th Jun 2017, 1:44 PM
Nguyễn Khải
Nguyễn Khải - avatar
+ 3
Thanks already got it, its a[x] instead of a[i] and instead of x-- its x++
24th Jun 2017, 2:19 PM
Joe McMillan
Joe McMillan - avatar
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
24th Jun 2017, 1:55 PM
Uni Verse
Uni Verse - avatar