-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbubble_sort.cpp
More file actions
41 lines (39 loc) · 905 Bytes
/
bubble_sort.cpp
File metadata and controls
41 lines (39 loc) · 905 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
#include<bits/stdc++.h>
using namespace std;
void bubble_sort(int a[],int n){
for(int i=0;i<n-1;i++){ //sorting upto n-1 as the last element will automatically be swapped
for(int j=0;j<n-i-1;j++){ //running the check upto n-i-1 as after every iteration of the outter loop the last element
if(a[j]>a[j+1]){ //gets sorted in its right place.
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
// for(int i=0;i<n;i++){
// cout<<a[i]<<" ";
// }
// cout<<endl;
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n;
cin>>n; //size of the array
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"Array before sorting\n";
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
// cout<<endl;
bubble_sort(arr,n);
cout<<"\nArray after sorting\n";
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
}