-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble-sort.cpp
51 lines (46 loc) · 914 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int no_of_item;
bool swap_check = true;
vector<int> numbers;
cout << "Enter the amount of numbers to sort: ";
cin >> no_of_item;
cout << "Enter " << no_of_item << " numbers: ";
// Input
int num;
for (int i = 0; i < no_of_item; i++)
{
cin >> num;
numbers.push_back(num);
}
// Bubble Sorting
for (int i = 0; (i < no_of_item) && (swap_check); i++)
{
swap_check = false;
for (int j = 0; j < no_of_item - 1 - i; j++)
{
if (numbers[j] > numbers[j + 1])
{
swap_check = true;
swap(numbers[j], numbers[j + 1]);
}
}
}
// Output
cout << "\nSorted Array : ";
for (int i = 0; i < numbers.size(); i++)
{
if (i != numbers.size() - 1)
{
cout << numbers[i] << ", ";
}
else
{
cout << numbers[i] << endl;
}
}
return 0;
}