-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting.cpp
More file actions
57 lines (53 loc) · 966 Bytes
/
sorting.cpp
File metadata and controls
57 lines (53 loc) · 966 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
52
53
54
55
56
57
//Sandip Nair 106116078
#include <iostream>
//insertion sort algorithm
using namespace std;
class array
{
int *ar,n;
public:
array()
{
ar = nullptr;
n = 0;
}
array(int x)
{
n = x;
ar = new int[n];
}
void accept()
{
for(int i = 0;i<n;++i)
cin>>ar[i];
}
void display()
{
for(int i = 0;i<n;++i)
cout<<ar[i]<<"\t";
cout<<endl;
}
void insertionSort()
{
for(int i = 1;i<n;i++)
{
int x = ar[i],j;
for(j = i-1;j >= 0 && ar[j] > x;j--)
ar[j+1] = ar[j];
ar[j+1] = x;
}
}
};
int main()
{
int n;
cout<<"Enter the number of integers:"<<endl;
cin>>n;
array a1(n);
cout<<"Enter "<<n<<" integers for the array:"<<endl;
a1.accept();
a1.insertionSort();
cout<<"The sorted array is:"<<endl;
a1.display();
return 0;
}