You are on page 1of 11

ASSIGNMENT-2

Name: Sandip Mourya


Enrollment No:190180107035
Subject: Analysis and Design of Algorithms (3150703)
Department: Computer Engineering
Question:

Implement Insertion Sort Algorithm in your programming language.


The program should read file to initialize the array.
Use the variable "cost" and do cost++ every time a line in algorithm is executed.

for(i=1;i<count; I++, cost++){ temp=number[i];cost++; j=i-


1;cost++; while((temp<number[j])&&(j>=0)){ number[j+1]=number[j];cost++;
j=j-1;cost++;cost++; } number[j+1]=temp;cost++;
}
Task1:
Do not consider cost of reading from and writing to files.
Print the cost value after you sort random number files of 10,100,1000,1000....so on..... try
the limit of your machine. Sort as many numbers you can.
Writing is a time consuming operation. Even after sorting, writing many numbers will take
time. (Some milli seconds - few seconds).

Task 2:
Do the same Task 1 but this time do not consider cost variable, but use start time and end
time values to see how much time is spend for different size of array and different size of
number.

Task 3:
Making graphs. Plot the values on graph using excel sheet.

Hint: while sorting large array of numbers, you can check your processors performance
from task manager. Take a screen shot if anything unusual.

Program:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class InsertionSort {

static int[] read(int size,String file) {


int i=0;
int num[] = new int[size];
try {
Scanner txt = new Scanner(new File(file));
while(txt.hasNextInt()) {
num[i++] = txt.nextInt();
}
txt.close();
}catch(Exception e) {
e.printStackTrace();
}
return num;
}

static void write(int[] num,String file) {


try {
FileWriter wr = new FileWriter(file);
for(int i=0;i<num.length;i++) {
wr.write(num[i]+"\n");
}
wr.close();
}catch(Exception e) {
e.printStackTrace();
}
}

static void sort(int arr[])


{
long cost = 0;
int n = arr.length;
for (int i = 1; i < n; ++i,cost++) {
int key = arr[i]; cost++;
int j = i - 1; cost++;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j]; cost++;
j = j - 1; cost++;
cost++;
}
arr[j + 1] = key; cost++;
}
System.out.println("Cost = " +cost);
}

public static void main(String args[]) throws IOException


{
int size;
String file;
long start,end;

Scanner input = new Scanner(System.in);


System.out.println("Enter No of elements");
size=input.nextInt();

System.out.println("Enter filename");
file=input.next();
input.close();

int num[] = read(size,file);

start = System.currentTimeMillis();
sort(num);
end = System.currentTimeMillis();
System.out.print("Sorting TIme"+(end-start)+"ms");

write(num,file);

}
}

Output of 6 Digit Random Files:


Output Sorted File:

Time Analysis of Insertion Sort:


Computer Resources performance after unresponsive output of 10000000 Input
Numbers

You might also like