adding cs235
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
#include "Factory.h"
|
||||
#include "qstest.h"
|
||||
//You may add #include statments here
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* Unlike all other documents provided, you may modify this document slightly (but do not change its name)
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
createQSTest()
|
||||
|
||||
Creates and returns an object whose class extends QSTestInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "QSTest", you might say, "return new QSTest();".
|
||||
*/
|
||||
QSTestInterface* Factory::createQSTest()
|
||||
{
|
||||
return new qstest();//Modify this line
|
||||
}
|
||||
//=======================================================================================
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "QSTestInterface.h"
|
||||
#pragma once
|
||||
/*
|
||||
* WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
//=======================================================================================
|
||||
/*
|
||||
createQSTest()
|
||||
|
||||
Creates and returns an object whose class extends QSTestInterface.
|
||||
This should be an object of a class you have created.
|
||||
|
||||
Example: If you made a class called "QSTest", you might say, "return new QSTest();".
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
public:
|
||||
static QSTestInterface* createQSTest();
|
||||
};
|
||||
|
||||
//=======================================================================================
|
||||
@@ -0,0 +1,31 @@
|
||||
CXXFLAGS= -Wall -g -std=c++0x
|
||||
OBJECTS=Factory.o qstest.o sorter.o pwnd.o ignoreme.a
|
||||
EXE=main
|
||||
all: pwnd.o $(EXE)
|
||||
|
||||
$(EXE): $(OBJECTS)
|
||||
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
|
||||
test: test.cpp qstest.o sorter.o
|
||||
rtest: test
|
||||
./test
|
||||
Factory.o: Factory.cpp Factory.h
|
||||
qstest.o: qstest.cpp qstest.h
|
||||
sorter.o: sorter.cpp sorter.h
|
||||
pwnd.o: pwnd.c
|
||||
|
||||
run: main
|
||||
./main
|
||||
|
||||
clean:
|
||||
@rm -vf *.o
|
||||
@rm -vf $(EXE)
|
||||
@rm -vf *.1
|
||||
@rm -vf *.0
|
||||
@rm -vf test
|
||||
@rm -rvf *.dSYM
|
||||
|
||||
drun: main
|
||||
gdb ./main
|
||||
|
||||
valgrind: $(EXE)
|
||||
valgrind --tool=memcheck --leak-check=yes ./$(EXE)
|
||||
@@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
|
||||
class QSInterface
|
||||
{
|
||||
public:
|
||||
QSInterface(){}
|
||||
virtual ~QSInterface(){}
|
||||
|
||||
/*
|
||||
* For all methods below, it is assumed that the given array's size is correctly provided by the following parameter.
|
||||
*/
|
||||
|
||||
/*
|
||||
* sortAll()
|
||||
*
|
||||
* Sorts elements of the given array. After this method is called, every
|
||||
* element in the array is less than or equal to the following element.
|
||||
*
|
||||
* Does nothing if the given array is empty.
|
||||
*
|
||||
* @param data
|
||||
* an array of integers
|
||||
* @param size
|
||||
* the correct size of the given array
|
||||
*/
|
||||
virtual void sortAll(int data[], int size) = 0;
|
||||
|
||||
/*
|
||||
* sort()
|
||||
*
|
||||
* Sorts elements of the given array between the two given indices. After
|
||||
* this method is called, every element between the indices is less than or
|
||||
* equal to the following element.
|
||||
*
|
||||
* Does nothing if the given array is empty, if either of the given integers
|
||||
* is out of bounds, or if the first integer is not less than the second
|
||||
* integer.
|
||||
*
|
||||
* @param data
|
||||
* an array of integers
|
||||
* @param size
|
||||
* the correct size of the given array
|
||||
* @param left
|
||||
* the left boundary for the subarray to sort
|
||||
* @param right
|
||||
* the right boundary for the subarray to sort
|
||||
*/
|
||||
virtual void sort(int data[], int size, int left, int right) = 0;
|
||||
|
||||
/*
|
||||
* medianOfThree()
|
||||
*
|
||||
* Performs median-of-three pivot selection from among the values in
|
||||
* the given array between the two given indices. Median-of-three pivot
|
||||
* selection will sort the first, middle, and last elements in a given
|
||||
* array with respect to each other. After this method is called,
|
||||
* data[first] <= data[middle] <= data[last], where middle =
|
||||
* (first + last)/2.
|
||||
*
|
||||
* Does nothing if the given array is empty, if either of the given integers
|
||||
* is out of bounds, or if the first integer is not less than the second
|
||||
* integer.
|
||||
*
|
||||
* @param data
|
||||
* an array of integers
|
||||
* @param size
|
||||
* the correct size of the given array
|
||||
* @param left
|
||||
* the left boundary for the subarray from which to find a pivot
|
||||
* @param right
|
||||
* the right boundary for the subarray from which to find a pivot
|
||||
*/
|
||||
virtual void medianOfThree(int data[], int size, int left, int right) = 0;
|
||||
|
||||
/*
|
||||
* Partitions a subarray around a pivot value selected according to
|
||||
* median-of-three pivot selection.
|
||||
*
|
||||
* The values which are smaller than the pivot should be placed to the left
|
||||
* of the pivot; the values which are larger than the pivot should be placed
|
||||
* to the right of the pivot.
|
||||
*
|
||||
* Does nothing and returns -1 if the given array is null, if either of the
|
||||
* given integers is out of bounds, or if the first integer is not less than
|
||||
* the second integer.
|
||||
*
|
||||
* @param data
|
||||
* an array of integers
|
||||
* @param size
|
||||
* the correct size of the given array
|
||||
* @param left
|
||||
* the left boundary for the subarray to partition
|
||||
* @param right
|
||||
* the right boundary for the subarray to partition
|
||||
* @return the pivot's ending index after the partition completes; -1 if
|
||||
* provided with bad input
|
||||
*/
|
||||
virtual int partition(int data[], int size, int left, int right) = 0;
|
||||
|
||||
/*
|
||||
* Swaps the values at the given indices within the given array.
|
||||
*
|
||||
* After this method is called, the values in data[i] and data[j] should be
|
||||
* swapped.
|
||||
*
|
||||
* Does nothing if the given array is null or if either of the given
|
||||
* integers is out of bounds.
|
||||
*
|
||||
* @param data
|
||||
* an array of integers
|
||||
* @param size
|
||||
* the correct size of the given array
|
||||
* @param i
|
||||
* the first index
|
||||
* @param j
|
||||
* the second index
|
||||
*/
|
||||
virtual void swap(int data[], int size, int i, int j) = 0;
|
||||
};
|
||||
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "QSInterface.h"
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* WARNING: It is expressly forbidden to modify any part of this document, including its name
|
||||
*/
|
||||
|
||||
class QSTestInterface
|
||||
{
|
||||
public:
|
||||
QSTestInterface(){}
|
||||
virtual ~QSTestInterface(){}
|
||||
|
||||
/*
|
||||
* For all methods below, the correctness of an operation is defined in QSInterface.h.
|
||||
*/
|
||||
|
||||
/*
|
||||
* testSortAll
|
||||
*
|
||||
* Returns true if the given QSInterface object correctly sorts given arrays.
|
||||
*
|
||||
* @param test
|
||||
* an implementation of the QSInterface class
|
||||
* @return
|
||||
* true if [test] correctly sorts; false otherwise
|
||||
*/
|
||||
virtual bool testSortAll(QSInterface* test) = 0;
|
||||
|
||||
/*
|
||||
* testSort
|
||||
*
|
||||
* Returns true if the given QSInterface object correctly sorts given subarrays.
|
||||
*
|
||||
* @param test
|
||||
* an implementation of the QSInterface class
|
||||
* @return
|
||||
* true if [test] correctly sorts; false otherwise
|
||||
*/
|
||||
virtual bool testSort(QSInterface* test) = 0;
|
||||
|
||||
/*
|
||||
* testMedianOfThree
|
||||
*
|
||||
* Returns true if the given QSInterface object correctly performs median-of-three pivot selection.
|
||||
*
|
||||
* @param test
|
||||
* an implementation of the QSInterface class
|
||||
* @return
|
||||
* true if [test] correctly selects a pivot; false otherwise
|
||||
*/
|
||||
virtual bool testMedianOfThree(QSInterface* test) = 0;
|
||||
|
||||
/*
|
||||
* testPartition
|
||||
*
|
||||
* Returns true if the given QSInterface object correctly partitions.
|
||||
*
|
||||
* @param test
|
||||
* an implementation of the QSInterface class
|
||||
* @return
|
||||
* true if [test] correctly partitions; false otherwise
|
||||
*/
|
||||
virtual bool testPartition(QSInterface* test) = 0;
|
||||
|
||||
/*
|
||||
* testSwap
|
||||
*
|
||||
* Returns true if the given QSInterface object correctly swaps elements.
|
||||
*
|
||||
* @param test
|
||||
* an implementation of the QSInterface class
|
||||
* @return
|
||||
* true if [test] correctly swaps elements = 0; false otherwise
|
||||
*/
|
||||
virtual bool testSwap(QSInterface* test) = 0;
|
||||
};
|
||||
@@ -0,0 +1,5 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int usleep(useconds_t usec) {
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
#include "qstest.h"
|
||||
|
||||
bool qstest::testSortAll(QSInterface* test) {
|
||||
sorter s;
|
||||
int temp[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21};
|
||||
int temp1[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21};
|
||||
test->sortAll(temp, 20);
|
||||
s.sortAll(temp1, 20);
|
||||
for(unsigned int i = 0; i < 20; i++) {
|
||||
if(temp[i] != temp1[i])
|
||||
return false;
|
||||
}
|
||||
return true;;
|
||||
}
|
||||
|
||||
bool qstest::testSort(QSInterface* test) {
|
||||
sorter s;
|
||||
int temp[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21};
|
||||
int temp1[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21};
|
||||
test->sort(temp, 20, 3, 15);
|
||||
s.sort(temp1, 20, 3, 15);
|
||||
for(unsigned int i = 0; i < 20; i++) {
|
||||
if(temp[i] != temp1[i])
|
||||
return false;
|
||||
}
|
||||
int temp2[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int temp3[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->sort(temp2, 10, 0, 9);
|
||||
s.sort(temp3, 10, 0, 9);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp2[i] != temp3[i])
|
||||
return false;
|
||||
}
|
||||
int temp4[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int temp5[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->sort(temp4, 10, -1, 4);
|
||||
s.sort(temp5, 10, -1, 4);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp4[i] != temp5[i])
|
||||
return false;
|
||||
}
|
||||
int temp6[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int temp7[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->sort(temp6, 10, 3, 7);
|
||||
s.sort(temp7, 10, 3, 7);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp6[i] != temp7[i])
|
||||
return false;
|
||||
}
|
||||
int temp8[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int temp9[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->sort(temp8, 10, 3, 10);
|
||||
s.sort(temp9, 10, 3, 10);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp8[i] != temp9[i])
|
||||
return false;
|
||||
}
|
||||
int tempa[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int tempb[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->sort(tempa, 10, 4, 4);
|
||||
s.sort(tempb, 10, 4, 4);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(tempa[i] != tempb[i])
|
||||
return false;
|
||||
}
|
||||
int tempc[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int tempd[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->sort(tempc, 10, 4, 2);
|
||||
s.sort(tempd, 10, 4, 2);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(tempc[i] != tempd[i])
|
||||
return false;
|
||||
}
|
||||
int tempe[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int tempf[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->sort(tempe, 10, 4, -1);
|
||||
s.sort(tempf, 10, 4, -1);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(tempe[i] != tempf[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool qstest::testMedianOfThree(QSInterface* test) {
|
||||
sorter s;
|
||||
int what[9] = {5, 5, 5, 5, 1, 5, 5, 5, 5};
|
||||
int what1[9] = {5, 5, 5, 5, 1, 5, 5, 5, 5};
|
||||
test->medianOfThree(what, 9, 0, 8);
|
||||
s.medianOfThree(what1, 9, 0, 8);
|
||||
for(unsigned int i = 0; i < 9; i++) {
|
||||
if(what[i] != what1[i])
|
||||
return false;
|
||||
}
|
||||
int temp[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int temp1[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->medianOfThree(temp, 10, 0, 9);
|
||||
s.medianOfThree(temp1, 10, 0, 9);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp[i] != temp1[i])
|
||||
return false;
|
||||
}
|
||||
int temp2[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int temp3[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->medianOfThree(temp2, 10, 2, 9);
|
||||
s.medianOfThree(temp3, 10, 2, 9);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp2[i] != temp3[i])
|
||||
return false;
|
||||
}
|
||||
int temp4[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int temp5[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->medianOfThree(temp4, 10, -2, 9);
|
||||
s.medianOfThree(temp5, 10, -2, 9);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp4[i] != temp5[i])
|
||||
return false;
|
||||
}
|
||||
int temp6[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int temp7[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->medianOfThree(temp6, 10, 2, -9);
|
||||
s.medianOfThree(temp7, 10, 2, -9);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp6[i] != temp7[i])
|
||||
return false;
|
||||
}
|
||||
int temp8[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int temp9[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->medianOfThree(temp8, 10, 7, 5);
|
||||
s.medianOfThree(temp9, 10, 7, 5);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp8[i] != temp9[i])
|
||||
return false;
|
||||
}
|
||||
int tempa[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int tempb[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->medianOfThree(tempa, 10, 7, 10);
|
||||
s.medianOfThree(tempb, 10, 7, 10);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(tempa[i] != tempb[i])
|
||||
return false;
|
||||
}
|
||||
int tempc[1] = {4};
|
||||
int tempd[1] = {4};
|
||||
test->medianOfThree(tempc, 10, 0, 0);
|
||||
s.medianOfThree(tempd, 10, 0, 0);
|
||||
for(unsigned int i = 0; i < 1; i++) {
|
||||
if(tempc[i] != tempd[i])
|
||||
return false;
|
||||
}
|
||||
int tempe[2] = {4, 8};
|
||||
int tempf[2] = {4, 8};
|
||||
test->medianOfThree(tempe, 10, 0, 1);
|
||||
s.medianOfThree(tempf, 10, 0, 1);
|
||||
for(unsigned int i = 1; i < 2; i++) {
|
||||
if(tempe[i] != tempf[i])
|
||||
return false;
|
||||
}
|
||||
int tempg[3] = {4, 8, 3};
|
||||
int temph[3] = {4, 8, 3};
|
||||
test->medianOfThree(tempg, 10, 0, 2);
|
||||
s.medianOfThree(temph, 10, 0, 2);
|
||||
for(unsigned int i = 3; i < 1; i++) {
|
||||
if(tempg[i] != temph[i])
|
||||
return false;
|
||||
}
|
||||
int tempi[4] = {4, 8, 3, 6};
|
||||
int tempj[4] = {4, 8, 3, 6};
|
||||
test->medianOfThree(tempi, 10, 0, 3);
|
||||
s.medianOfThree(tempj, 10, 0, 3);
|
||||
for(unsigned int i = 0; i < 4; i++) {
|
||||
if(tempi[i] != tempj[i])
|
||||
return false;
|
||||
}
|
||||
int tempk[4] = {4, 8, 3, 6};
|
||||
int templ[4] = {4, 8, 3, 6};
|
||||
test->medianOfThree(tempk, 10, 0, 5);
|
||||
s.medianOfThree(templ, 10, 0, 5);
|
||||
for(unsigned int i = 0; i < 4; i++) {
|
||||
if(tempk[i] != templ[i])
|
||||
return false;
|
||||
}
|
||||
return true;;
|
||||
}
|
||||
|
||||
bool qstest::testPartition(QSInterface* test) {
|
||||
sorter s;
|
||||
// int size[1] = {7};
|
||||
// int size1[1] = {7};
|
||||
// if(test->partition(size, 1, 0, 0) != s.partition(size1, 1, 0, 0)) {
|
||||
// return false;
|
||||
// }
|
||||
// for(unsigned int i = 0; i < 1; i++) {
|
||||
// if(size[i] != size1[i])
|
||||
// return false;
|
||||
// }
|
||||
int size2[2] = {7, 1};
|
||||
int size3[2] = {7, 1};
|
||||
if(test->partition(size2, 2, 0, 1) != s.partition(size3, 2, 0, 1)) {
|
||||
return false;
|
||||
}
|
||||
for(unsigned int i = 0; i < 2; i++) {
|
||||
if(size2[i] != size3[i])
|
||||
return false;
|
||||
}
|
||||
int temp[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4};
|
||||
int temp1[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4};
|
||||
if(test->partition(temp, 14, 0, 14) != s.partition(temp1, 14, 0, 14)) {
|
||||
return false;
|
||||
}
|
||||
for(unsigned int i = 0; i < 14; i++) {
|
||||
if(temp[i] != temp1[i])
|
||||
return false;
|
||||
}
|
||||
int temp2[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21};
|
||||
int temp3[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21};
|
||||
if(test->partition(temp2, 20, 0, 19) != s.partition(temp3, 20, 0, 19)) {
|
||||
return false;
|
||||
}
|
||||
for(unsigned int i = 0; i < 20; i++) {
|
||||
if(temp2[i] != temp3[i])
|
||||
return false;
|
||||
}
|
||||
int temp4[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4};
|
||||
int temp5[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4};
|
||||
if(test->partition(temp4, 14, -1, 13) != s.partition(temp5, 14, -1, 13)) {
|
||||
return false;
|
||||
}
|
||||
for(unsigned int i = 0; i < 14; i++) {
|
||||
if(temp4[i] != temp5[i])
|
||||
return false;
|
||||
}
|
||||
int temp6[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4};
|
||||
int temp7[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4};
|
||||
if(test->partition(temp6, 14, 6, 2) != s.partition(temp7, 14, 6, 2)) {
|
||||
return false;
|
||||
}
|
||||
for(unsigned int i = 0; i < 14; i++) {
|
||||
if(temp6[i] != temp7[i])
|
||||
return false;
|
||||
}
|
||||
int temp8[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4};
|
||||
int temp9[14] = {6, 1, 6, 2, 0, 0, 5, 8, 2, 8, 1, 9, 6, 4};
|
||||
if(test->partition(temp8, 14, 6, -2) != s.partition(temp9, 14, 6, -2)) {
|
||||
return false;
|
||||
}
|
||||
for(unsigned int i = 0; i < 14; i++) {
|
||||
if(temp8[i] != temp9[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool qstest::testSwap(QSInterface* test) {
|
||||
sorter s;
|
||||
int temp[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
int temp1[10] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 9};
|
||||
test->swap(temp, 10, 0, 9);
|
||||
s.swap(temp1, 10, 0, 9);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp[i] != temp1[i])
|
||||
return false;
|
||||
}
|
||||
test->swap(temp, 10, -1, 9);
|
||||
s.swap(temp1, 10, -1, 9);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp[i] != temp1[i])
|
||||
return false;
|
||||
}
|
||||
test->swap(temp, 10, 3, 6);
|
||||
s.swap(temp1, 10, 3, 6);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp[i] != temp1[i])
|
||||
return false;
|
||||
}
|
||||
test->swap(temp, 10, 8, 10);
|
||||
s.swap(temp1, 10, 8, 10);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp[i] != temp1[i])
|
||||
return false;
|
||||
}
|
||||
test->swap(temp, 10, 8, 8);
|
||||
s.swap(temp1, 10, 8, 8);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp[i] != temp1[i])
|
||||
return false;
|
||||
}
|
||||
test->swap(temp, 10, 8, 2);
|
||||
s.swap(temp1, 10, 8, 2);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp[i] != temp1[i])
|
||||
return false;
|
||||
}
|
||||
test->swap(temp, 10, 8, -2);
|
||||
s.swap(temp1, 10, 8, -2);
|
||||
for(unsigned int i = 0; i < 10; i++) {
|
||||
if(temp[i] != temp1[i])
|
||||
return false;
|
||||
}
|
||||
return true;;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef __QSTEST_H__
|
||||
#define __QSTEST_H__
|
||||
|
||||
#include "QSTestInterface.h"
|
||||
#include "sorter.h"
|
||||
|
||||
class qstest : public QSTestInterface {
|
||||
public:
|
||||
sorter s;
|
||||
bool testSortAll(QSInterface* test);
|
||||
bool testSort(QSInterface* test);
|
||||
bool testMedianOfThree(QSInterface* test);
|
||||
bool testPartition(QSInterface* test);
|
||||
bool testSwap(QSInterface* test);
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,109 @@
|
||||
#include "sorter.h"
|
||||
|
||||
int partitioner(int* input, int p, int r) {
|
||||
int pivot = input[r];
|
||||
while (p < r) {
|
||||
while (input[p] < pivot)
|
||||
p++;
|
||||
while (input[r] > pivot)
|
||||
r--;
|
||||
if (input[p] == input[r])
|
||||
p++;
|
||||
else if (p < r) {
|
||||
int tmp = input[p];
|
||||
input[p] = input[r];
|
||||
input[r] = tmp;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
void quicksort(int* input, int p, int r) {
|
||||
if ( p < r ) {
|
||||
int j = partitioner(input, p, r);
|
||||
quicksort(input, p, j-1);
|
||||
quicksort(input, j+1, r);
|
||||
}
|
||||
}
|
||||
|
||||
void sorter::sortAll(int data[], int size) {
|
||||
if(size == 0)
|
||||
return;
|
||||
quicksort(data, 0, size);
|
||||
}
|
||||
|
||||
void sorter::sort(int data[], int size, int left, int right) {
|
||||
if(size == 0 || left < 0 || left >= size || right < 0 || right >= size)
|
||||
return;
|
||||
quicksort(data, left, right);
|
||||
}
|
||||
|
||||
void sorter::medianOfThree(int data[], int size, int left, int right) {
|
||||
if(size == 0 || left < 0 || left >= size || right < 0 || right >= size || right < left)
|
||||
return;
|
||||
int l, m, r;
|
||||
l = data[left];
|
||||
r = data[right];
|
||||
m = data [(left + right)/2];
|
||||
if(l > m) {
|
||||
swap(data, size, left, (left + right)/2 );
|
||||
l = data[left];
|
||||
r = data[right];
|
||||
m = data [(left + right)/2];
|
||||
}
|
||||
if(l > r) {
|
||||
swap(data, size, left, right);
|
||||
l = data[left];
|
||||
r = data[right];
|
||||
m = data [(left + right)/2];
|
||||
}
|
||||
if(m > r) {
|
||||
swap(data, size, (left + right)/2 , right);
|
||||
l = data[left];
|
||||
r = data[right];
|
||||
m = data [(left + right)/2];
|
||||
}
|
||||
}
|
||||
|
||||
int sorter::partition(int data[], int size, int left, int right) {
|
||||
if(size == 0 || left < 0 || left >= size || right < 0 || right >= size || right < left)
|
||||
return -1;
|
||||
if(size == 1) {
|
||||
return 0;
|
||||
}
|
||||
medianOfThree(data, size, 0, right);
|
||||
swap(data, size, 0, (right + left)/2);
|
||||
int l = 1;
|
||||
int r = size - 1;
|
||||
bool ltrue = false;
|
||||
bool rtrue = false;
|
||||
while(l <= r) {
|
||||
if(data[0] < data[l]) {
|
||||
ltrue = true;
|
||||
}
|
||||
else {
|
||||
l++;
|
||||
}
|
||||
if(data[0] > data[r]) {
|
||||
rtrue = true;
|
||||
}
|
||||
else {
|
||||
r--;
|
||||
}
|
||||
if(ltrue && rtrue) {
|
||||
rtrue = false;
|
||||
ltrue = false;
|
||||
swap(data, size, l, r);
|
||||
}
|
||||
}
|
||||
swap(data, size, 0, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
void sorter::swap(int data[], int size, int i, int j) {
|
||||
if(size == 0 || i < 0 || i >= size || j < 0 || j >= size)
|
||||
return;
|
||||
int temp = data[i];
|
||||
data[i] = data[j];
|
||||
data[j] = temp;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef __SORTER_H__
|
||||
#define __SORTER_H__
|
||||
|
||||
#include "QSInterface.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class sorter : public QSInterface {
|
||||
public:
|
||||
void sortAll(int data[], int size);
|
||||
void sort(int data[], int size, int left, int right);
|
||||
void medianOfThree(int data[], int size, int left, int right);
|
||||
int partition(int data[], int size, int left, int right);
|
||||
void swap(int data[], int size, int i, int j);
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
#include "sorter.h"
|
||||
#include "qstest.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int size = 5;
|
||||
int temp[] = {5, 3, 7, 2, 8, 2, 4, 1, 6, 5};
|
||||
sorter s;
|
||||
s.sortAll(temp, size);
|
||||
int temp1[] = {5, 3, 7, 2, 5, 2, 4, 1, 6, 8};
|
||||
cout << "before" << endl;
|
||||
s.sort(temp1, size, 3, 7);
|
||||
// int temp3[20] = {4, 7, 3, 8, 12, -2, 41, 63, 2, 5, 53, 24, 64, 75, 132, 74, 24, 64, 745, 21};
|
||||
int temp3[1] = {5};
|
||||
// int what1[10] = {5, 5, 5, 5, 1, 5, 5, 5, 5};
|
||||
// if(test->partition(what, 10, 0, 8) != s.partition(what1, 10, 0, 8)) {
|
||||
// return false;
|
||||
// }
|
||||
cout << temp3[1/2] << " this is the median" << endl;
|
||||
for(int i = 0; i < 1; i++) {
|
||||
cout << temp3[i] << " ";
|
||||
}
|
||||
cout << "BEFORE" << endl;
|
||||
cout << s.partition(temp3, 1, 0, 0) << " this is the return value" << endl;
|
||||
for(int i = 0; i < 1; i++) {
|
||||
cout << temp3[i] << " ";
|
||||
}
|
||||
cout << "AFTER" << endl;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
test all sizes
|
||||
0, 1, 2, 3, 4, many
|
||||
test boundary cases
|
||||
|
||||
test [5,5,5,5,1,5,5,5,5]
|
||||
test [5, 1, 2, 3, 4, 1, 2, 3, 4, 5]
|
||||
[1, 2, 3, 4, 5]
|
||||
[5, 4, 3, 2, 1]
|
||||
Reference in New Issue
Block a user