Question

In: Computer Science

CAN YOU PLEASE ANSWER THESE two QUESTION I don't need anymore...... Y u guys so late.....

CAN YOU PLEASE ANSWER THESE two QUESTION

I don't need anymore...... Y u guys so late.. im so mad

1. Consider the following two methods:

public static boolean isTrue(int n){

if (n <= 1)

         return false;

       for (int i = 2; i < n; i++){

         if (n % i == 0)

            return false;

}

       return true;

}

public static int Method(int[] numbers, int startIndex) {

if(startIndex >= numbers.length)

return 0;

if (isTrue(numbers[startIndex]))

     return 1 + Method(numbers, startIndex + 1);

else

     return Method(numbers, startIndex + 1);

}

What is the final return value of Method() if it is called with the following parameters: numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13}, startIndex = 0

2.

Following is the algorithm of Quicksort for sorting an array of integers in ascending order.

Partition(numbers, lowIndex, highIndex) {

   midpoint = lowIndex + (highIndex - lowIndex) / 2

   pivot = numbers[midpoint]

   done = false

   while (!done) {

      while (numbers[lowIndex] < pivot)

         lowIndex++

      while (pivot < numbers[highIndex])

         highIndex--

      if (lowIndex >= highIndex) {

         done = true

      }

      else {

         temp = numbers[lowIndex]

         numbers[lowIndex] = numbers[highIndex]

         numbers[highIndex] = temp       

         lowIndex++

         highIndex--

      }

   }

   return highIndex

}

Quicksort(numbers, lowIndex, highIndex) {

   if (lowIndex >= highIndex)

      return

   lowEndIndex = Partition(numbers, lowIndex, highIndex)

   Quicksort(numbers, lowIndex, lowEndIndex)

   Quicksort(numbers, lowEndIndex + 1, highIndex)

}

(i) Re-write the code of Quicksort so that it sorts the array of integers in descending order. Writing the specific lines of code which should be changed is sufficient.

(ii) What is the worst-case runtime of this Quicksort algorithm?

Solutions

Expert Solution

1.  

Code

// This is the code of Counting the prime numbers in an array.

public class dd {

   public static void main(String[] args) {
      
       int[] arr= {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
      
       System.out.println(Method(arr,0));
      
   }
  
   public static boolean isTrue(int n){

       if (n <= 1)
return false;

       for (int i = 2; i < n; i++){

       if (n % i == 0)

       return false;
}
return true;
}

       public static int Method(int[] numbers, int startIndex) {

       if(startIndex >= numbers.length)

       return 0;

       if (isTrue(numbers[startIndex]))

       return 1 + Method(numbers, startIndex + 1);

       else

       return Method(numbers, startIndex + 1);

       }
}

// This is the code of Counting the prime numbers in an array.

Output : 6   

Explanation: main method calls  Method(int[] numbers, int startIndex) , it has given 2 parameters 1st is array and 2nd is integer named startindex.

Method is a recursive method which consists a base case :

if(startIndex >= numbers.length)

       return 0;

Ist of all the startindex is 0 then, if base condition fails, then further execute to the next line escaping base condition

then   again another cindition (if (isTrue(numbers[startIndex]))

( => isTrue(int num) method : this method return true if given number is prime else return false)

so at startindex 0, the number[0] is 1 so it calls isTrue Method which rerurns a false value, so (Istrue for(number[0])) fails so it execute in else condition which recursivey calls the same function without adding 1, and increase startindex by 1.

Now startindex is 1 and and number[startindex] is 2. and Istrue(2) will return true because 2 is prime number ,

so it recursivey call method by adding 1 and increasing startindex by 1.

it will executes untill base condition passes if it passes it will return the count of prime numbers in given array.

prime numbers in {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13} are 2, 3, 5, 7, 11, 13   and their count is 6

so method will return 6 for this array and startindex 0.

SAMPLE OUTPUT:

2: DESCENDING ORDER USING QUICK SORT:

code:

public class Sort {

   public static void main(String[] args) {
      
       int[] arr= {4,2,6,4,7,9,22,12,4,6,8,10,11};
      
       System.out.print("Unsorted Array : ");
      
       for(int i=0;i<arr.length;i++) {
           System.out.print(arr[i]+" ");
       }
      
       System.out.println();
       System.out.println();
      
       sorting(arr,0,arr.length-1);
      
System.out.print("Sorted Array in Descending Order : ");
      
       for(int i=0;i<arr.length;i++) {
           System.out.print(arr[i]+" ");
       }
      
      
   }
  
  
       public static int partition(int arr[], int left, int right){
       int pivot = arr[left];
       int i = left;
       for(int j = left + 1; j <= right; j++){
       if (arr[j] > pivot){
       i = i + 1;
       int temp = arr[i];
       arr[i]= arr[j];
       arr[j]= temp;
       }
       }

       int temp = arr[i];
       arr[i] = arr[left];
       arr[left] = temp;

       return i;

       }

       public static void sorting(int arr[], int left, int right){
       if(left < right)
       {
       int q = partition(arr, left, right);
       sorting(arr, left, q);
       sorting(arr, q + 1, right);
       }
       }
}

SAMPLE OUTPUT:

Wrost time complexity of Quick sort is : O(n2).

Average time complexity of Quick sort is : O(nlogn).

// PLEASE THUMBS-UP AND RATE POSITIVELY

If you have any doubt regarding this question please ask me in comments

// THANK YOU:-)


Related Solutions

Can you guys answer this question for me please, need it asap!!! Please explain the difference...
Can you guys answer this question for me please, need it asap!!! Please explain the difference between systematic and unsystematic risk. In doing so, provide 4 examples of each type of risk. ( around ~200 words)
please i need unique answer , don't copy and paste ,, don't use handwriting.. can you...
please i need unique answer , don't copy and paste ,, don't use handwriting.. can you complete my answer , i need you answer b only Question: 3- Al Yamamah Steel Industries Co. uses the step method for allocating the costs of its service departments to operating departments. The company has two support departments (Human Resource and Information Technology) and two operating departments (Hot Rolled Hollow Steel and Cold Rolled Hollow Steel). Al Yamamah Steel Industries Co. decided to allocate...
#2 I post this twice, please don't duplicate answer I need two different views. If you...
#2 I post this twice, please don't duplicate answer I need two different views. If you upload a photo please make sure it is clear handwriting, Thanks Discussion: Skin Color is an Illusion What to do: Please watch the TED Talk Skin Color is an Illusion Links: https://www.ted.com/talks/nina_jablonski_breaks_the_illusion_of_skin_color. and also review the site Understanding Race Links:http://understandingrace.org/home.html. from the American Anthropological Association and post what you thought was the most interesting thing that you learned.
I post this twice, please don't duplicate answer I need two different views. If you upload...
I post this twice, please don't duplicate answer I need two different views. If you upload a photo please make sure it is clear handwriting, Thanks Discussion: Skin Color is an Illusion What to do: Please watch the TED Talk Skin Color is an Illusion Links: https://www.ted.com/talks/nina_jablonski_breaks_the_illusion_of_skin_color. and also review the site Understanding Race Links:http://understandingrace.org/home.html. from the American Anthropological Association and post what you thought was the most interesting thing that you learned.
********I NEED THE BELL SHAPED CURVE, PLEASE DON'T ANSWER IF YOU CAN'T INCLUDE*********** ********I NEED THE...
********I NEED THE BELL SHAPED CURVE, PLEASE DON'T ANSWER IF YOU CAN'T INCLUDE*********** ********I NEED THE BELL SHAPED CURVE, PLEASE DON'T ANSWER IF YOU CAN'T INCLUDE*********** ********I NEED THE BELL SHAPED CURVE, PLEASE DON'T ANSWER IF YOU CAN'T INCLUDE*********** ********I NEED THE BELL SHAPED CURVE, PLEASE DON'T ANSWER IF YOU CAN'T INCLUDE*********** According to the Organization for Economic Co-Operation and Development (OECD), adults in the United States worked an average of 1,805 hours in 2007. Assume the population standard deviation...
YOU GUYS PROVIDE ME THE WRONG ANSWER SO I NEED THE CORRECT ANS Check my work...
YOU GUYS PROVIDE ME THE WRONG ANSWER SO I NEED THE CORRECT ANS Check my work Check My Work button is now enabledItem 4 Item 4 30 points A private not-for-profit entity is working to create a cure for a deadly disease. The charity starts the year with cash of $775,000. Of this amount, unrestricted net assets total $425,000, temporarily restricted net assets total $225,000, and permanently restricted net assets total $125,000. Within the temporarily restricted net assets, the entity...
Hi guys, ASAP please answer the question thank you so much!! A cyclist travelling at 8.0...
Hi guys, ASAP please answer the question thank you so much!! A cyclist travelling at 8.0 m/s is rounding a corner of radius 20 m on a flat road by leaning into the curve at an angle x with respect to the vertical. the coefficient of static friction between the tyres and the road is 0.80 and the rims and tyres are of negligible mass in comparison to the mass of the cycle and rider. 1. draw a diagram explicitly...
Please answer all If you can't answer all then please don't answer just one question. i...
Please answer all If you can't answer all then please don't answer just one question. i need all 2. You would like to buy a house in in 16 years and estimate that you will need a deposit of $73,014. You plan to make bi-weekly deposits into an account that you hope will earn 7.05%. How much do you have to deposit every two weeks? 3. You have accumulated $1,085.55 in debt by buying things on Amazon during quarantine.  The minimum...
I need a full answer ...not a part.. please and you don't have to explain just...
I need a full answer ...not a part.. please and you don't have to explain just give me a answer plesase.. question 1 . Match the following, each choice is used once. 1. Levering System a. long or irregular bones b. synovial joint c. how things move d. muscle power e .object to be moved 2. Pivot (Fulcrum) a. long or irregular bones b. synovial joint c. how things move d. muscle power e .object to be moved 3. Effort...
( i need Unique answer, don't copy and paste, please) (dont' use handwriting, please). (i need...
( i need Unique answer, don't copy and paste, please) (dont' use handwriting, please). (i need references URL Link) General Question ** How to perform logistic regression in SPSS?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT