6 sorting/3 Bubble Sort/main.cpp
/*If array is already sorted in before n-1'th pass then remaining pass can be skipped */
If array is already sorted in before n-1'th pass then remaining pass can be skipped
If array is already sorted in before n - 1 ' th pass then remaining pass can be skipped
void bubble_sort_efficient(int a[], int n){
    for(int i=0; i<n-1; i++){
        bool swapped = false;
        for(int j=0; j<n-i-1; j++)
            if(a[j] > a[j+1]){
                swap(a[j], a[j+1]);
                swapped = true;
            }
        if(swapped == false)
            break;
    }
}
void bubble_sort_efficient ( int a [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { bool swapped = false ; for ( int j = 0 ; j < n - i - 1 ; j ++ ) if ( a [ j ] > a [ j + 1 ] ) { swap ( a [ j ] , a [ j + 1 ] ) ; swapped = true ; } if ( swapped == false ) break ; } }
If array is already sorted in before n-1'th pass then remaining pass can be skipped
If array is already sorted in before n - 1 ' th pass then remaining pass can be skipped