Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Hw1-1 Answers Hand Written

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

CSE 5311 Homework Assignment 1 Answers

(Fall 2021)

Name – Rushikesh Mahesh Bhagat


UTA ID – 1001911486

(1) Implement the insertion sort and merge sort algorithms with any programming
language you choose and run them with the same input number list. Generate the
list elements with a random function and increase the list size incrementally until
you find the execution time of your merge sort program is consistently shorter. Plot
the two curves in a figure (execution time vs input list size) about the two
programs. Using the example to discuss why the asymptotic analysis is
meaningful. Attach program codes in your submission.

Answer:

Execution Code:
Code written in JAVA.

import java.util.*;

public class Sorting {

public static void insertionSort(int arr[]) {


int len=arr.length;

for(int i=1;i<len;i++) {
int key=arr[i];
int j=i-1;

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


arr[j+1]=arr[j];
j=j-1;

}
arr[j+1]=key;
}

public static void merge(int arr[],int left,int mid,int right) {

int leftArr[]= new int[mid-left+1];


int rightArr[]= new int[right-mid];

for (int i = 0; i < leftArr.length; i++) {


leftArr[i]=arr[left+i];

}
for (int j = 0; j < rightArr.length; j++) {
rightArr[j]=arr[mid+1+j];

int i=0, j=0;


int k=left;

while (i<leftArr.length && j< rightArr.length) {


if (leftArr[i]<=rightArr[j]) {
arr[k]=leftArr[i];
i++;

else {
arr[k]=rightArr[j];
j++;

}
k++;

}
while (i<leftArr.length) {
arr[k]=leftArr[i];
i++;
k++;

while (j<rightArr.length) {
arr[k]=rightArr[j];
j++;
k++;

}
}

public static void mergeSort(int arr[],int left,int right) {

if(left<right) {

int mid = (left+right-1)/2;

mergeSort(arr, left, mid);


mergeSort(arr, mid+1, right);
merge(arr, left, mid, right);

public static void printArray(int arr[]) {

int len = arr.length;

for (int i = 0; i < len; i++) {


System.out.print(arr[i]+" ");
}
}

public static void main(String[] args) {


//int arr[] = {12, 14, 13, 5, 6, 7, 25};
Scanner scanner=new Scanner(System.in);
Random random=new Random();
Sorting sortArray = new Sorting();

System.out.println("\nEnter length of array");


int len=scanner.nextInt();

int mArr[]=new int[len];


int iArr[]=new int[len];

for (int i = 0; i < len; i++) {


int anyNum=random.nextInt(150);
mArr[i]=anyNum;
iArr[i]=anyNum;
}

System.out.println("Given Array for Insertion Sort");


printArray(iArr);

long issTime=System.nanoTime();
//sortArray.mergeSort(arr, 0, arr.length-1);
sortArray.insertionSort(iArr);
long iseTime=System.nanoTime();

long totalisTime= iseTime - issTime;


System.out.println("\nSorted Array");
printArray(iArr);
System.out.println("\nThe runtime was " + totalisTime);
System.out.println();

System.out.println("Given Array for Merge Sort");


printArray(mArr);
long mssTime=System.nanoTime();
//sortArray.mergeSort(arr, 0, arr.length-1);
sortArray.mergeSort(mArr, 0, mArr.length-1);
long mseTime=System.nanoTime();

long totalmsTime= mseTime - mssTime;


System.out.println("\nSorted Array");
printArray(mArr);
System.out.println("\nThe runtime was " + totalmsTime);

}
}
Output:
Note: Please check next pages for asymptotic analysis and graph plotting.
{:_ Page: j))
l0oa1e~
--.-=-:====·=-=----
:c:::===1- L ----........-:,:;..-

C
0
<..I
U)
0

<S
C

.-
0

LD

2..5 12.s tso


X
( ('\Page: Q
~ate: I 1 1J
r ::r

oJ
(2) Problem 2-1 on Page 39 of the CLRS textbook. (“2-1 Insertion sort on small
arrays in merge sort”)

Answer:

Note: For answers, please check next pages.


_Pag_e :-=-_--{,)
---d:!=============~===--====-..r_ --r- ~
Date : I ! }

a_. As

b. To

be 0

_ 0 Gn
Page :
Date : //

· O

C. I •

. ·. 0
0
J -~
Page:
Date : '_ - ,-
l.

l. . ' I
I

'. .

' I )

You might also like