|
@@ -0,0 +1,55 @@
|
|
|
|
+#include <iostream>
|
|
|
|
+#include <vector>
|
|
|
|
+
|
|
|
|
+#include <fstream>
|
|
|
|
+
|
|
|
|
+using namespace std;
|
|
|
|
+void display(const vector<int>& v, ostream& os = cout);
|
|
|
|
+
|
|
|
|
+void display(const vector<int>& v, ostream& os) {
|
|
|
|
+ for (vector<int>::const_iterator iter = v.begin(); iter != v.end(); ++iter) {
|
|
|
|
+ os << *iter << " ";
|
|
|
|
+ }
|
|
|
|
+ os << endl;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void swap(int& value_a, int& value_b) {
|
|
|
|
+ int temp = value_a;
|
|
|
|
+ value_a = value_b;
|
|
|
|
+ value_b = temp;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void buble_sort(vector<int>& v, ostream* ofil=0) {
|
|
|
|
+ vector<int>::iterator ix, iy;
|
|
|
|
+
|
|
|
|
+ for (ix = v.begin(); ix != v.end(); ++ix) {
|
|
|
|
+ for (iy = ix + 1; iy != v.end(); ++iy) {
|
|
|
|
+ if (*ix > *iy) {
|
|
|
|
+ if (ofil) {//need include #include <fstream>
|
|
|
|
+ (*ofil) << *ix << " " << *iy << endl;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ swap(*ix, *iy);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int main() {
|
|
|
|
+ vector<int> v = { 0, 1, 0,5,4,3 };
|
|
|
|
+
|
|
|
|
+ display(v);
|
|
|
|
+
|
|
|
|
+ //swap(v[0], v[1]);
|
|
|
|
+
|
|
|
|
+ //display(v);
|
|
|
|
+
|
|
|
|
+ ofstream fout("test.txt");
|
|
|
|
+ buble_sort(v, &fout);
|
|
|
|
+ display(v);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|