Interview Preparation Kit/Sorting/bubble_sort.cpp
// Complete the countSwaps function below.
Complete the countSwaps function below.
Complete the countSwaps function below .
void countSwaps(vector<int> a) {
    int max = *max_element(a.begin(), a.end());
    int min = *min_element(a.begin(), a.end());
    int temp = 0;
    int swap_count = 0;
    for (int x = 0; x < a.size(); ++x) {
        for (int y = 0; y < a.size() - 1; ++y) {
            if (a[y] > a[y + 1]) {
                temp = a[y];
                a[y] = a[y + 1];
                a[y + 1] = temp;
                ++swap_count;
            }
        }
    }
    cout << "Array is sorted in " << swap_count << " swaps." << endl;
    cout << "First Element: " << min << endl;
    cout << "Last Element: " << max << endl;
}
void countSwaps ( vector < int > a ) { int max = * max_element ( a . begin ( ) , a . end ( ) ) ; int min = * min_element ( a . begin ( ) , a . end ( ) ) ; int temp = 0 ; int swap_count = 0 ; for ( int x = 0 ; x < a . size ( ) ; ++ x ) { for ( int y = 0 ; y < a . size ( ) - 1 ; ++ y ) { if ( a [ y ] > a [ y + 1 ] ) { temp = a [ y ] ; a [ y ] = a [ y + 1 ] ; a [ y + 1 ] = temp ; ++ swap_count ; } } } cout << " " << swap_count << " " << endl ; cout << " " << min << endl ; cout << " " << max << endl ; }
Complete the countSwaps function below.
Complete the countSwaps function below .
// Could opt for printf instead