-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathbubble sort.cpp
50 lines (47 loc) · 912 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
45
46
47
48
49
50
#include <iostream>
#include <conio.h>
#include <stdio.h>
using namespace std;
void bubblesort(int[],int);
int main()
{
int a[30];
int n;
cout<<"enter the size of array";
cin>>n;
cout<<"enter array elements";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
bubblesort(a,n);
cout<<"the sorted array is";
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
void bubblesort(int a[],int n)
{
int temp;
for(int i=0;i<n;i++)
{
for (int j=0;j<(n-1);j++)
{
if(a[j+1]<a[j])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
cout<<"array after every pass";
for (int j=0;j<n;j++)
{
cout<<a[j]<<" ";
}
cout<<endl;
}
}