You are on page 1of 22

INSERTION SORT

DAA LAB
COURSE CODE: BCSE204P
ISHIT SETIA
REG NO:22BLC1039
FACULTY: DR. PANDIYARAJU V SIR
LAB: L13+L14
INSERTION SORT ALGORITHM:

Implementing Insertion sort algorithm in C++ Using STL and


without STL and compute the time complexity of both algorithms
by varying the input sizes:

ALGORITHM:
INSERTION-SORT
1. for j =2 to A.length
2. key = A[ j ]
3. //Insert A[ j ] into the sorted sequence A[0, .. j-1].
4. i=j–1
5. while i > 0 and A [ i ] > key
6. A[ i +1 ] = A[ i ]
7. i = i -1
8. A [i +1] = key

CODE:
#include<iostream>
#include<vector>
#include<ctime>
using namespace std;
int main()
{
vector<int> elements;
int key,i,j,n,ele;
//cout<<"Enter number of elements";
cin>>n;
for(i=0;i<n;i++)
{
cin>>ele;
elements.push_back(ele);
}
clock_t tStart = clock();
for(j=1;j<n;j++)
{
key = elements[j];
i = j-1;
while((i>=0)&&(elements[i]>key))
{
elements[i+1] = elements[i];
i = i-1;
}
elements[i+1] = key;
}
double time1=(double)(clock() - tStart)/CLOCKS_PER_SEC;
cout<<"Time taken is "<<time1<<endl;
/*for(i=0;i<n;i++)
{
cout<<elements[i]<<" ";
}*/
}

ASCENDING NUMBER GENERATION USING PYTHON:


TAKING N=5000(INPUT)
n=5000
fp = open("ascending_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(1,n):
fp.write(str(i)+" ")
fp.write(str(n))
fp.close()

TAKING INPUT AS N=10000:


n=10000
fp = open("ascending_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(1,n):
fp.write(str(i)+" ")
fp.write(str(n))
fp.close()

TAKING THE INPUT AS N=15000


n=15000
fp = open("ascending_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(1,n):
fp.write(str(i)+" ")
fp.write(str(n))
fp.close()

TAKING THE INPUT AS N=20000


n=20000
fp = open("ascending_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(1,n):
fp.write(str(i)+" ")
fp.write(str(n))
fp.close()

OUTPUT:
OUTPUT ANALYSIS:
For n=5000
Time taken : 4.1e-05

For n=10000
Time taken : 7.8e-05

For n=15000
Time taken : 0.000115

For n=20000
Time taken : 0.000153
OUTPUT ANALYSIS GRAPH:

DESCENDING NUMBER GENERATION USING PYTHON:


For n=5000:
n=5000
fp = open("descending_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(n,1,-1):
fp.write(str(i)+" ")
fp.write(str(1))
fp.close()

For n=10000
n=10000
fp = open("descending_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(n,1,-1):
fp.write(str(i)+" ")
fp.write(str(1))
fp.close()

For n=15000
n=15000
fp = open("descending_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(n,1,-1):
fp.write(str(i)+" ")
fp.write(str(1))
fp.close()

For n=20000
n=20000
fp = open("descending_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(n,1,-1):
fp.write(str(i)+" ")
fp.write(str(1))
fp.close()
OUTPUT:
Time Complexity
For n=5000
Time taken: 0.053572

For n=10000
Time taken : 0.213671

For n=15000
Time taken : 0.473799

For n=20000
Time taken : 0.837606
DESCENDING NUMBER GENERATION USING PYTHON:
For n=5000
import random
n = 5000
fp = open("random_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(1,n):
x = random.random()
fp.write(str(int(x*n))+" ")
x = random.random()
fp.write(str(int(x*n))+" ")
fp.close()

For n=10000
import random
n = 10000
fp = open("random_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(1,n):
x = random.random()
fp.write(str(int(x*n))+" ")
x = random.random()
fp.write(str(int(x*n))+" ")
fp.close()

For n=15000
import random
n = 15000
fp = open("random_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(1,n):
x = random.random()
fp.write(str(int(x*n))+" ")
x = random.random()
fp.write(str(int(x*n))+" ")
fp.close()

For n=20000
import random
n = 20000
fp = open("random_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(1,n):
x = random.random()
fp.write(str(int(x*n))+" ")
x = random.random()
fp.write(str(int(x*n))+" ")
fp.close()

OUTPUT:

For n=5000
Time taken : 0.053306

For n=10000
Time taken : 0.195349
For n=15000
Time taken : 0.43813

For n=20000
Time taken : 0.915859
ANALYZING TIME COMPLELXITIES :

CODE:
#include<iostream>
using namespace std;
#include<vector>
#include<math.h>
class point
{
int x,y;
public:
bool operator>(point);
friend istream& operator>>(istream&,point&);
friend ostream& operator<<(ostream&,point&);
};
bool point::operator>(point p)
{
float dis1,dis2;
dis1 = sqrt(x*x+y*y);
dis2 = sqrt(p.x*p.x+p.y*p.y);
return (dis1>dis2);
}
istream& operator>>(istream& in, point &p)
{
in>>p.x>>p.y;
return in;
}
ostream& operator<<(ostream& out,point &p)
{
out<<p.x<<" "<<p.y<<endl;
return out;
}
int main()
{
vector<point> elements;
point key,ele;
int i,j,n;
//cout<<"Enter number of elements";
cin>>n;
for(i=0;i<n;i++)
{
cin>>ele;
elements.push_back(ele);
}
for(j=1;j<n;j++)
{
key = elements[j];
i = j-1;
while((i>=0)&&(elements[i]>key))
{
elements[i+1] = elements[i];
i = i-1;
}
elements[i+1] = key;
}
for(i=0;i<n;i++)
{
cout<<elements[i]<<" ";
}
}
ASCENDING ORDER NUMBER GENERATION USING
PYTHON:
For n=50
n=50
fp = open("ascending_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(1,n):
fp.write(str(i)+" ")
fp.write(str(n))
fp.close()
OUTPUT:
For n=5000
n=5000
fp = open("ascending_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(1,n):
fp.write(str(i)+" ")
fp.write(str(n))
fp.close()
DESCENDING ORDER NUMBER GENERATION USING
PYTHON:
For n=50
n=50
fp = open("descending_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(n,1,-1):
fp.write(str(i)+" ")
fp.write(str(1))
fp.close()

OUTPUT:
For n=5000
n=5000
fp = open("descending_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(n,1,-1):
fp.write(str(i)+" ")
fp.write(str(1))
fp.close()
OUTPUT:

RANDOM ORDER NUMBER GENERATION USING


PYTHON:
For n=50
import random
n = 50
fp = open("random_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(1,n):
x = random.random()
fp.write(str(int(x*n))+" ")
x = random.random()
fp.write(str(int(x*n))+" ")
fp.close()

OUTPUT:
For n=5000
import random
n = 5000
fp = open("random_"+str(n)+".txt","w")
fp.write(str(n)+" ")
for i in range(1,n):
x = random.random()
fp.write(str(int(x*n))+" ")
x = random.random()
fp.write(str(int(x*n))+" ")
fp.close()
OUTPUT:
The analysis focussed on varying input sizes where we calculated
the time complexity for three distinct scenarios: Furthermore we
represented the relationship between time complexity and the
input size (n) by plotting graphs for all the 3 cases.

We have learnt about insertion sort and implemented insertion


sort in c++ language for nos in ascending,descending and nos in
random order.

You might also like