forked from IOSD/Algo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathins_sort.cpp
44 lines (41 loc) · 1.17 KB
/
ins_sort.cpp
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
//INSERTION SORT
#include<iostream>
#include<climits> //ADDING CLIMITS TO GET INT_MIN VALUE
using namespace std;
void inssort(int a[],int n) //FUNCTION INSERTION SORT TAKING ARGUMENTS ARRAY & SIZE - N
{ int i,j;
a[0]=INT_MIN; //GIVING FIRST ELEMENT OF ARRAY MINIMUM VALUE
for(i=1;i<=n;i++)
{
int t=a[i];
j=i-1;
while(t<a[j]) //CJECKING OF COND & MOVING ELEMENTS
{
a[j+1]=a[j];
j--;
}
a[j+1]=t; //PUTTING FINAL ELEMENTS
}
}
//TIME COMPLEXITY:O(n^2)
//SPACE COMPLEXITY : O(1)
int main() //MAIN BODY
{
int *a,n,i;
cout<<"Enter No OF Units : ";
cin>>n; // TAKING INPUT OF SIZE OF ARRAY
a=new int[n+1];
for(int i=1;i<=n;i++) //INPUT OF ARRAY
{
cin>>a[i];
}
inssort(a,n); //CALLING INSERTION SORT FUNCTION
cout<<endl;
for(int i=1;i<=n;i++)
{
cout<<a[i]<<endl; //PRINTING OF MODIFIED ARRAY
}
system("pause");
delete a;
return 0;
}