-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBubbleSort.cpp
51 lines (41 loc) · 1.41 KB
/
BubbleSort.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>
using namespace std;
// BUBBLE SORT
// if arr[i] > arr[i+1] swap them. To place the element in their respective position, we have to do the following operation N-1 times.
// Time complexity is --- O(n^2) --- (Big O of n square)
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << "\n";
}
int main() {
const int MAX_SIZE = 100;
int size;
int arr[MAX_SIZE];
cout << "Enter the size of the array:";
cin >> size;
if (size > MAX_SIZE) {
cout << "Size exceeds maximum allowed size of " << MAX_SIZE << endl;
return 1;
}
cout << "Enter the elements of the array:";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
cout << "Array before sorting:";
printArray(arr, size);
// Sorting Mechanism
for(int i = 0; i < size-1; i++) {
for(int j = 0; j < size-i-1; j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
cout<<"Array after sorting:";
printArray(arr, size);
return 0;
}