Question

In: Computer Science

implement c++ Quicksort using median of 3 as pivot. this also pass comparator as a function...

implement c++ Quicksort using median of 3 as pivot. this also pass comparator as a function param so it can sort vector in increasing or decreasing order based on the comparator passed. the test code uses std::less comparator.

#ifndef QSORT_H

#define QSORT_H

#include

#include

using namespace std;

template

T median_of_three(T& a, T& b, T& c, TComparator comparator) {

}

template

size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) {

// TODO: implement.

}

template

void QuickSort(vector& vec, TComparator comparator,size_t low,size_t high) {

if(comparator(low,high)){

size_t loc = partition(vec,comparator,low,high);

QuickSort(vec,comparator,low,loc-1);

QuickSort(vec,comparator,loc+1,high);

}

return;

}

template

void quicksort(vector& vec, TComparator comparator) {

// TODO: implement.

size_t size = vec.size();

QuickSort(vec,comparator,0,size-1);

}

#endif

test_case:

int main() {

srand(time(NULL));

vector test;

for (int i =0; i < 10; i++) {

test.push_back(rand() % 1000);

}

for (int i = 0; i < 10; i++) {

cout << test[i] << " ";

}

cout<<"Before sort;"<

quicksort(test, std::less {});

for (int i = 0; i < 10; i++) {

cout << test[i] << " ";

}

cout << endl;

return 0;

}

Solutions

Expert Solution

#include <iostream>
#include<algorithm>
using namespace std;
#define size 10
int i;   
void show(int* array, int n);
int partition(int* array, int pValue, int left, int right);
void QuickSort(int* array, int left, int right);

int main(void)
{
int array[size];
int i;

for( i = 0; i < size; i++)
{
array[i]=rand()%100;
}

cout<<endl<<"The random generated numbers are: "<<endl;
show(array, size);
QuickSort(array,0,size - 1);
cout<<endl<<"The sorted numbers are : "<<endl;
show(array, size);

system("pause");
return 0;
}

void show(int* array, int n)
{
int i;

for( i = 0; i < n; i++) cout<<array[i]<<'\t';
}

void QuickSort(int* array, int left, int right)
{
for(i=0;i<3;i++)
{
array[i]=array[rand()%100];
}
stable_sort(array,array+3);
int p=array[(i+1)/2];
//int p = array[left];
int split;

if(right > left)   
{
split = partition(array, p, left, right);

array[split] = p;
QuickSort(array, left, split-1);   
QuickSort(array, split+1, right);
}
}


int partition(int* array, int p, int left, int right)
{
int lb = left;
int rb = right;

while(lb < rb)   
{
while( p < array[rb]&& rb > lb)
{
rb--;   
}
swap(array[lb], array[rb]);

while( p >= array[lb]&& lb < rb)   
{
lb++;
}
swap(array[rb], array[lb]);

}
return lb;


}


Related Solutions

Choose a pivot using the median of 3 technique and show the result after 1 pass...
Choose a pivot using the median of 3 technique and show the result after 1 pass of quicksort (stop just before choosing a 2nd pivot) for the list [ 85, 40,60, 30, 55, 63, 70, 75 ,25]
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std;...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std; template T median_of_three(T& a, T& b, T& c, TComparator comparator) { } template size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) { // TODO: implement. } template void QuickSort(vector& vec, TComparator comparator,size_t low,size_t high) { if(comparator(low,high)){ size_t loc = partition(vec,comparator,low,high); QuickSort(vec,comparator,low,loc-1); QuickSort(vec,comparator,loc+1,high); } return; } template void quicksort(vector& vec, TComparator comparator) { // TODO: implement. size_t size = vec.size(); QuickSort(vec,comparator,0,size-1); } #endif test_case:...
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...
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?
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.
Python Programming Question 1. Implement the median-of-three method for selecting a pivot value as a modification...
Python Programming Question 1. Implement the median-of-three method for selecting a pivot value as a modification to quickSort (name this function mo3_quickSort). Prepare test cases for your mo3_quickSort .function QuickSort function: def quickSort(alist): quickSortHelper(alist,0,len(alist)-1) def quickSortHelper(alist,first,last): if first 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 while alist[rightmark] >= pivotvalue and rightmark >=...
Using C: Implement function types that takes no input, declares 3 variables of type char, 3...
Using C: Implement function types that takes no input, declares 3 variables of type char, 3 of type short, 3 of type int, and 3 of type double---in that order---and prints the addresses of the 12 variables---in the same order---in both hex (use %p conversion instruction) and unsigned long format. &a1 = 0x7ffd45e3ac0f, 140725776002063 &a2 = 0x7ffd45e3ac0e, 140725776002062 &a3 = 0x7ffd45e3ac0d, 140725776002061 &b1 = 0x7ffd45e3ac0a, 140725776002058 &b2 = 0x7ffd45e3ac08, 140725776002056 ...
Overview In this assignment you are required to implement binary code comparator using Xilinx that it...
Overview In this assignment you are required to implement binary code comparator using Xilinx that it is compatible with the MXK Seven Segment Displays. You will draw your digital logic circuit using Xilinx and then simulate it to verify the functionality of your design. Software Requirements ? Xilinx ISE 10.1 or higher Specifications Binary Code Comparator The binary code comparator is to be implemented and made compatible with the seven 7-segment displays of the board. Represent the first five digits...
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new...
C++ Program 2–Implement a Priority queue using a SORTED list. Use Quicksort after adding a new node.Example of quick sort below. Adopt to your program. #include <iostream> voidquickSort(inta[], intfirst, intlast); intpivot(inta[], intfirst, intlast); voidswap(int& a, int& b); voidswapNoTemp(int& a, int& b); voidprint(intarray[], constint& N); usingnamespacestd; intmain() { inttest[] = { 7, -13, 1, 3, 10, 5, 2, 4 }; intN = sizeof(test)/sizeof(int); cout << "Size of test array :"<< N << endl; cout << "Before sorting : "<< endl; print(test,...
Complete the function main in file median.c to implement the computation of the median of a...
Complete the function main in file median.c to implement the computation of the median of a sequence of integers read from stdin. The program should use realloc to allow an arbitrary number of integers to be read into a dynamically allocated array. Every time realloc is called, let the new size of the array be twice the old size plus 1. Look at function readLongLine in the slides for Chapter 6 for an example of how to use realloc. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT