-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbubbleSort.cpp
More file actions
40 lines (39 loc) · 811 Bytes
/
bubbleSort.cpp
File metadata and controls
40 lines (39 loc) · 811 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
39
40
#include <iostream>
#include <ctime>
using namespace std;
//time O(N^2) and Space O(1)
bool compare(int a, int b)
{
return a > b;
}
//Sort the elements in increasing order
void bubble_sort(int a[], int n)
{
int flag = 0;
for (int times = 0; times < n - 1; times++)
{
//repeated swapping
for (int j = 0; j < n - times - 1; j++)
{
if (compare(a[j], a[j + 1]))
{
swap(a[j], a[j + 1]);
flag = 1;
}
}
//it will be zero if after first iteration no swaps took place as the array was in sorted order
if (flag == 0) //giving this check reduces time to O(N) for sorted qarray rather than O(N^2)
break;
}
}
int main()
{
int arr[] = {-2, 3, 4, -1, 5, -12, 6, 1, 3};
int n = sizeof(arr) / sizeof(int);
bubble_sort(arr, n);
for (auto i : arr)
{
cout << i << " ";
}
return 0;
}