-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBUBBLE_SORT.cpp
65 lines (65 loc) · 902 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Bubble sorting.
#include<iostream.h>
#include<conio.h>
class DataArray
{
protected:
int a[100],n;
public:
void GetData(void);
void Display(void);
};
void DataArray::GetData(void)
{
cout<<"\n Enter the number of elements to be sorted : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<" Enter element no. "<<i+1<< " :";
cin>>a[i];
}
}
void DataArray::Display(void)
{
for(int i=0;i<n;i++)
cout<<" "<<a[i];
cout<<endl;
}
class Bubble : public DataArray
{
public:
void BubbleSort(void);
};
void Bubble::BubbleSort(void)
{
int flag=1,i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=0;
}
cout<<"\n Array after iteration "<<i<<": ";
Display();
if(flag)
break;
else
flag=1;
}
}
int main()
{
Bubble B;
clrscr();
B.GetData();
B.BubbleSort();
cout<<"\n\t The Sorted Array";
cout<<"\n\t **************\n";
B.Display();
getch();
return 0;
}