0% found this document useful (0 votes)
42 views16 pages

Hw1-1 Answers Hand Written

This document contains the answers to homework assignment 1 for CSE 5311 (Fall 2021) submitted by Rushikesh Mahesh Bhagat (UTA ID: 1001911486). It includes code implementing insertion sort and merge sort in Java, as well as answers to two problems: (1) plotting execution time curves comparing insertion and merge sort for increasing input sizes, and discussing asymptotic analysis; and (2) addressing problem 2-1 on page 39 of the CLRS textbook regarding insertion sort on small arrays in merge sort.

Uploaded by

Rushikesh Bhagat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views16 pages

Hw1-1 Answers Hand Written

This document contains the answers to homework assignment 1 for CSE 5311 (Fall 2021) submitted by Rushikesh Mahesh Bhagat (UTA ID: 1001911486). It includes code implementing insertion sort and merge sort in Java, as well as answers to two problems: (1) plotting execution time curves comparing insertion and merge sort for increasing input sizes, and discussing asymptotic analysis; and (2) addressing problem 2-1 on page 39 of the CLRS textbook regarding insertion sort on small arrays in merge sort.

Uploaded by

Rushikesh Bhagat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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