-
Notifications
You must be signed in to change notification settings - Fork 17
/
ds question 8.cpp
69 lines (67 loc) · 1.11 KB
/
ds question 8.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
66
67
68
69
#include<iostream>
using namespace std;
class sorting
{
int n;
int *a;
int c;
public:
sorting(int size)
{
n=size;
a=new int[n];
}
void input();
void display();
void BubbleSort();
};
void sorting::input()
{
for(int i=0;i<n;i++)
cin>>a[i];
}
void sorting::BubbleSort()
{
int i,j,f=0,sum=0;
for(i=n;i>1;i--)
{
c=0;
for(j=0;j<i-1;j++)
{
c++;
if(a[j]>a[j+1])
{
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
++f;sum+=f;
cout<<endl<<"The number of comparisons is "<<c<<endl;
cout<<"Pass number "<<f<<endl;
cout<<"Intermediate array ";
display();
}
cout<<" Total number of comparisons is "<<+sum<<endl;
}
void sorting::display()
{
cout<<endl;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
int main()
{
int size;
cout<<"Enter the size of array"<<endl;
cin>>size;
sorting sort(size);
cout<<"Enter the elements in the array "<<endl;
sort.input();
cout<<endl<<"The array before sorting is";
sort.display();
sort.BubbleSort();
cout<<endl<<"The array after sorting is ";
sort.display();
}