-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.cpp
More file actions
58 lines (57 loc) · 1.44 KB
/
insertionSort.cpp
File metadata and controls
58 lines (57 loc) · 1.44 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
#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 display();
void isort();
};
template<class T>
void array<T>::readarray()
{
for(int i=0;i<size;i++)
cin>>element[i];
}
template<class T>
void array<T>::display()
{
for(int i=0;i<size;i++)
cout<<element[i]<<"\t";
}
template<class T>
void array<T>::isort()
{
for(int i=0;i<size;i++)
{
int j=i+1;
T elt=element[j];
while(j>0 && element[j-1]>elt)
{
element[j]=element[j-1];
j=j-1;
}
element[j]=elt;
}
}
int main()
{
array<int> d(10);
cout<<"\n enter elements:";
d.readarray();
cout<<"\n entered array is:";
d.display();
d.isort();
cout<<"\n the sorted array is:";
d.display();
return 0;
}