Bubble Sort

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <vector> using namespace std; //O(n^2) void BubbleSort(int size, vector<int> & Arr){ if(size > 1){ int tmp; for (int i = 1; i < size; i++){ if(Arr[i] < Arr[i-1]){ tmp = Arr[i]; Arr[i] = Arr[i-1]; Arr[i-1] = tmp; } //compare starting at the second one. } //loops until it gets to the last one size -= 1; BubbleSort(size, Arr); } //the vector ain't just 1 number to be evaluated. else return; } int main() { vector <int> Arr {51, 65, 3, 658, 89, 1, 6, 8, 452, 87}; int size = Arr.size(); BubbleSort(size, Arr); for(int i = 0; i < Arr.size(); i ++) cout << Arr[i] << endl; return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines