Question

In: Computer Science

As code is given below, follow the instructions: 1. Count the number of comparisons and find...

As code is given below, follow the instructions:

1. Count the number of comparisons and find where to put that counter in the code

2. Pick a random pivot, right pivot, left pivot, middle pivot for each smaller array /sub-array

import java.util.*;

import java.util.List;

public class QuickSort {

public static void main(String[] args) {

int[] values = { 6, 5, 4, 3, 1, 7, 8 };

System.out.println("Original order: ");

for (int element : values)

System.out.print(element + " ");

IntQuickSorter.quickSort(values);

System.out.println("\nFirst Sorted order: ");

for (int element : values)

System.out.print(element + " "); }

public static void quickSort(int array[]) {

doQuickSort(array, 0, array.length - 1); }

private static void doQuickSort(int array[], int start, int end){

int pivotPoint;

pivotPoint = partition(array, start, end);

doQuickSort(array, start, pivotPoint - 1);

doQuickSort(array, pivotPoint + 1, end); }

private static int partition(int array[], int start, int end) {

int pivotValue = array[start];

int endOfLeftList = start;

int mid = (start + end) / 2;

int unsortedValue;

int scan;

swap(array, start, mid);

for (scan = start + 1; scan <= end; scan++) {

if (array[scan] < pivotValue) {

endOfLeftList++;

swap(array, endOfLeftList, scan); } }

swap(array, start, endOfLeftList);

  return endOfLeftList; }

private static void swap(int[] array, int a, int b) {

int temp;

temp = array[a];

array[a] = array[b];

array[b] = temp; } }



Solutions

Expert Solution

Code: (For pivot as middle element)

import java.util.*;

import java.util.List;


public class Main {
static int ctr=0; //static counter for counting comparisons
public static void main(String[] args) {

int[] values = { 6, 5, 4, 3, 1, 7, 8 };


System.out.println("Original order: ");

for (int element : values)

System.out.print(element + " ");

quickSort(values);

System.out.println("\nFirst Sorted order: ");

for (int element : values)

System.out.print(element + " ");

System.out.println("\n Total number of comparisons: "+ctr);
  
}

public static void quickSort(int array[]) {
  

doQuickSort(array, 0, array.length - 1);
  
}

private static void doQuickSort(int array[], int start, int end){

int pivotPoint;
if(start<end){

pivotPoint = partition(array, start, end);

doQuickSort(array, start, pivotPoint - 1);

doQuickSort(array, pivotPoint + 1, end);
  
}
  
}

private static int partition(int array[], int start, int end) {
int mid = (start + end) / 2;

int pivotValue = array[mid]; //pivot as middle element

int endOfLeftList = start;

int unsortedValue;

int scan;

swap(array, start, mid);

for (scan = start + 1; scan <= end; scan++) {
ctr+=1; //incrementing comparisons

if (array[scan] < pivotValue) {

endOfLeftList++;

swap(array, endOfLeftList, scan); } }

swap(array, start, endOfLeftList);

return endOfLeftList; }

private static void swap(int[] array, int a, int b) {

int temp;

temp = array[a];

array[a] = array[b];

array[b] = temp; } }

Output:

Code: (For pivot as starting element)

import java.util.*;

import java.util.List;


public class Main {
static int ctr=0; //static counter for counting comparisons
public static void main(String[] args) {

int[] values = { 6, 5, 4, 3, 1, 7, 8 };


System.out.println("Original order: ");

for (int element : values)

System.out.print(element + " ");

quickSort(values);

System.out.println("\nFirst Sorted order: ");

for (int element : values)

System.out.print(element + " ");

System.out.println("\n Total number of comparisons: "+ctr);
  
}

public static void quickSort(int array[]) {
  

doQuickSort(array, 0, array.length - 1);
  
}

private static void doQuickSort(int array[], int start, int end){

int pivotPoint;
if(start<end){

pivotPoint = partition(array, start, end);

doQuickSort(array, start, pivotPoint - 1);

doQuickSort(array, pivotPoint + 1, end);
  
}
  
}

private static int partition(int array[], int start, int end) {
int mid = (start + end) / 2;

int pivotValue = array[start]; //pivot a starting element

int endOfLeftList = start;

int unsortedValue;

int scan;

swap(array, start, mid);

for (scan = start + 1; scan <= end; scan++) {
ctr+=1; //incrementing comparisons

if (array[scan] < pivotValue) {

endOfLeftList++;

swap(array, endOfLeftList, scan); } }

swap(array, start, endOfLeftList);

return endOfLeftList; }

private static void swap(int[] array, int a, int b) {

int temp;

temp = array[a];

array[a] = array[b];

array[b] = temp; } }

Output:

Code: (For pivot as ending element)

import java.util.*;

import java.util.List;


