-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbubble_sort.cpp
More file actions
55 lines (44 loc) · 730 Bytes
/
bubble_sort.cpp
File metadata and controls
55 lines (44 loc) · 730 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<stdio.h>
#include<iostream>
using namespace std;
void bubble_sort(int a[],int n);
int main()
{
int i,a[10],n;
cout<<"Enter the no of array elements ";
cin>>n;
cout<<"Enter the array elements ";
for(i=0;i<n;i++)
cin>>a[i];
bubble_sort(a,n);
cout<<"The sorted array is as follows \n";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
return 0;
}
void bubble_sort(int a[],int n)
{
int i,j,temp;
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
/* Output
Enter the no of array elements 5
Enter the array elements 22
66
44
99
11
The sorted array is as follows
11 22 44 66
*/