+ 2

Why I am getting compilation error/no output for the below insertion sort code?

class MyClass {     public static void main(String[ ] args) {         int a[] = {4,3,2,1};         int n=4;         for(int i=1; i<n ;i++)         { int j=i; System.out.println("hi"); int back= a[i-1];         while(a[j]<back && j>0)         { a[j-1] = a[j]; j--;         } a[j]=back;         }         for(int k=0; k<n ;k++)         {             System.out.println(a[k]);         }     } }

17th Sep 2017, 2:48 PM
Anil Patil
Anil Patil - avatar
1 Réponse
+ 4
There seems to be lots of hidden 'new line' characters in the code that's causing the issue. I assume you copy pasted from something or saved the file in UTF format¿ (Probably more things that can cause this though) Try removing all the spaces. Then when all the code is on one line, enter it back to look normal again. If you are using an IDE there may be a 'replace all' option, where you can replace all spaces with nothing. If you use a text-editor there may be an option to 'see invisible characters'. Perhaps that can help so you don't have to remove every space.
17th Sep 2017, 3:50 PM
Rrestoring faith
Rrestoring faith - avatar