Question

In: Computer Science

Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format...

Instructions:

Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format file (.rtf), Word document (.doc), or ASCII text file (.txt). For problem 6 submit an excel sheet containing your chart.

4. (40 points) Determine the number of statement executions (precise big-Oh) for each of the following sample code, as described in the lecture. Your answers should be in the form of a Big-Oh polynomial (e.g., O(3N2 + 7N + 6)).

Sample #1:

for (int i = 0; i < n; i++)

{

   sum += i;

}

int j = 0;

while (j < n)

{

    sum--;

    j++;

}                        

*************************************************************

Sample #2:

int sumi=0;

int sumj;

int sumk;

for (int i=0; i< n; i++)

{

   sumi++;

   for (int j =0; j< i; j++)

{

sumj++;

      for (int k=0; k<j; k++)

sumk++

}

}

*****************************************************

Sample #3

int sum=0;

for (int i=0; i<n; i++)   

if (i % 2 =0)

sum++;    //% is the modulo operation

*****************************************************

Sample #4:

for (int i = 0; i < n; i++)

{

   sum += i;  

}

for (int j = 0; j < n; j++)

{

    for (int k = 0; k < n; k++)

   {

    sum--;

   }

}                             

***************************************************************

Sample #5:

for (int i = 0; i < n; i++)

{

for (int j = 0; j < n; j++)

{

      for (int k = j; k < n; k++)

      {

        sum += j;         

      }

   }                   

}

************************************************************************

   

5. (16 points) determine the order of magnitude for method 1 implemented in java as below. This method sorts an array of integers in a descending order. Unlike the previous question, you do not need to count the total number of statement executions to come up with a precise big-Oh; instead, you can use the shortcut rules covered in the lecture for computing the big-Oh. Notice that method 1 includes a statement that calls method 2.

     public static void method1(int[] array, int n)

      {

          for (int index=0; index<n ; index++ )

          {     

                 int mark = method2(array, index, n-1);

                 int temp= array[index];

                 array[index] = array[mark];

                 array[mark] = temp;

           }

       }

      

       public static int method2(int[]array, int first, int last)

       {

              int max= array[first];

              int indexOfMax= first;

              for (int index=first+1; index<=last; index++)

                     if(array[index]>max)

                     {

                           max= array[index];

                           indexOfMax = index;

                     }

              return indexOfMax;

       }

Solutions

Expert Solution

4.

Sample #1

The first for loop runs n times, and so the line of the for loop runs n+1 times. The line int j = 0; runs 1 time. The line for the while loop runs n+1 times and the next two lines run n times each. So the number of statement executions is O(5n+3) times.

Sample #2

The first three lines run once each. The for loop line runs n+1 times. The line sumi++; runs n times. For each value of i, the second for loop iterates i times, and the line of the for loop runs (i+1) times. So it runs (1+2+...+(n+1)) = (n+1)(n+2)/2 times. The line sumj++; gets executed (1+2+...+n) = n(n+1)/2 times. The last for loop line runs (n(n+1)/2)(n+1) times and the last line runs (n(n+1)/2)n times. So the number of statement execution is O((2n³+5n²+9n+8)/2).

Sample #3

The first line gets executed once. The second line gets executed n+1 times. The if statement gets checked n times. The last line gets executed everytime i is even. So it is executed n/2 times. So the number of statement executions is. O((5n/2)+2) times.

Sample #4

The first line gets executed n+1 times. The next line is executed n times. The line for the second for loop gets executed n+1 times. The third for loop line is executed n(n+1) times. The line  sum--; is executed n² times. So the number of statement executions is O(2n²+4n+2) times.

Sample #5

The first for loop line runs n+1 times. The second for loop line runs n(n+1) times. The third for loop line runs n²(n+1) times. The last line runs n³ times. So the number of statement executions is O(n³+2n²+3n+1) times.


Related Solutions

Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format...
Instructions: Answer the following questions. Submit your answers to questions 1-5 as a Rich Text Format file (.rtf), Word document (.doc), or ASCII text file (.txt). For problem 6 submit an excel sheet containing your chart. 1. (12 points) State the order of magnitude for each of the following mathematical functions. (Hint: Find the dominant term and drop its coefficient) 5n2 + 105 nlogn 5n3 – 7n + 30 (n2 / logn)+ 40000n + 1000 (5n2 + 8n + 3n)...
Read the following case study and answer the questions that follow. Submit your answers to this...
Read the following case study and answer the questions that follow. Submit your answers to this Dropbox. The Case of the Coughing Housewife Jessica, a fifty-nine year old mother of four, moved from a ranch in Colorado to Los Angeles, after the death of her husband, to be closer to her oldest son and his family. She has been in Los Angeles for 18 months and has noticed that she is experiencing shortness of breath which has worsened over the...
Instructions: Read the problemn and complete all the questions included below. Submit your answers on a...
Instructions: Read the problemn and complete all the questions included below. Submit your answers on a Word (.doc) or Excel (.xls) document Check the Rubric As a consultant for Acme Engineering you have been able to establish the following parameters from their Financial Statements: Item Amount Cash $200,000 Securities $90,000 Accounts Receivable $300,000 Inventories $400,000 Prepaid Expenses $16,000 Accounts Payable $630,000 Other Liabilities $180,000 Calculate the following parameters: Total Assets Total Liabilities Working Capital Current Ratio Acid Test Ratio
Read the article assigned for this week’s reading and answer the following questions. Submit your answers...
Read the article assigned for this week’s reading and answer the following questions. Submit your answers in an APA format summary. a. What is the definition of a medication error? b. What are "high-alert" medications? c. What abbreviations are dangerous? Are these evidence based? d. What drug names are frequently confused? e. How should tall man lettering be applied to differentiate look-alike/soundalike drug names?
Review the following scenario, and answer the questions. Create a Word document for your answers, submit...
Review the following scenario, and answer the questions. Create a Word document for your answers, submit via submission link. A 28-year-old primigravida at 41 weeks’ gestation is admitted to the L&D unit for early labor at 2 cm, 70% effaced, and 0 station. How can the nurse best describe to this patient the latent phase of labor? How will the cardinal movements of labor facilitate the birth of the fetus?
Answer the following questions and upload to Canvas. Submit in Word or PDF format.  Show your work...
Answer the following questions and upload to Canvas. Submit in Word or PDF format.  Show your work and upload the Excel sheet as well. All the writing parts must be your original writing, don't quote, write in your own words. The following table presents the orders of Samson Company for the last 36 months (3 years). Month Order Year 1 Order Year 2 Order Year 3 January 502 614 712 February 408 592 698 March 491 584 686 April 456 532...
Instructions: Answer any ONE of the following essay questions. Your response should be in essay format....
Instructions: Answer any ONE of the following essay questions. Your response should be in essay format. Write as much as possible telling me who, what, where, when, and why. Use complete sentences and multiple paragraphs; 3-5-7 total. Your response is worth up to 20 points. Discuss American foreign policy in the 1970s under President Richard Nixon. What was his strategy for the Cold War? Did he do things different or were his efforts similar to his predecessors? Was he successful,...
Answer the following questions and submit answers in Microsoft Word. Be sure to fully answer each...
Answer the following questions and submit answers in Microsoft Word. Be sure to fully answer each question. 1. Does a precedent system operate in your social group, at work or in making your personal decisions? Explain. 2. Burglar Bob breaks into Vince Victim’s house. Bob steals a flat screen television and laptop and does a significant amount of damage to the property before he leaves. Fortunately, Vince has a state-of-the-art security system. It captures excellent images of Bob, who is...
Please just submit text answers for questions a) b) and c) a) Use the “ls -lt...
Please just submit text answers for questions a) b) and c) a) Use the “ls -lt filesize2.c” command to see what the size is reported by OS for this file. In the file “filesize2.c”, the following code segment was originally commented out: Uncomment this block of code. /* fseek(fd, 10L, SEEK_SET); putc(-1, fd); rewind(fd); */ //try uncomment this block, to see what it does. b) Think about what this code segment does. c) Then compile and run the code with...
Answer the following questions and submit as a PDF on Webcourses. The assignment is worth 5%...
Answer the following questions and submit as a PDF on Webcourses. The assignment is worth 5% of your grade . For the TCP/IP model, describe 2 types of vulnerabilities commonly attacked for each layer. (40 points) Can two network interfaces have the same MAC address? Why or why not? Also, can two network interfaces have the same IP address? Why or why not? (10 points) Most modern TCP implementations use pseudo-random number generators (PRNG) to determine starting sequence numbers for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT