sol2 - 0 for (int i = list.length-1; i >= 1;...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3530 Solution 2 1. Runtime: 10% accuracy: 500 ms 5% accuracy: 1000 ms 1% accuracy: 5000 ms public class Test{ public static void main(String args[]){ long startTime = System.currentTimeMillis(); long counter=0; do { int array[] = {20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}; counter++; bubbleSort(array); } while (System.currentTimeMillis() -startTime < 5000); long elapsedTime = System.currentTimeMillis()- startTime; float timeForMethod = ((float) elapsedTime)/counter; System.out.print(timeForMethod); } public static void bubbleSort(int[] list){ for (int i = list.length-1; i >= 1; i--){ for (int j = 0; j < i; j++){ if (list[j] > list[j + 1]){ // swap list[j] with list[j + 1] int temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2. public static void bubbleSort(int[] list){
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 0 for (int i = list.length-1; i &gt;= 1; i--){ n for (int j = 0; j &lt; i; j++){ n if (list[j] &gt; list[j + 1]){ 1 // swap list[j] with list[j + 1] 0 int temp = list[j]; 1 list[j] = list[j + 1]; 1 list[j + 1] = temp; 1 } } } } Count: n*n*4 Worst case: n^2 3. a) Yes. As n -&gt; infinity, A actually approaches e, which is a constant. b) Yes. Any constant c &gt; 1 would work here. c) No. As for infinitely many n, cos n = 0, which makes B = 1. d) No. Let x=lg n, then A=10^x and B = x^c. e) Yes. Actually lgA=lgn*lgc=lgB, so A=B. 4. a) (4, 2) , (4, 1), (4, 3), (2, 1), (9, 3) or by index (0, 1), (0,2), (0,4), (1, 2), (3,4) b) A = (n,n-1,. ..,2,1). This array has all the possible inversions, which amounts to C(n,2) c) Linear relation, because the insertion sort is a process to detect and invert the inversions one by one....
View Full Document

This note was uploaded on 01/15/2010 for the course COP 3530 taught by Professor Davis during the Fall '08 term at University of Florida.

Page1 / 2

sol2 - 0 for (int i = list.length-1; i &amp;amp;gt;= 1;...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online