{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

sol3 - COP3530 Solution 3 Question 1 The worst case...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COP3530 Solution 3 Question 1: The worst case complexity of the SetSize(int size) method will be O(n) and best case complexity can be O(1). The code is as follows: /** Extension of array implementation of Linear List*/ package dataStructures; import java.util.*; import utilities.*; public class ArrayLinearListExt extends ArrayLinearList { public ArrayLinearListExt(int capacity) { super(capacity); } public void SetSize(int size) { if(size>element.length) { throw new IllegalArgumentException("The size should be smaller than the capacity of the List"); } if(this.size<size) { for(int i=this.size+1;i<=size;i++) { element[i]=null; } this.size=size; } else if(this.size>size) { for(int i=this.size‐1;i>=size;i‐‐) { element[i]=null; } this.size=size; } } public static void main(String args) { // test default constructor ArrayLinearListExt x = new ArrayLinearListExt(10); // test size System.out.println("Initial size is " + x.size()); // test isEmpty if (x.isEmpty()) System.out.println("The list is empty"); else System.out.println("The list is not empty"); // test put x.add(0, new Integer(2)); x.add(1, new Integer(6)); x.add(0, new Integer(1)); x.add(2, new Integer(4)); System.out.println("List size is " + x.size()); //test SetSize x.SetSize(5); System.out.println("List size is " + x.size()); x.SetSize(3); System.out.println("List size is " + x.size()); x.SetSize(11); System.out.println("List size is " + x.size()); } } Question 2: a) The time consumptions with different sizes are shown below: Data Set Total Time (ms) Size 50 Q 6.35*10^‐2 S 0.0149 I 0.0104 500 Q 0.107 S 1.30 I 0.771 5000 Q 1.36 S 127 I 83.0 50000 Q 18.4 S 1.29*10^4 I 9.26*10^3 (the time is depending on their machine) Q = Quick sort, S = Selection sort, I = Insertion Sort. b)(20) On the random data set, the time complexity of quick sort seems to be a little more than O(n) (looks like O(nlogn)). The time complexities of selection sort and insertion sort might be O(n^2). Explainations: Quick sort: By employing a divide and conquer algorithm, qsort rearranges and divides a unsorted array into two parts, and repeats this recurrsively. On average, these two parts have similar sizes. So qsort will last for logn rounds. Within each round, O(n) actions will be done. So its complexity is O(nlogn). Selection sort: Selection sort alwayes scans the array and makes O(n^2) comparison. Insertion sort: As stated in the previous assignment, the time complexity of insertion sort is O(n+d), where d is the number of inversions within the array. Geneally, d = O(n^2). But in some cases, d can be very small. And the insertion sort will be very fast. In all cases, in general the results agree with the theoretical expectations. There might be some deviations ‐ especially with the smallest data set ‐ that might be due to several reasons. Repeating the tests only 10 times, measuring the time of processes that run very fast ‐ too fast for the measurements to be dependable, and running the simulation for only 4‐5 different data sets might be some of the reasons. ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online