Kat H.

asked • 10/06/21

Partition Function Write the partition function (described in function comment below).

Include source code and please make the code that you add bold.


Write the partition function (described in function comment below).


Do not use std::algorithms.


Make sure that you only work on the correct part of the array. The partition function will NOT always be one on the range 0 - (size - 1)


Code:


#include <iostream>


using namespace std;


/**

* @brief partitionFunct partitions a range in array and returns pivot location

* @param arr array to partition

* @param low index to start the partition at (inclusive)

* @param high index to end the partition at (inclusive)

* @return pivot final index of pivot value

*

* @post

* for all i < p, arr[i] <= arr[p]

* for all i > p, arr[i] >= arr[p]

* where p is index of pivot value

*/

int partitionFunct(int arr[], int low, int high) {

//TODO - Fixme


//Pivot value is at arr[low[


//Set up i at low + 1, and j at high


//While i and j have not crossed over

// Until i is at something larger than pivot or passes j, increment it

// Until j is at something smaller than pivot or passes i, decrement it

// If i and j have not crossed, swap those elements


//Swap low and j to place pivot


//return new location of pivot


return 0; //Delete this

}


tests.cpp:


//Bring in unit testing code and tell it to build a main function

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN

//This pragma supresses a bunch of warnings QTCreator produces (and should not)

//#pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses"

#include "doctest.h"


//Use Approx from doctest without saying doctest::Approx

using doctest::Approx;


//Declare functions we will test

int partitionFunct(int arr[], int low, int high);

void quickSort(int arr[], int arrSize);


#include <algorithm>

#include <random>


using namespace std;


TEST_CASE( "Partition/Basic" ) {

int arr[] = {12, 18, 3, 20, 6, 7, 15};

const int size = 7;


int backup[size]; //used to verify all values are kept

std::copy(arr, arr + size, backup);


int pivotLocation = partitionFunct(arr, 0, size - 1);


INFO("You claimed the pivot was " << arr[pivotLocation]

<< " located at " << pivotLocation);

for(int i = 0; i < pivotLocation; i++)

REQUIRE( arr[i] <= arr[pivotLocation] );

for(int i = pivotLocation + 1; i < size; i++)

REQUIRE( arr[i] >= arr[pivotLocation] );


bool hasAll = true;

for(int val : arr) {

if(std::find(backup, backup+size, val) == backup+size) {

hasAll = false;

break;

}

}

INFO("Your array does not have the right elements!");

REQUIRE( hasAll == true );

}


TEST_CASE( "Partition/Duplicates" ) {

int arr[] = {5, 10, 5, 12, 2, 18, 5, 10};

const int size = 8;


int backup[size]; //used to verify all values are kept

std::copy(arr, arr + size, backup);


int pivotLocation = partitionFunct(arr, 0, size - 1);


INFO("You claimed the pivot was " << arr[pivotLocation]

<< " located at " << pivotLocation);

for(int i = 0; i < pivotLocation; i++)

REQUIRE( arr[i] <= arr[pivotLocation] );

for(int i = pivotLocation + 1; i < size; i++)

REQUIRE( arr[i] >= arr[pivotLocation] );


bool hasAll = true;

for(int val : arr) {

if(std::find(backup, backup+size, val) == backup+size) {

hasAll = false;

break;

}

}

INFO("Your array does not have the right elements!");

REQUIRE( hasAll == true );

}


TEST_CASE( "Partition/Partial" ) {

//Partitions only the range 1-5

int arr[] = {999, 6, 10, 2, 8, 4, 999};

const int size = 7;


int backup[size]; //used to verify all values are kept

std::copy(arr, arr + size, backup);


int pivotLocation = partitionFunct(arr, 1, 5);


INFO("You claimed the pivot was " << arr[pivotLocation]

<< " located at " << pivotLocation);

for(int i = 2; i < pivotLocation; i++)

REQUIRE( arr[i] <= arr[pivotLocation] );

for(int i = pivotLocation + 1; i < 6; i++)

REQUIRE( arr[i] >= arr[pivotLocation] );


bool hasAll = true;

for(int val : arr) {

if(std::find(backup, backup+size, val) == backup+size) {

hasAll = false;

break;

}

}

INFO("Your array does not have the right elements!");

REQUIRE( hasAll == true );

}

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.