Modified Bubble Sort

Let’s optimize the bubble sort.

Introduction

The array is already sorted when there is no more swapping in one run of the outer loop. We stop sorting at this point. When we know that the array is almost sorted, the enhancement to the bubble sort can be particularly valuable.

Code example

Let’s optimize our code.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.