Skip to content

Instantly share code, notes, and snippets.

@qrno
Created January 22, 2017 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qrno/c09b8214bd2181a7ffc2cef0e8ee8ff2 to your computer and use it in GitHub Desktop.
Save qrno/c09b8214bd2181a7ffc2cef0e8ee8ff2 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
void swap(int &x, int &y) {
int aux = x;
x = y;
y = aux;
}
void bubble_sort(int list[], int listSize) {
bool swapped = false;
do {
swapped = false;
for (int i = 0; i < listSize-1; i++) {
if(list[i] > list[i+1]) {
swap(list[i], list[i+1]);
swapped = true;
}
}
} while (swapped);
}
int main() {
int list[5] = {31, 41, 59, 26, 53};
int listSize = 5;
bubble_sort(list, listSize);
for (int i = 0; i < listSize; i++) {
cout << list[i] << " ";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment