-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch2_bubbleSort.cpp
More file actions
71 lines (58 loc) · 1.28 KB
/
Copy pathch2_bubbleSort.cpp
File metadata and controls
71 lines (58 loc) · 1.28 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
void display(const vector<int> &vec)
{
for(int i = 0; i < vec.size(); ++i)
cout << vec[i] << ' ';
// vec.emplace(10); //error, since const!
cout << endl;
}
void display(const vector<int> *vec)
{
if(!vec)
{
cerr << "display(): the vector pointer is 0\n";
return;
}
cout << "display(const vector<int>*) pointer version:\n";
for(int i = 0; i < vec->size(); ++i)
cout << (*vec)[i] << ' ';
cout << endl;
}
void swap(int &v1, int &v2)
{
int temp = v1;
v1 = v2;
v2 = temp;
}
ofstream out("debug.txt");
void bubble_sort(vector<int> &vec)
{
for(int i = 0; i < vec.size(); ++i)
for(int j = i + 1; j < vec.size(); ++j)
if(vec[i] > vec[j])
{
out << "about to call swap!"
<< " i: " << i << " j: " << j << "\t"
<< " swapping: " << vec[i]
<< " with " << vec[j] << endl;
swap(vec[i], vec[j]);
}
}
int main()
{
int a = 9, b = 10;
swap(a, b);
cout << a << ' ' << b << endl;
int ia[8] = {8, 34, 3, 13, 1, 21, 5, 2};
vector<int> vec(ia, ia+8); //template<typename InputIt> vector(InputIt first, InputIt last)
cout << "vector before sort: ";
display(vec);
bubble_sort(vec);
cout << "vector after sort: ";
display(vec);
display(&vec); //pointer version
return 0;
}