-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_sort.cpp
44 lines (39 loc) · 988 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
/*
Best-case time complexity O(n)
Average-case time complexity O(n^2)
worst-case time complexity O(n^2)
space complexity O(1)
it is inplace and stable sorting algorithm
*/
/* hint to remember:-
take the biggest element to the end by swapping between nearest element. (i.e. 1&2, 2&3, 3&4....)
*/
#include <iostream>
using namespace std;
int main(){
cout<<"enter the size of array?"<<endl;
int n,k;
cin>>n;
cout<<"enter the elements of the array......."<<endl;
int arr[n],flag;
for(int i=0;i<n;++i){
cin>>arr[i];
}
for(int i=0;i<n-1;++i){
for(int k=0;k<n-1-i;++k){
flag=0;
if(arr[k] > arr[k+1]){
int temp=arr[k];
arr[k]=arr[k+1];
arr[k+1]=temp;
flag=1;
}
}
if(flag==0) break;
}
cout<<"sorted array: ";
for(int i=0;i<n;++i){
cout<<arr[i]<<" ";
}cout<<endl;
return 0;
}