-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.cpp
More file actions
43 lines (36 loc) · 768 Bytes
/
bubbleSort.cpp
File metadata and controls
43 lines (36 loc) · 768 Bytes
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
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// Working bubble sort algorithm
void bubbleSort(vector<int> &vec)
{
vector<int>::size_type vecSize = vec.size();
bool swapped = true;
for(int i = 1; i < vecSize && swapped; ++i)
{
swapped = false;
for(int j = 0; j < vecSize - i; ++j)
{
if(vec[j] > vec[j+1])
{
swap(vec[j], vec[j+1]);
swapped = true;
}
}
}
}
int main()
{
int ints[] = {32,71,12,45,26,88,53,33, 1};
vector<int> vec(ints, ints + sizeof(ints)/sizeof(int));
bubbleSort(vec);
cout << "Sorted vector:";
for(vector<int>::iterator it = vec.begin(); it != vec.end(); it++)
{
cout << ' ' << *it;
}
cout << '\n';
return 0;
}