-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort.cpp
More file actions
36 lines (35 loc) · 841 Bytes
/
bubble_sort.cpp
File metadata and controls
36 lines (35 loc) · 841 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
//Implementation of bubble sort using c++
#include<iostream>
using namespace std;
int main(){
int n,i;
cout<<"Enter the number of element that you want to give in the array :\n";
cin>>n;
int array[n];
cout<<"Enter "<<n<<" element in the array :\n";
for(i=0;i<n;i++){
cin>>array[i];
}
int counter=1;
while(counter<n){
for(i=0;i<n-counter;i++){
if(array[i]>array[i+1]){
int temp=array[i];
array[i]=array[i+1];
array[i+1]=temp;
}
}
counter++;
}
cout<<"Array in sorted order is :\n";
for(i=0;i<n;i++){
cout<<array[i]<<" ";
cout<<endl;
}
}
// we can also use that
// for(i=0;i<n-1;i++){
// for(j=0;j<n-i-1;j++){
// swapping
// }
// }