forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble-sort.cpp
54 lines (47 loc) · 990 Bytes
/
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
#include <iostream>
using namespace std;
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
cout << " " << array[i];
}
cout << "\n";
}
void bubbleSort(int array[], int size) {
for (int i = 0; i < size - 1; ++i) {
int swapped = 0;
for (int j = 0; j < size - i - 1; ++j) {
//sorting
if (array[j] > array[j + 1]) {
// Swap if greater is at the rear position
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = 1;
}
}
// If there is not swapping in the last swap, then the array is already sorted.
if (swapped == 0)
break;
}
printArray(array,size);
}
//sample
//enter the size5
//enter thr nos.6
// 1
// 23
// 65
// 9
// 1 6 9 23 65
int main(){
int n;
cout<<"enter the size : ";
cin>>n;
cout<<"enter the nos. ";
int ar[n];
for(int i=0;i<n;i++){
cin>>ar[i];
}
bubbleSort(ar,n);
return 0;
}