Hw1-1 Answers Hand Written
Hw1-1 Answers Hand Written
Hw1-1 Answers Hand Written
(Fall 2021)
(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.*;
for(int i=1;i<len;i++) {
int key=arr[i];
int j=i-1;
}
arr[j+1]=key;
}
}
for (int j = 0; j < rightArr.length; j++) {
rightArr[j]=arr[mid+1+j];
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++;
}
}
if(left<right) {
long issTime=System.nanoTime();
//sortArray.mergeSort(arr, 0, arr.length-1);
sortArray.insertionSort(iArr);
long iseTime=System.nanoTime();
}
}
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
oJ
(2) Problem 2-1 on Page 39 of the CLRS textbook. (“2-1 Insertion sort on small
arrays in merge sort”)
Answer:
a_. As
b. To
be 0
_ 0 Gn
Page :
Date : //
· O
C·
C. I •
. ·. 0
0
J -~
Page:
Date : '_ - ,-
l.
l. . ' I
I
'. .
' I )