Question

In: Computer Science

Suppose we are sorting an array of eight integers using quicksort, and we have just finished...

Suppose we are sorting an array of eight integers using quicksort, and we have just finished the first partitioning with the array looking like this: 2 5 1 7 9 12 11 10. What are the possible values of pivot?

Algorithm coures

Solutions

Expert Solution

Quicksort

It is a highly effective and efficient technique that uses the divide and conquer approach to solve the problem. The main array is subdivided into smaller arrays until and unless we reach the elements in the two arrays one of which holds values smaller than the specified value. The other array keeps on storing the values greater than the specified values. The pivot value serves as the basis for the division of the array. It keeps on changing after the values with the selected pivot value have been separated.

As per given array elements:- 2 5 1 7 9 12 11 10

Here,

If we start with the array,  2 5 1 7 9 12 11 10 we can start of by choosing any element of array as pivot.

Suppose, initially we start of choosing 10 as pivot element, then the low and high pointers are pointing at 2 and 11.

if (Low < pivot) then increment low

else if( High>pivot) then decrement high

else swap(low , high)

New Pivot after the above steps will be decided now. If left >=right, the point where they meet is the new pivot.

So as per the above statement:-

THE POSSIBLE VALUES OF PIVOT ARE:- 10, 11, 9, 7, 1, 2

After choosing 10 as pivot, the sorted array will look like :- 2 5 1 7 9 10 11 12

After choosing 11 as pivot, the sorted array will look like:- 2 5 1 7 9 10 11 12

After choosing 9 as pivot, the sorted array will look like:- 2 5 1 7 9 10 11 12

After choosing 7 as pivot, the sorted array will look like:- 2 5 1 7 9 10 11 12

After this we will choose 1 as Pivot, and our sorted array will become:- 1 5 2 7 9 10 11 12

At last we will chose 2 as pivot and the whole array will be sorted

1 2 5 7 9 10 11 12

Hope this helps :)


Related Solutions

Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
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...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 2. Give an efficient sorting algorithm for an array C[1, ..., n] whose elements are taken from the set {1, 2, 3, 4, 5}.
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 3. Give an efficient sorting algorithm for an array D[1, ..., n] whose elements are distinct (D[i] ̸= D[j], for every i ̸= j ∈ {1, ..., n}) and are taken from the set {1, 2, ..., 2n}.
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n...
Consider the problem of sorting an array A[1, ..., n] of integers. We presented an O(n log n)-time algorithm in class and, also, proved a lower bound of Ω(n log n) for any comparison-based algorithm. 1. Give an efficient sorting algorithm for a boolean1 array B[1, ..., n]. 2. Give an efficient sorting algorithm for an array C[1,...,n] whose elements are taken from the set {1,2,3,4,5}. 3. Give an efficient sorting algorithm for an array D[1, ..., n] whose elements...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers....
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using an ascending order selection sort, modified to print out the...
Sorting and Searching Given an unsorted array numbers of integers with duplicate values. Sort the array...
Sorting and Searching Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Sorting (selection, bubble, insertion) Suppose an array contains the following integers: -1, 20, 10, -5, 0,...
Sorting (selection, bubble, insertion) Suppose an array contains the following integers: -1, 20, 10, -5, 0, -7, 100, -7, 30, -10. Sort the array using the following algorithms: selection sort, bubble sort, and insertion sort. For each algorithm, write the array content after each pass (i.e., from pass 1 to pass 9). Each pass is defined as one iteration of the outer loop. There are total of n − 1 passes, where n is the array size. The answer should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT