In: Computer Science
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?
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:-)