-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubblesorting.cpp
45 lines (40 loc) · 891 Bytes
/
bubblesorting.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
45
#include<iostream>
using namespace std;
void bubblesort(int arr[], int n){
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-i-1; j++)
{
if (arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
int main(){
int *a;
int n;
cout<<"Enter the size of the array:"<<endl;
cin>>n;
a=new (nothrow) int[n];
cout<<"Enter the elements of the array:"<<endl;
for (int i = 0; i < n; i++)
{
cin>>a[i];
}
cout<<"The arrays in unsorted order is:"<<endl;
for (int i = 0; i < n; i++)
{
cout<<a[i]<<"\t";
}
bubblesort(a,n);
cout<<endl<<"The arrays in sorted order is:"<<endl;
for (int i = 0; i < n; i++)
{
cout<<a[i]<<"\t";
}
return 0;
}