Skip to content

Instantly share code, notes, and snippets.

@YeyoM
Created February 22, 2023 15:29
Show Gist options
  • Save YeyoM/5df0b7a17af23c81e04a92d17144e5d4 to your computer and use it in GitHub Desktop.
Save YeyoM/5df0b7a17af23c81e04a92d17144e5d4 to your computer and use it in GitHub Desktop.
Bubble Sort implemented on cpp
// Diego Emilio Moreno Sanchez
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n - 1; j++) {
if(arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {5, 4, 3, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
bubbleSort(arr, n);
for(int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment