Question

Show the first partition of quicksort using median of three numbers technique for the pivot element....

Show the first partition of quicksort using median of three numbers technique for the pivot element. Circle the pivot. Show step by step of your work.

23     5     27   25   9   7    11   19   17    1    29      3    21      13

0 0
Add a comment Improve this question Transcribed image text
Answer #1

0 1 2 3 4 5 6 7 8 9 10 11 12 13 -> Index values 23 5 27 25 9 7 11 19 17 1 29 3 21 13 -> Given list Pivot - 11 [index - 6]

STEP BY STEP SOLUTION:

1. Select the leftmost index value of the 1st number which is "0" and the rightmost index value which is 13

2. Calculate the median. Sum the index values (0+13). If the sum is an odd number which is true in this case, then add 1 to the sum and divide the final number by 2.

   So, the median here would be (13+1)/2 = 7 (i.e. the 7th index value starting from the leftmost value which is 11 in this case)

3. Now We will compare the three numbers using the index values and based on the below defined conditions:
       left - 0 (value 23)
       right - 13 (value 13)
       mid - 7 (value 11)
      
       Conditions -
           1. if right < left then we will swap the values
           2. if mid < left then we will swap the values
           3. if right < mid then we will swap the values
     
4. Once the first iteration is completed then we ill return the "median" value.

5. Do the first four steps in recursive manner.

Below is the sample code -
  
# Get the median of three of the array, changing the array as you do.
# arr = Data Structure (List)
# left = Left most index into list to find MOT on.
# right = Right most index into list to find MOT on

int mdeianofthree(arr, left, right)
{
   mid = (left + right)/2
       if (arr[right] < arr[left])
           {
               Swap(arr, left, right);
           }
       if (arr[mid] < arr[left])
           {
               Swap(arr, mid, left);
           }
       if arr[right] < arr[mid]:
           {
               Swap(arr, right, mid);
           }
       return mid;
   }
  
void Swap(arr, left, right)
{
       temp = arr[left];
       arr[left] = arr[right];
       arr[right] = temp;
      
   }

Add a comment
Know the answer?
Add Answer to:
Show the first partition of quicksort using median of three numbers technique for the pivot element....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am trying to implement this version of quicksort in C/c++ but I am getting stuck....

    I am trying to implement this version of quicksort in C/c++ but I am getting stuck. Below is how the algorithm is supposed to work. This is the partitioning scheme I am trying to implement. 17, -10, 7, 19, 21, 23, -13, 31, 59 # ^ ^ start 17, -10, 7, 19, 21, 23, -13, 31, 59 # ^ ^ move left pointer to first element larger than pivot. 3 compares 17, -10, 7, 19, 21, 23, -13, 31, 59...

  • And the related algorithms: (20 points) Consider the following strategy for choosing a pivot element for...

    And the related algorithms: (20 points) Consider the following strategy for choosing a pivot element for the Partition subroutine of QuickSort, applied to an array A. .Let n be the number of elements of the array A. If n 24, perform an Insertion Sort of A and return. Otherwise: Choose 2n/2)| elements at random from n; let S be the new list with the chosen elements. Sort the list S using Insertion Sort and use the median m of S...

  • Show the contents of the array below, once the “pivot” element is placed at its appropriate...

    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 ascending order (from smallest to largest value). Always select the first element of the partition as “pivot” Apply sorting on the following data set 19,       20,       1,         13,       16,       5,         4,         9,         14,       7 Index 0 1 2 3 4 5 6 7...

  • Using the code below, we need to implement the median-of-three method for selecting a pivot. If...

    Using the code below, we need to implement the median-of-three method for selecting a pivot. If it is possible to add the code needed and any pseudo code as comments, that would be helpful. Thank you Code: def quickSort(alist): quickSortHelper(alist,0,len(alist)-1) def quickSortHelper(alist,first,last): if first<last: splitpoint = partition(alist,first,last) quickSortHelper(alist,first,splitpoint-1) quickSortHelper(alist,splitpoint+1,last) def partition(alist,first,last): pivotvalue = alist[first] leftmark = first+1 rightmark = last done = False while not done: while leftmark <= rightmark and \ alist[leftmark] <= pivotvalue: leftmark = leftmark + 1...

  • 13) Using non-randomized Select(A, n, i) to find the 4h largest element in array A, with A as ท first pivot 8 11 14...

    13) Using non-randomized Select(A, n, i) to find the 4h largest element in array A, with A as ท first pivot 8 11 14 0 9 4 25 33 98 5 22 63 54 2 111 4 I put the first pivot in the right place for you 45 23 19 728 16 116 10 w the array in sequence, one in each row, after each recursive call of the algorithm. You must use blanks (or% out) for the parts...

  • Construct a Stem and Leaf plot (stemplot) using the data below. The sum (total) of the...

    Construct a Stem and Leaf plot (stemplot) using the data below. The sum (total) of the numbers that comprise the leaf in the first row is ____: 11 25 22 19 23 21 17 27 21 21 23 28 10 29 11 17 19 29

  • 1. Show the steps in order to sort {11,5,6,3,8,1,9,2} using Mergesort algorithm. 2. Show the element...

    1. Show the steps in order to sort {11,5,6,3,8,1,9,2} using Mergesort algorithm. 2. Show the element sequences of running Shellsort on the input {15,2,8,1,10,7,4,3,9,11,12,6} at the increments {7, 3, 1}, respectively. 3. Show the steps in details of sorting {15, 2, 8, 1, 10, 7, 4, 3, 9, 11, 12, 6} using quicksort with median-of-three partitioning and a cutoff 3 (if the elements are less than 3, using insertion sort).

  • Implement quicksort and bucket sort. Use the code in your book to help; the partition in...

    Implement quicksort and bucket sort. Use the code in your book to help; the partition in quicksort is tricky. Make sure your implementations are correct — it is easy to gain some confidence in the correctness of your code by writing a program which creates arrays filled with random numbers, sorts them, and then checks that they are sorted. Then time your code (using clock() or similar methods) on both methods for arrays filled with random integers of the following...

  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine...

    Assembly Language Programming Assignment program must be in: MASM assembly language / x86 architecture / irvine library procedures Objectives: 1. using register indirect addressing 2. passing parameters 3. generating “random” numbers 4. working with arrays Description: Write and test a MASM program to perform the following tasks: 1. Introduce the program. 2. Generate ARRAYSIZE random integers in the range [LO = 10 .. HI = 29], storing them in consecutive elements of an array. ARRAYSIZE should be set to 200....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT