-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bubble_Sort.cpp
63 lines (58 loc) · 1.09 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
///Time Complexity: O(n^2) for the average case
/*
Auther : Abdullah Al Masum
*/
#include <bits/stdc++.h>
using namespace std;
#define MAXX 10000000
int Arr[MAXX];
void printArray(int A[], int N)
{
for (int i = 0; i < N; i++)
{
cout << A[i] << " ";
}
cout << endl;
}
void swapp(int *A, int i, int j)
{
int temp = A[j];
A[j] = A[i];
A[i] = temp;
}
void BubbleSort(int Arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
int adaptive = 1;
printf("Working on pass number %d\n", i + 1);
for (int j = 0; j < n - 1 - i; j++)
{
if (Arr[j] > Arr[j + 1])
{
swapp(Arr, j, j + 1);
adaptive = 0;
}
}
if (adaptive)
{
return;
}
}
}
int main()
{
//takeInput();
int size;
cin >> size;
for (int i = 0; i < size; i++)
{
cin >> Arr[i];
}
cout << "Before Sorting: " << endl;
printArray(Arr, size);
BubbleSort(Arr, size);
cout << "After Sorting:" << endl;
printArray(Arr, size);
return 0;
}