We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
staticvoidcountSwaps(int[]a){intcount=0;booleanflag=false;while(!flag){flag=true;for(inti=0;i<a.length-1;i++){if(a[i]>a[i+1]){inttemp=a[i];a[i]=a[i+1];a[i+1]=temp;flag=false;count++;}}}System.out.println(String.format("Array is sorted in %d swaps.%n"+"First Element: %d%n"+"Last Element: %d%n",count,a[0],a[a.length-1]));}
Sorting: Bubble Sort
You are viewing a single comment's thread. Return to all comments →
JAVA 8 BubbleSort Solution: