-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.cpp
More file actions
52 lines (42 loc) · 1.17 KB
/
Copy pathBubbleSort.cpp
File metadata and controls
52 lines (42 loc) · 1.17 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
//g++ bubble_sort.cpp -o bubble_sort Para compilar
//./bubble_sort 64 34 25 12 22 11 90 Para ejecutar
#include <iostream>
#include <vector>
#include <cstdlib> // for atoi
using namespace std;
void bubble_sort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main(int argc, char* argv[]) {
if (argc < 2) {
cout << "Usage: " << argv[0] << " <list of numbers>" << endl;
return 1;
}
// Convert arguments into integers
vector<int> my_list;
for (int i = 1; i < argc; i++) {
my_list.push_back(atoi(argv[i]));
}
// Keep a copy of the original list
vector<int> original = my_list;
// Sort the list
bubble_sort(my_list);
// Print results
cout << "Original list: ";
for (int num : original) cout << num << " ";
cout << endl;
cout << "Sorted list: ";
for (int num : my_list) cout << num << " ";
cout << endl;
return 0;
}