-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble_sort.cpp
36 lines (29 loc) · 936 Bytes
/
bubble_sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
#include <assert.h>
#include "utilities.h"
using namespace std;
/*****************************************************************************************
* bubble sort
****************************************************************************************/
//pseudocode found on Wikipedia
void bubble_sort(vector<int> &array) {
bool swapped = true;
int length = array.size();
while(swapped) {
int i;
swapped = false;
for(i = 1; i < length; i++) {
if(array[i-1] > array[i]) {
swap( array[i-1], array[i]);
swapped = true;
}
}
}
//expensive, so comment out when checked
//assert(is_increasingly_sorted(array));
//print_array(array);
}
/*****************************************************************************************
* end
****************************************************************************************/