-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.cpp
More file actions
69 lines (66 loc) · 1.17 KB
/
Copy pathbubbleSort.cpp
File metadata and controls
69 lines (66 loc) · 1.17 KB
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
#include<iostream>
using namespace std;
template<class T>
class array
{
private:
int size;
T *element;
public:
array(int s)
{
size=s;
element=new T[size];
}
void readarray();
void sort();
void display();
void swap(int,int);
};
template<class T>
void array<T>::readarray()
{
for(int i=0;i<=size-1;i++)
cin>>element[i];
}
template<class T>
void array<T>::sort()
{
int i,j;
for(i=0;i<=size-1;i++)
{
for(j=size-1;j>=i;j--)
if(element[j-1]>element[j])
{
swap(j-1,j);
}
}
}
template<class T>
void array<T>::display()
{
cout<<" elements are";
for(int i=0;i<=size-1;i++)
cout<<element[i]<<"\t";
}
template<class T>
void array<T>::swap(int i,int j)
{
T t=element[j-1];
element[j-1]=element[j];
element[j]=t;
}
int main()
{
array<int>a(10);
cout<<"enter elements";
a.readarray();
cout<<"the entered array is:";
a.display();
a.sort();
cout<<"\n";
cout<<"the elements after sorting:\n ";
a.display();
cout<<"\n";
return 0;
}