public class Main {
static int ctr=0; //static counter for counting comparisons
public static void main(String[] args) {

int[] values = { 6, 5, 4, 3, 1, 7, 8 };


System.out.println("Original order: ");

for (int element : values)

System.out.print(element + " ");

quickSort(values);

System.out.println("\nFirst Sorted order: ");

for (int element : values)

System.out.print(element + " ");

System.out.println("\n Total number of comparisons: "+ctr);
  
}

public static void quickSort(int array[]) {
  

doQuickSort(array, 0, array.length - 1);
  
}

private static void doQuickSort(int array[], int start, int end){

int pivotPoint;
if(start<end){

pivotPoint = partition(array, start, end);

doQuickSort(array, start, pivotPoint - 1);

doQuickSort(array, pivotPoint + 1, end);
  
}
  
}

private static int partition(int array[], int start, int end) {
int mid = (start + end) / 2;

int pivotValue = array[end]; //pivot a starting element

int endOfLeftList = start;

int unsortedValue;

int scan;

swap(array, start, mid);

for (scan = start + 1; scan <= end; scan++) {
ctr+=1; //incrementing comparisons

if (array[scan] < pivotValue) {

endOfLeftList++;

swap(array, endOfLeftList, scan); } }

swap(array, start, endOfLeftList);

return endOfLeftList; }

private static void swap(int[] array, int a, int b) {

int temp;

temp = array[a];

array[a] = array[b];

array[b] = temp; } }

Output:


Related Solutions

In JAVA!!! write a method called Comparisons that will return the number of comparisons to find...
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code:...
write a method called Comparisons that will return the number of comparisons to find an element...
write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code: int totalComparisons=0;...
                           Apps and Privacy - An Ethics Case Study Now follow the instructions given below to...
                           Apps and Privacy - An Ethics Case Study Now follow the instructions given below to complete the assessment task: Use the title of the article/case study as the title of your argument visualisation so that the lecturer knows which article you are analysing. Undertake further research about your chosen case and the ethical issue involved, to assist you in analysing it in your argument visualisation Identify some logical arguments based on the four classical ethical theories including utilitarianism, deontology,...
The code is bellow. follow this instructions to edit the code : lets assume that instead...
The code is bellow. follow this instructions to edit the code : lets assume that instead of the maximum and minimum values, you want to find the 2nd largest/smallest. For example, in the numbers 1 to 10, this would return 2 and 9 .you will have to change the "max_min" function accordingly. this has a different puepose than "max_min", so you must give it another name. code: #include <stdio.h> #define N 235 void max_min (int a[], int n, int *max,...
Please use Minitab and follow instructions given with the problem A contractor has recorded the number...
Please use Minitab and follow instructions given with the problem A contractor has recorded the number of jobs performed in the last seven months. Last year’s performance evaluation score For each of the following methods, develop a forecast for the number of jobs for the 8th month. A three-period moving average Exponential smoothing (use Minitab default smoothing parameters) A linear trend equation Using the mean squared difference (MSD), explain which of the methods you believe results in the best forecast...
Write the basic JAVA code for the beginners and follow all the instructions listed 1. A...
Write the basic JAVA code for the beginners and follow all the instructions listed 1. A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for e.g., 1980), except that it is not a leap year if it is also divisible by 100 (for e.g., 1900); however, it is a leap year if it is further divisible by 400 (for e.g., 2000). Write a program that prompts the...
Use the data below for this problem, follow instructions to find answers. 1.64 1.55 1.83 1.94...
Use the data below for this problem, follow instructions to find answers. 1.64 1.55 1.83 1.94 1.86 1.56 1.56 1.96 1.88 1.92 1.67 1.97 1.69 1.71 1.58 1.99 1.77 1.70 1.84 2.10 1.69 1.59 1.67 1.97 1.58 1.58 1.79 1.88 1.58 1.51 1.61 1.91 1.70 1.68 1.91 2.21 1.71 1.63 1.68 2.01 1.68 1.59 1.76 1.99 1.64 1.83 1.64 1.94 1.68 1.76 1.67 1.98 1.65 1.95 1.76 2.35 1.47 1.61 1.61 1.91 1.80 1.73 1.59 2.10 1.75 1.70 1.65 2.10...
Use the data below for this problem, follow instructions to find answers: 1.64 1.55 1.83 1.94...
Use the data below for this problem, follow instructions to find answers: 1.64 1.55 1.83 1.94 1.86 1.56 1.56 1.96 1.88 1.92 1.67 1.97 1.69 1.71 1.58 1.99 1.77 1.70 1.84 2.10 1.69 1.59 1.67 1.97 1.58 1.58 1.79 1.88 1.58 1.51 1.61 1.91 1.70 1.68 1.91 2.21 1.71 1.63 1.68 2.01 1.68 1.59 1.76 1.99 1.64 1.83 1.64 1.94 1.68 1.76 1.67 1.98 1.65 1.95 1.76 2.35 1.47 1.61 1.61 1.91 1.80 1.73 1.59 2.10 1.75 1.70 1.65 2.10...
Consider the following code segment:    count = 1    while count <= 10:       print(count,...
Consider the following code segment:    count = 1    while count <= 10:       print(count, end=" ") Which of the following describes the error in this code? The loop control variable is not properly initialized. The comparison points the wrong way. The loop is infinite. The loop is off by 1. Does this code contain an error? If so, what line is the error on? 0: ans = input("Yes/No? ") 1: if ans == "Yes": 2: print("Confirmed!") 3: else...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential...
IN C++ Write a program to find the number of comparisons using binarySearch and the sequential search algorithm as follows: Suppose list is an array of 1000 elements. 3 Search list for some items as follows: a. Use the binary search algorithm to search the list. (You may need to modify the algorithm given in this chapter to count the number of comparisons.) b. Use the binary search algorithm to search the list, switching to a sequentialsearch when the size...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT