Question

In: Computer Science

We are going to derive the average number of moves for quicksort using a somewhatunusual partitioning...

We are going to derive the average number of moves for quicksort using a somewhatunusual partitioning algorithm. We partition on the first element. Take it out. Look for theright most element that is smaller and place it in the first position (which is the newly openedposition on the left side). Look for the left most element that is larger and place it in the newlyopened position on the right side. Starting from there look for the right most element that issmaller and place it in the the newly opened position on the left side. Starting from there lookfor the left most element that is larger and place it in the newly opened position on the rightside. Continue in this fashion until the pointers cross. Finally, put the partition element intothe hole, which is its final position in the sorted array.(a) Assume that the partition element ends up in positionq.i. What is the probability that an element originally to the left (of positionq) went tothe right (of positionq)?ii. What is the expected number of elements originally to the left that go to the right?iii. What is the probability that an element originally to the right went to the left?iv. What is the expected number of elements originally to the right that go to the left?v. What is the total expected number of moves (for partition)?(b) Write a recurrence for the expected number of moves (for quicksort).(c) Simplify the recurrence, but do not solve it.

Solutions

Expert Solution

main.cpp:

#include <iostream>

using namespace std;

void swap(int* a, int* b)

{

int temp = *a;

*a = *b;

*b = temp;

}

int partition (int arr[], int LOW, int HIGH)

{

int i = (LOW - 1);

int pivot = arr[HIGH];

for (int j = LOW; j <= HIGH- 1; j++)

{

if (arr[j] <= pivot)

{

i++;

swap(&arr[i], &arr[j]);

}

}

swap(&arr[i + 1], &arr[HIGH]);

return (i + 1);

}

int AverageMoves (int arr[], int LOW, int HIGH)

{

int i = (LOW - 1);

int pivot = arr[HIGH];

int AvgMoves = 0;

for (int j = LOW; j <= HIGH- 1; j++)

{

if (arr[j] <= pivot)

{

i++;

swap(&arr[i], &arr[j]);

AvgMoves++;

}

}

return AvgMoves;

}

void quickSort(int arr[], int LOW, int HIGH)

{

if (LOW < HIGH)

{

int pivot = partition(arr, LOW, HIGH);

quickSort(arr, LOW, pivot - 1);

quickSort(arr, pivot + 1, HIGH);

}

}

void displayArray(int arr[], int size)

{

int i;

for (i=0; i < size; i++)

cout<<arr[i]<<"\t";

}

int main()

{

int arr[] = {6,9,8,4,3,12,23,43,51};

int n = sizeof(arr)/sizeof(arr[0]);

cout<<"Input array "<<endl;

displayArray(arr,n);

cout<<endl;

quickSort(arr, 0, n-1);

cout<<"\n";

cout<<"Array after sorted with quick sort: "<<endl << "\n";

displayArray(arr,n);

cout<<"\n";

cout<<"Average number of moves: ";

cout<<AverageMoves(arr, 0, n-1);

return 0;

}

Output:

Input array
6   9   8   4   3   12   23   43   51  

Array after sorted with quick sort:

3   4   6   8   9   12   23   43   51  
Average number of moves: 8


Related Solutions

Implement Quicksort in Java by sorting in parallel after partitioning; for this, the parent thread can...
Implement Quicksort in Java by sorting in parallel after partitioning; for this, the parent thread can continue sorting one segment and a child thread is created for sorting the other segment. However, create a new thread only if both segments contain more than S elements, otherwise sort sequentially both segments. %%writefile Quicksort.java import java.util.Random; YOUR CODE HERE public class Quicksort { static int N; // number of elements to be sorted static int S; // threshold for creating a sub-thread...
IN JAVA Implement Quicksort with ‘median-of-three’ partitioning You should be able to test a 1000, 10000...
IN JAVA Implement Quicksort with ‘median-of-three’ partitioning You should be able to test a 1000, 10000 and 10000 length array with inters spanning from 1 to 10,000. You cannot use functions from standard libraries implementing Quicksort.
9. Modify the quicksort and mergesort programs we learnt during the class to count the number...
9. Modify the quicksort and mergesort programs we learnt during the class to count the number of element comparisons for the two sorting methods. Use the following test drive to test them. public class Sorts {    int numComparisions = 0;    public void quicksort(int [] x, int l, int h)    { // your modifies codes go here    }    public void mergesort(int [] x, int l, int h)    { // your modifies codes go here   ...
Suppose we are interested in estimating the average number of customers in the supermarket. On average,...
Suppose we are interested in estimating the average number of customers in the supermarket. On average, there are 15 customers entering the supermarket every 5 minutes. And on average, a typical customer spends about 20 minutes in the supermarket (from entering to exiting the market). a. Calculate the average number of customers in the supermarket. b. About 20% of customers leave without making a purchase. On average, a customer spends about 5 minutes in the checkout area. Estimate the average...
Below is the pseudocode for Quicksort and Partition that we talked about in class. As usual...
Below is the pseudocode for Quicksort and Partition that we talked about in class. As usual with recursive functions on arrays, we see the array indices s and e as arguments. Quicksort(A, s, e) sorts the part of the array between s and e inclusively. The initial call (that is, to sort the entire array) is Quicksort(A, 0, n − 1) QuickSort(A, s, e)   if s < e p = Partition (A, s, e) // Partition the array and return...
If we change the randomized QUICKSORT algorithm so that it repeatedly randomly selects a pivot and...
If we change the randomized QUICKSORT algorithm so that it repeatedly randomly selects a pivot and runs PARTITION until it finds a good pivot, and suppose we keep track of the pivots used so far so we never use one twice for the same array, what is the worst case cost of the algorithm? Give a recurrence for this and its asymptotic solution (you can use the master method.)
Derive an expression for the power going into the braking system when a car is decelerated...
Derive an expression for the power going into the braking system when a car is decelerated from a speed u to a standstill with constant deceleration a (Remember using v = u-at = 0, where a is the magnitude of the deceleration) on a level road. Include the force required to overcome aerodynamic drag (1 ??2???) and rolling 2 friction (μmg). (2) Hence, derive an expression to allow the energy dissipated during the deceleration period to be calculated. (3) The...
Problem 1: Evaluation of a known integral using various quadratures: In this problem, we are going...
Problem 1: Evaluation of a known integral using various quadratures: In this problem, we are going to compute the price of a European call option with 3 month expiry, strike 12, and implied vol 20, Assume the underlying is 10 now and the interest rate is 4%. 1. Use Black-Scholes formula to compute the price of the call analytically. 2. Calculate the price of the call numerically using the following 3 quadrature methods: (a) Left Riemann rule (b) Midpoint rule...
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
What is the average performance of Quicksort assuming every input permutation is equally likely? Prove it....
What is the average performance of Quicksort assuming every input permutation is equally likely? Prove it. Hint: In this case pivot(split) element very likely to provide good balance during partition.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT