Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

determine the order of magnitude for method 1 implemented in java as below. This

ID: 667768 • Letter: D

Question

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;

   }

Explanation / Answer

The order of magnitude for method 1 includes the magnitude of method 2 so to calculate the order of magnitude for method 1 would be n2.