
Bubble Sort
Bubble sort is a simple sorting algorithm that works by repeatedly comparing adjacent elements in an array and swapping them if they are in the wrong order.
Use Case
Use Case#include <bits/stdc++.h>
void BubbleSort(vector<int>& arr, int n){
for(int i = 1; i < n; i++){
// for Round i to n-1
bool isSorted = true; // condition for if the given array already sorted
for(int j = 0; j < n-i; j++){
// process elements till n-i index
if(arr[j] > arr[j+1]){
swap(arr[j], arr[j+1]);
isSorted = false; // not sorted
}
}
if(isSorted)
break;
}
}Last updated