I've always wanted to know the concept behind the condition in the 2nd loop in all bubble sortings, even tho I never use it since I use the STL cont. sort function so the condition is always j < n - i - 1 , assuming the first loop is using variable i and n is the array size. I kinda know the -1 is cause we use index+1 inside the if condition so we dont wanna go out of the array boundaries but the use of n-i is what confuses me.
Yes that's the first way that came to my mind when I was asked to write a bubble sort the first time but then I looked up bubble sort and I found examples with this method and I honestly dunno why is this SPECIFIC condition used
If I understand it, its a case of arranging the deck chairs on the titanic. That is, its an efficiency tweak to a bad algorithm.
It keeps from checking items that are already sorted.
To see this, sort a very small list of like 5 numbers, and print the list after every iteration of the inner loop.
BTW the last time I checked, which was not long ago, the std::sort was not set up for multiprocessor. It may be now... its very fast, regardless.