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

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...
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.
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?
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...
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...
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.
Programming Language : JavaModify 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 sure...
Write a C function to implement operation of a stack using the following format: /** *...
Write a C function to implement operation of a stack using the following format: /** * function: *       push * * expects: *       pointer to the stack *       pointer to the size *       the value to push * * returns: *     true when value has been pushed *       false otherwise * * The push function push a value to the passed in stack */ bool push(int *stack, int *size, int max_size, int to_push) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT