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.
// Write your code hereAtomicIntegercount=newAtomicInteger();AtomicIntegertotalCount=newAtomicInteger();count.set(0);totalCount.set(0);do{count.set(0);IntStream.range(0,a.size()-1).forEach(x->{if(a.get(x)>a.get(x+1)){Collections.swap(a,x,x+1);totalCount.incrementAndGet();count.incrementAndGet();}});}while(count.get()>0);System.out.println(String.format("Array is sorted in %s swaps.",totalCount));System.out.println(String.format("First Element: %s",a.get(0)));System.out.println(String.format("Last Element: %s",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
Day 20: Sorting
You are viewing a single comment's thread. Return to all comments →
Java