forked from Sniper7sumit/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble_Sort.cpp
More file actions
47 lines (35 loc) · 823 Bytes
/
Copy pathBubble_Sort.cpp
File metadata and controls
47 lines (35 loc) · 823 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
41
42
43
44
45
46
47
// Bubble Sort == Reapetedly swap two adjacent elements if they are in wrong order
// For n elements in array == (n-1) iterations to get sorted array
// If ith iteration == Check upto (n-i)
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n];
for(int i=0;i<n;i++)
{
cin >> arr[i];
}
int counter = 1;
while(counter < n-1)
{
for(int i=0;i<n-counter;i++)
{
if(arr[i]>arr[i+1])
{
// Swapping the numbers
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
counter++;
}
for(int i=0;i<n;i++)
{
cout << arr[i] << " ";
}
return 0;
}