Question

In: Computer Science

. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to...

. Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are ≤x and all elements in the right portion A[m:hi] are ≥x) is the second element of the array to be split (i. e., A[lo+1]).

For a specific value of n at least 13 (your choice), construct an assignment of the numbers 1...n to the n array elements that causes QuickSort, with the stated choice of pivot, to

(a) execute optimally (that is A[lo:m] and A[m:hi] are always of equal size)

(b) execute in the slowest possible way.

Solutions

Expert Solution

class QuickSort {

  int partition(int arr[], int low, int high) {

    int pivot = arr[high];

    int i = (low - 1);

    for (int j = low; j < high; j++) {

      if (arr[j] <= pivot) {

        i++;

        int temp = arr[i];

        arr[i] = arr[j];

        arr[j] = temp;

      }

    }

    int temp = arr[i + 1];

    arr[i + 1] = arr[high];

    arr[high] = temp;

    return i + 1;

  }

  void sort(int arr[], int low, int high) {

    if (low < high) {

      int pi = partition(arr, low, high);

      sort(arr, low, pi - 1);

      sort(arr, pi + 1, high);

    }

  }

  static void printArray(int arr[]) {

    int n = arr.length;

    for (int i = 0; i < n; ++i) System.out.print(arr[i] + " ");

    System.out.println();

  }

  public static void main(String args[]) {

    int arr[] = { 8, 3, 25, 6, 10, 17, 1, 2, 18, 5 };

    int n = arr.length;

    QuickSort ob = new QuickSort();

    ob.sort(arr, 0, n - 1);

    System.out.println("sorted array");

    printArray(arr);

  }

}

******************************************************************************************
PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION
******************************************************************************************


Related Solutions

Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split...
Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are ≤x and all elements in the right portion A[m:hi] are ≥x) is the second element of the array to be split (i. e., A[lo+1]). For a specific value of n at least 13 (your choice), construct an assignment of the numbers 1...n to the n array elements that...
(do not need to code) Consider QuickSort on the array A[1:n] andassume that the pivot...
Consider QuickSort on the array A[1:n] and assume that the pivot element x (used to split the array A[lo:hi] into two portions such that all elements in the left portion A[lo:m] are =< x and all elements in the right portion A[m:hi] are >=x) is the first element of the array to be split (i. e., A[lo]).Construct an infinite sequence of numbers for n and construct an assignment of the numbers 1...n to the n array elements that causes QuickSort,...
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
What is the reason to choose a random pivot element in Randomized Quicksort? How can using...
What is the reason to choose a random pivot element in Randomized Quicksort? How can using randomization in this way possibly help?
(Data structure) Show the contents of the array below, once the “pivot” element is placed at...
(Data structure) Show the contents of the array below, once the “pivot” element is placed at its appropriate location after each call of the “Partition” algorithm, in the process of running Quick-Sort on said array. Arrange the data in descending order (from largest to smallest value). Always select the first element of the partition as “pivot” Apply sorting on the following data set s,       f,       p,      a,      g,      e,       v,      q,      i,        c
Consider sorting n numbers stored in array A by first finding the smallest element of A...
Consider sorting n numbers stored in array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A and exchange it with A[2]. Continue in this manner for the first n-1 elements of A. a) (10 points) This algorithm is known as the selection sort. Write the pseudo code for this algorithm. b) (10 points) What loop invariant does this algorithm maintain? Note: You do not...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array? ( Use Java and an original code )
What is the running time of QUICKSORT on an array A of length n containing all...
What is the running time of QUICKSORT on an array A of length n containing all the same values? What is the running time if A contains distinct elements sorted in decreasing order? Explain your answers in detail, referring to the source code for PARTITION and QUICKSORT.
Consider the element uniqueness problem: check whether all the elements in a given array of n...
Consider the element uniqueness problem: check whether all the elements in a given array of n elements are distinct answer in pseudo code places
Using Java, Given an array A[0 ... n-1], where each element of the array represent a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT