forked from IOSD/Algo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBubble_Sort.cpp
38 lines (33 loc) · 945 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
//BUBBLE SORT ALGORITHM
#include<iostream>
using namespace std;
void bsort(int a[],int n) //FUNTION FOR BUBBLE SORT TAKING ARRAY AND IT'S SIZE AS ARGUMENTS
{
int t;
for(int i=1;i<n;i++)
for(int j=0;j<n-i;j++)
if(a[j+1]<a[j])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
//TIME COMPLEXITY : О(n^2)
//SPACE COMPLEXITY : O(n)
int main()
{
int *b,n;
cout<<"Enter NO. of elements:";
cin>>n; //NO. OF ELEMENTS
b=new int[n]; //DYNAMIC INITIALISATION OF ARRAY
cout<<"\nEnter the array:";
for(int i=0;i<n;i++) //INPUT OF ARRAY
cin>>b[i];
bsort(b,n); //CALLING FUNCTION SSORT
cout<<"\nSorted Array:";
for(int i=0;i<n;i++) //DISPLAY OF SORTED ARRAY
cout<<b[i]<<"\n";
delete b;
return 0;
}