-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble_Sort(Recursion).cpp
More file actions
59 lines (51 loc) · 1.11 KB
/
Bubble_Sort(Recursion).cpp
File metadata and controls
59 lines (51 loc) · 1.11 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
#include <iostream>
#include <vector>
using namespace std;
void bubbleSort(vector<int> &arr, int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
// If jth element is greater than 'j + 1' th element, swap them
if (arr[j] > arr[j + 1])
{
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
vector<int> arr;
int n;
// Taking user input for the array size
cout << "Enter the size of the array: ";
cin >> n;
// Taking user input for the array elements
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
{
int num;
cin >> num;
arr.push_back(num);
}
// Before sorting
cout << "Array before sorting: ";
for (int num : arr)
{
cout << num << " ";
}
cout << endl;
bubbleSort(arr, n);
// After sorting
cout << "Array after sorting: ";
for (int num : arr)
{
cout << num << " ";
}
cout << endl;
return 0;
}