-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.cpp
More file actions
54 lines (41 loc) · 1.13 KB
/
insertionSort.cpp
File metadata and controls
54 lines (41 loc) · 1.13 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
// /*----------------------------------------------------------------------------------------------------*/
// //************************* Insertion sort *********************
//
// * Language used- C++
// * Function Used-
// * void inputFile(vector<int>& vec,int n)--input array function
// -----------------------------------------------------------------------------------------------------------------------*/
#include<bits/stdc++.h>
using namespace std;
void inputFile(vector<int>& vec,int n){
int val;
ifstream in_file("files/inputArray.txt");
for(int i{0};i<n;i++){
in_file>>val;
vec.push_back(val);
}
}
int main()
{
int val,n,j,key;
vector<int> vec;
cout<<"Enter the no. of Elements:";
cin>>n;
inputFile(vec,n);
for (int i = 1; i < n; i++)
{
key = vec[i];
j = i - 1;
while (j >= 0 && vec[j] > key)
{
vec[j + 1] = vec[j];
j = j - 1;
}
vec[j + 1] = key;
}
cout<<"\n SORTED ARRAY \n ";
for(int i=0;i<n;i++)
cout<<vec[i]<<" | ";
cout<<endl;
return 0;
}