Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

DESIGN AND ANALYSIS OF ALGORITHMS LAB

ASSIGNMENT – 1
IPS-1 INSERTION SORT
SUBJECT: DAA Lab
COURSE CODE: BCSE204P
FACULTY NAME: Dr. Pandiyaraju V Sir
LAB SLOT: L25+L26

TOPIC: Insertion Sort

Aim
To implement 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(A)
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

Illustration
CODE
Implementation Of Insertion Sort Algorithm using STL and
without STL

INSERTION SORT VECTOR

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]<<" ";
}*/
}

PYTHON CODE FOR ASCENDING ORDER NUMBER GENERATION

CODE:

For Input value 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()
For Input value 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()

For Input value 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()
For Input value 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:

Time Complexity

For Input value n=5000


Time taken is 2.8e-05

For Input value n=10000


Time taken is 5.5e-05

For Input value n=15000


Time taken is 8.1e-05

For Input value n=20000


Time taken is 0.000108
PYTHON CODE FOR DESCENDING ORDER NUMBER
GENERATION

CODE:

For Input value 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 Input value 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 Input value 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 Input value 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 Input value n=5000
Time taken is 0.062754

For Input value n=10000


Time taken is 0.246533
For Input value n=15000
Time taken is 0.548735

For Input value n=20000


Time taken is 0.980614
PYTHON CODE FOR RANDOM ORDER NUMBER GENERATION

CODE:

For Input value 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 Input value 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 Input value 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 Input value 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:

Time Complexity

For Input value n=5000


Time taken is 0.034814

For Input value n=10000


Time taken is 0.129812

For Input value n=15000


Time taken is 0.286958

For Input value n=20000


Time taken is 0.511443
INSERTION SORT POINTS

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]<<" ";
}
}

PYTHON CODE FOR ASCENDING ORDER NUMBER GENERATION

CODE:
For Input value 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 Input value 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()
OUTPUT:

PYTHON CODE FOR DESCENDING ORDER NUMBER


GENERATION

CODE:

For Input value 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 Input value 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:
PYTHON CODE FOR RANDOM ORDER NUMBER GENERATION
CODE:
For Input value 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:

Conclusion:
In this Assignment we have learned about Insertion sort and implemented
Insertion sort in c++ language and we have done analysis on Insertion sort by
varying the input size and calculated time complexity for different input sizes
for three cases i.e., If the numbers are in Ascending order, Descending order
and Random order. We have Plotted graph for Time Complexity vs Input n for
all three cases.

You might also like