bubble sort

PHOTO EMBED

Tue Nov 05 2024 20:14:48 GMT+0000 (Coordinated Universal Time)

Saved by @Yakostoch #c++

#include <iostream>
#include <vector>
using namespace std; 
 
int main() {
    vector<int> arr = { 64, 34, 25, 12, 22, 11, 90 };
    int n = arr.size();
    bool swapped;
  
    for (int i = 0; i < n - 1; i++) {
        swapped = false;
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = true;
            }
        }
      
        // If no two elements were swapped, then break
        if (!swapped)
            break;
    }
    
    for (int num : arr)
        cout << " " << num;
}
content_copyCOPY

пузырек