forked from MRL-HSL-Software/Fall2023-MBT2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionsort.cpp
More file actions
56 lines (53 loc) · 924 Bytes
/
insertionsort.cpp
File metadata and controls
56 lines (53 loc) · 924 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
#include <iostream>
#include <vector>
using namespace std;
class Insertion {
vector <int> numbers;
public:
void input();
int sort();
void print();
};
void Insertion::input()
{
int size;
cout<<"what size vector do you want";
cin>>size;
for (int i = 0; i < size; i++)
{
int number;
cout<<"enter number";
cin>>number;
numbers.push_back(number);
}
}
int Insertion::sort()
{
int n=numbers.size();
int j;
for (int i = 1; i < n; i++)
{
int key=numbers[i];
int j=i-1;
while (j>=0 && numbers[j]>key)
{
numbers[j+1]=numbers[j];
j--;
}
numbers[j+1]=key;
}
}
void Insertion::print()
{
for (int i = 0; i < numbers.size(); i++)
{
cout<<numbers[i]<<" ";
}
}
int main(){
Insertion I;
I.input();
I.sort();
I.print();
return 0;
}