Why do we multiply 2 inside the for loop?

I think that this is an basic implementation of a bubble sort. You can find an explication of this algorithm on wiki.

In brief, this algorithm will check each pair and inverse it if needed. For having a totally sorted result, they need to parse the array n*n ot n2 and not n*2 at worst.

By the way, Arrays.sort(words); already sort your array.

Leave a Comment