-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble_Sort.cpp
More file actions
38 lines (33 loc) · 814 Bytes
/
Copy pathBubble_Sort.cpp
File metadata and controls
38 lines (33 loc) · 814 Bytes
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
#include <bits/stdc++.h>
using namespace std;
// Time Complexity = O(n^2).
// Space Complexity = O(1).
void bubbleSort(int a[], int n)
{
// outer loop will run (n-1) times
for (int i = 0; i < n - 1; i++)
{
bool isSwapped = false;
for (int j = 0; j < n - i - 1; j++)
{
if (a[j] > a[j + 1])
{
swap(a[j], a[j + 1]);
isSwapped = true;
}
}
// if we haven't swapped any element then the array is already sorted.
if (isSwapped == false)
break;
}
// Printing the sorted array.
for (int i = 0; i < n; i++)
cout << a[i] << " ";
}
int main()
{
int a[] = {3, 4, 1, 2, 5};
int n = sizeof(a) / sizeof(a[0]);
bubbleSort(a, n);
return 0;
}