TheAlgorithms / Ruby

All algorithms implemented in Ruby
MIT License
1.15k stars 289 forks source link

New improved implementation of bubble sort #186

Closed shanshaji closed 2 years ago

shanshaji commented 2 years ago

The current implementation is iterating/looping the length of the array inside the loop. The bubble sort is called bubble sort because in each pass-through, the highest unsorted value “bubbles” up to its correct position. So that those elements can be omitted in the upcoming loop. This new implementation wouldn't go through the whole array every time. Once an element is sorted, it's not considered in the next loop