notes26

notes26 - Example: Asymptotis Analysis for bubble sort void...

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

View Full Document Right Arrow Icon
file:///H|/pu.data/Desktop/0114.html Example: Using Asymptotis Analysis in obtaining the maximum value in an array of numbers #include <assert.h> int maxVal (int array[], int len){ // test precondition assert(len > 0); // time: 1*c1 int max = array[0]; // time: 1*c2 for(int i = 1; i <len; i++) { // time: 1*c3 + len*c4 if(array[i] > max){ // time: len*c5 max = array[i]; // time: len*c6 (worst case) } } return max; } Step 1: Count number of operation and time Total time(len) = c1 + c2 + c3 + len*c4 + len*c5 + len*c6 + c7 Lets make len = n because we usually use n as the size of the input. Totla time (n) = (c1 + c2 + c3 + c7) + n*(c4 + c5 + c6) Step2: Obtain Bid Oh notation by eliminating constants and only considering terms that are important as n -> Totla time(n) = (c1 + c2 + c3+ c7) + n*(c4 + c5 + c6) = O((c4 + c5 + c6)*n) = O(n) (grows linearly) Obtaining the maximum of an array takes time O(n), that is linear with the size of the input. --
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Example: Asymptotis Analysis for bubble sort void bubbleSort (int array, int n){ for( int i = 0; i &lt; n-1; i++){ // time: c1 + c2*(n-1) for( int j = 0; j &lt; n-1; j++){ // time: c3 + c4*((n-1)+(n-2)+ +1) (worst case) if(array[j] &gt; array[j+1]){ int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; file:///H|/pu.data/Desktop/0114.html (1 of 2) [2/3/2008 1:04:43 AM] file:///H|/pu.data/Desktop/0114.html } } } } Step 1: Count number of operation and time Totla time (n) = c 1 + c 2 *(n-1) + c 3 + c 4 *((n-1)+(n-2)+ +1) Step2: Obtain Bid Oh notation by eliminating constants and only considering terms that are important as n -&gt; Totla time(n) = c 1 + c 2 *(n-1) + c 3 + c 4 *((n-1)*n /2) = c 1 + c 2 *n c 2 + c 3 + (c 4 /2)*n 2 = O(n 2 ) Bubble sort takes times proportional to the square of the size of the imput. file:///H|/pu.data/Desktop/0114.html (2 of 2) [2/3/2008 1:04:43 AM]...
View Full Document

Page1 / 2

notes26 - Example: Asymptotis Analysis for bubble sort void...

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