Question

In: Computer Science

Assume the following list of keys for problems 17, 58, 80, 22, 45, 48, 95, 5,...

Assume the following list of keys for problems

17, 58, 80, 22, 45, 48, 95, 5, 100, 38, 36, 11, 27

Problem 4:

(2 points) The above list is to be sorted using the quick sort algorithm as discussed in this chapter. Use pivot as the middle element of the list.

  1. What is the pivot?
  2. Give the resulting list after one call to the partition function

Problem 5:

(2.5 points) Assume the following list of keys:

78, 40, 16, 82, 64, 67, 57, 40, 37, 47, 72, 14, 17, 27, 55

This list is to be sorted using the quick sort algorithm as discussed in this chapter. Use pivot as the median of the first, last, and middle elements of the list.

  1. What is the pivot?
  2. Give the resulting list after one call to the partition function.

JAVA

Solutions

Expert Solution

As the ques we want to sort is in JAVA so we have to save the name of the file same as the class name which contains the main class, so we put our function in the QuickSort class

The pivot or pivot element is the element of a matrix, or an array, which is selected first by an algorithm, to do certain calculations. In the case of matrix algorithms, a pivot entry is usually required to be at least distinct from zero, and often distant from it; in this case finding this element is called pivoting.

1)  First Example

public class QuickSort {
        public static void main(String[] args) {
                int[] x = { 17, 58, 80, 22, 45, 48, 95, 5, 100, 38, 36, 11, 27 };
                System.out.println(Arrays.toString(x));

                int low = 0;
                int high = x.length - 1;
 
                quickSort(x, low, high);
                System.out.println(Arrays.toString(x));
        }
 
        public static void quickSort(int[] arr, int low, int high) {
                if (arr == null || arr.length == 0)
                        return;
 
                if (low >= high)
                        return;
 
                // pick the pivot
        // In this we took the middle element as the middle element
                int middle = low + (high - low) / 2;
                int pivot = arr[middle];
 
                // make left < pivot and right > pivot
                int i = low, j = high;
                while (i <= j) {
                        while (arr[i] < pivot) {
                                i++;
                        }
 
                        while (arr[j] > pivot) {
                                j--;
                        }
 
                        if (i <= j) {
                                int temp = arr[i];
                                arr[i] = arr[j];
                                arr[j] = temp;
                                i++;
                                j--;
                        }
                }
 
                // recursively sort two sub parts
                if (low < j)
                        quickSort(arr, low, j);
 
                if (high > i)
                        quickSort(arr, i, high);
        }
}

2) Second example

public class QuickSort {
        public static void main(String[] args) {
                int[] x = { 78, 40, 16, 82, 64, 67, 57, 40, 37, 47, 72, 14, 17, 27, 55 };
                System.out.println(Arrays.toString(x));
 
                int low = 0;
                int high = x.length - 1;
 
                quickSort(x, low, high);
                System.out.println(Arrays.toString(x));
        }
 
        public static void quickSort(int[] arr, int low, int high) {
                if (arr == null || arr.length == 0)
                        return;
 
                if (low >= high)
                        return;
 
                // pick the pivot
        // In this we took the middle element as the middle element 
                int middle = low + (high - low) / 2;
                int pivot = arr[middle];
 
                // make left < pivot and right > pivot
                int i = low, j = high;
                while (i <= j) {
                        while (arr[i] < pivot) {
                                i++;
                        }
                        while (arr[j] > pivot) {
                                j--;
                        }
                        if (i <= j) {
                                int temp = arr[i];
                                arr[i] = arr[j];
                                arr[j] = temp;
                                i++;
                                j--;
                        }
                }
                // recursively sort two sub parts
                if (low < j)
                        quickSort(arr, low, j);
                if (high > i)
                        quickSort(arr, i, high);
        }
}

This is the example of quick sort. By this we can sort the two list very easily.


Related Solutions

79, 19, 16, 64, 28, 28, 31,90, 60, 56, 31, 56, 22, 18, 45, 48, 17,...
79, 19, 16, 64, 28, 28, 31,90, 60, 56, 31, 56, 22, 18, 45, 48, 17, 63, 69, 17 with these number find a) Frequency distribution b)Frequency polygon c) less than cumulative frequency polgon.
22 17 19 10 30 10.5 31 11 32 15 45 6 48 11 49 5.2...
22 17 19 10 30 10.5 31 11 32 15 45 6 48 11 49 5.2 68 3 65 4.2 a) Create a scatterplot of this data and comment on the relationship between the variables b) Calculate the correlation coefficient between these two variables c) Does a linear relationship exist? d) Calculate the least squares regression equation e) Using your equation, predict how long we would expect a 43 year old new hire to remain with the company
Consider the following two variables: x y 45 10 23 30 17 48 19 45 41...
Consider the following two variables: x y 45 10 23 30 17 48 19 45 41 34 13 27 39 26 37 31 24 38 12 44 What is the correlation between these two variables? Use Pearson's r, and take your answer to two decimal places. To what two-tailed critical value of Pearson's r would you compare this? Use the provided tables, assume alpha, = 0.05, and express your answer as an absolute value. Round to two decimal places. If...
Consider the following set of data. (17, 8), (27, 55), (64, 31), (83, 22), (115, 58),...
Consider the following set of data. (17, 8), (27, 55), (64, 31), (83, 22), (115, 58), (119, 6) (a) Calculate the covariance of the set of data. (Give your answer correct to two decimal places.) (b) Calculate the standard deviation of the six x-values and the standard deviation of the six y-values. (Give your answers correct to three decimal places.) sx = sy = (c) Calculate r, the coefficient of linear correlation, for the data in part (a). (Give your...
42, 31, 48, 12, 21, 17, 27, 22, 35, 19, 17 Let's include one more data...
42, 31, 48, 12, 21, 17, 27, 22, 35, 19, 17 Let's include one more data point in the data set. Suppose this new data value lies between the values 12 and 48, inclusively. Find the smallest possible value of the median of the new data set Find the largest possible value of the median of the new data set
Age Weight (pounds) 56 156 35 110 22 80 57 91 40 90 22 150 48...
Age Weight (pounds) 56 156 35 110 22 80 57 91 40 90 22 150 48 165 48 99 25 189 40 178 25 123 25 190 59 255 49 112 33 155 56 134 20 117 31 91 27 100 23 80 45 190 29 255 31 112 59 200 39 134 35 110 44 99 27 110 24 180 27 150    The data represents the age and weight (in pounds) of 30 Langley residents.   Create a model to...
46 42 37 52 58 36 75 72 90 65 78 80 56 40 48 60...
46 42 37 52 58 36 75 72 90 65 78 80 56 40 48 60 58 76 82 75 38 48 86 88 75 65 45 80 68 47 62 75 56 85 44 70 75 64 67 72 For the data set distribution of Salaries of the employees                                               Arrange in an array (ascending order) Evaluate the mean, the median, the mode, the skew, the average deviation the standard deviation and the interquartile range. Group, on a new...
Assume the following list of keys: 78, 40, 16, 82, 64, 67, 57, 40, 37, 47,...
Assume the following list of keys: 78, 40, 16, 82, 64, 67, 57, 40, 37, 47, 72, 14, 17, 27, 55 This list is to be sorted using the quick sort algorithm as discussed in this chapter. Use pivot as the median of the first, last, and middle elements of the list. What is the pivot? Give the resulting list after one call to the partition function.
May 62 58 35 95 102 149 80 229 69 227 202 138 201 72 135...
May 62 58 35 95 102 149 80 229 69 227 202 138 201 72 135 274 99 117 146 146 89 167 141 251 145 189 156 229 214 113 204 188 330 250 170 183 174 127 133 232 244 336 138 178 162 Open Tornadoes_HW6 data. Test if the month of May has more than 130 tornadoes on average. 13. What test/procedure did you perform? (5 points) a. One-sided t-test b. Two-sided t-test c. Regression d. Confidence...
G R 57 62 46 58 85 81 80 88 95 84 31 54 56 44...
G R 57 62 46 58 85 81 80 88 95 84 31 54 56 44 40 65 52 37 26 51 93 76 54 43 67 64 42 59 29 51 81 70 35 49 59 61 44 57 84 97 34 55 49 44 73 86 74 89 44 52 41 49 72 61 60 48 48 69 92 87 64 77 52 47 58 66 84 80 60 50 49 38 96 74 20 49 42 19...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT