Question

In: Computer Science

Trace the execution of quicksort on the following array, assuming that the first item in each...

Trace the execution of quicksort on the following array, assuming that the first item in each subarray is the pivot value. Show the values of first and last for each recursive call and the array elements after returning from each call. Also, show the value of pivot during each call and the value returned through pivIndex. How many times is sort called, and how many times is partition called? 55 50 10 40 80 90 60 100 70 80 20 50 22

The following code is provided in the textbook:

/** Implements the quicksort algorithm. */

public class QuickSort {

/** Sort the table using the quicksort algorithm. @pre table contains Comparable objects. @post table is sorted. @param table The array to be sorted */

public static > void sort(T[] table) { // Sort the whole table. quickSort(table, 0, table.length - 1); }

/** Sort a part of the table using the quicksort algorithm.

@post The part of table from first through last is sorted.

@param table The array to be sorted

@param first The index of the low bound
@param last The index of the high bound

*/

private static > void quickSort(T[] table, int first, int last) {

if (first < last) { // There is data to be sorted.

// Partition the table.

int pivIndex = partition(table, first, last);

// Sort the left half.

quickSort(table, first, pivIndex - 1);

// Sort the right half. quickSort(table, pivIndex + 1, last);

}

}

/** Partition the table so that values from first to pivIndex are less than or equal to the pivot value, and values from pivIndex to last are greater than the pivot value.

@param table The table to be partitioned

@param first The index of the low bound

@param last The index of the high bound

@return The location of the pivot value

*/

private static > int partition(T[] table, int first, int last) {

// Select the first item as the pivot value.

T pivot = table[first]; int up = first; int down = last;

do {

/* Invariant: All items in table[first . . . up - 1] <= pivot All items in table[down + 1 . . . last] > pivot */

while ((up < last) && (pivot.compareTo(table[up]) >= 0)) { up++;

}

// assert: up equals last or table[up] > pivot.

while (pivot.compareTo(table[down]) < 0) {

down--; }

// assert: down equals first or table[down] <= pivot.

if (up < down) {

// if up is to the left of down. //

Exchange table[up] and table[down].

swap(table, up, down);

}

}

while (up < down);

// Repeat while up is left of down. //

Exchange table[first] and table[down] thus putting the // pivot value where it belongs.

swap(table, first, down);

// Return the index of the pivot value. return down;

}

Solutions

Expert Solution

QUICK SORT

Assumption of language convention:

i = first

j = last

p = pivIndex

i = up

j = down

page 1

page 2

page 3

page 4

page 5


Related Solutions

Trace the execution of the following program assuming the input stream contains the numbers 10, 3,...
Trace the execution of the following program assuming the input stream contains the numbers 10, 3, and 14.3. Use a table that shows the value of each variable at each step. Also show the output (exactly as it would be printed) // FILE: Trace.java // PURPOSE: An exercise in tracing a program and understanding // assignment statements and expressions. import java.util.Scanner; public class Trace { public static void main (String[] args) { int one, two, three; double what; Scanner scan...
trace by using quicksort on the array : 4 6 5 3 2 7 1 using...
trace by using quicksort on the array : 4 6 5 3 2 7 1 using 7 as the pivot
Make a detailed trace of the execution of the following program fragment, giving the contents of...
Make a detailed trace of the execution of the following program fragment, giving the contents of all the registers, and memory locations involved. Assume that before execution begins the SS register contains 0410h, and the SP register 0100h, and that the contents of AX, BX, CX, and DX are 789Ah, 0020h, 2000h, and 1234h respectively. SS SP TOS AX BX CX DX Initial contents 0990 0100 / 789A 0020 2000 1234 after PUSH AX after PUSH BX after PUSH CX...
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...
in C++, Modify the quicksort algorithm such that it uses the last item as the pivot...
in C++, Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order. NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array. After it has been sorted in descending order, go through all the items in the array and make...
Quicksort - Divider and Conquer Illustrate the operation of PARTITION on the array: A = {9,...
Quicksort - Divider and Conquer Illustrate the operation of PARTITION on the array: A = {9, 19, 13, 5, 12, 8, 7, 4, 21, 6, 11}. Let A[1] = 9 be the pivot value.
Trace the execution of my_recursive_function(100) and my_recursive_function(32) for the following code snippet. [0.5 M] void my_recursive_function(int...
Trace the execution of my_recursive_function(100) and my_recursive_function(32) for the following code snippet. [0.5 M] void my_recursive_function(int n) { if(n == 0) { printf("False"); return; } if(n == 1) { printf("True"); return; } if(n%2==0) my_recursive_function(n/2); else { printf("False"); return; } } int main() { my_recursive_function(n); return 0; }
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.
(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,...
What is the running time of Quicksort when the input array is sorted in ascending order?...
What is the running time of Quicksort when the input array is sorted in ascending order? (please explain in detail)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT