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.
publicstaticvoidcountSwaps(List<Integer>a){intcount=0;for(inti=0;i<a.size();i++){for(intj=0;j<a.size()-1;j++){if(a.get(j)>a.get(j+1)){intaux=a.get(j);a.set(j,a.get(j+1));a.set(j+1,aux);count++;}}}System.out.println("Array is sorted in "+count+" swaps.");System.out.println("First Element: "+a.get(0));System.out.println("Last Element: "+a.get(a.size()-1));}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sorting: Bubble Sort
You are viewing a single comment's thread. Return to all comments →
JAVA 8