-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble-sort.cpp
More file actions
51 lines (39 loc) · 828 Bytes
/
Copy pathbubble-sort.cpp
File metadata and controls
51 lines (39 loc) · 828 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
#include<iostream>
using namespace std ;
// program for sort elements in array using bubble sort .
int main()
{
int i , a[50] , n , j ,temp ;
cout << " How many elements in array : " ;
cin >> n ;
cout << " \n Enter the elements in array" << endl ;
for(i=0 ; i<n ; i++)
{
cin >> a[i] ;
}
cout << " \n " ;
cout << " The numbers are" << endl ;
for(i=0 ; i<n ; i++)
{
cout << "\n a" << "[" << i << "] : " << a[i] << endl;
}
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < n-i-1 ; j++)
{
if(a[j] > a[j+1]) // swap
{
temp = a[j] ;
a[j] = a[j+1] ;
a[j+1] = temp ;
}
}
}
cout << "\n\n" ;
cout << "****** sorted number ******" << endl ;
for(i=0 ; i<n ; i++)
{
cout << "\n" ;
cout << a[i] ;
}
}