SlideShare a Scribd company logo
1 of 33
Assg 05: Quicksort
COSC 2336 Data Structures
Objectives
• Practice writing functions
• Practice writing recursive functions.
• Learn about Analysis of algorithms and O(n log n) sorts
Description
In this assignment we will be implementing one of the most
popular sorting
algorithms used in libraries (like the C++ STL library, and the
UNIX qsort
function) to provide basic sorting abilities, the Quicksort
algorithm. I would
recommend that you at least read section 7.5 from our
supplemental Shaffer
textbook on Quicksort, if not sections 7.1-7.5 talking about
three well known
O(n log n) sorting algorithms, and the 3 O(n2) algorithms we
discussed last
week.
Quicksort, when properly implemented, is very attractive
because it pro-
vides a way to do a fast sort completely in-place (without
having to allocate
additional memory to do the sort, beyond a single value needed
when swap-
ping two values in the list being sorted). In the worst case,
Quicksort is
actually O(n2), no better than bubble sort. But this worse case
only occurs
when every pivot selected is the wort possible, and does not
divide the list
at all. This is very unlikely to happen, unless you know how the
pivot is
selected, and specifically design the input list to always choose
the worst
possible pivot. On average the cost of Quicksort is O(n log n),
and it is
usually very likely that average case performance will result
when lists to be
sorted are relatively random.
The most direct implementation of Quicksort is as a recursive
algorithm.
Quicksort is an example of a divide and conquer approach to
solving the
problem of sorting the list. We are given a list of items, A and
indexes left
1
and right that indicate a sub-portion of the list to be sorted. left
and right
indicate the actual indexes, so if the list is a regular C array of
integers, and
the array is of size 10
int left;
int right;
const inst SIZE = 10;
int A[size];
Then to sort the whole list we set left = 0 and right = 9 to
initially
call the Quicksort function:
left = 0;
right = size-1;
quicksort(A, left, right);
Conceptually the steps of the Quicksort algorithm are as
follows:
1. if list size is 0 or 1 (left <= right) return (lists of this size are
sorted
by definition).
2. Choose a pivot value and swap the pivot value to the end of
the list
swap(pivotIndex, right)
3. Partition the list. Partitioning means all values less than the
pivot
value should end up on the left of the list, and all values greater
will
be on the right. The first index k where a value >= to the pivot
value
is at indicates the new left and right side sub-lists.
4. Swap the pivot value to its correct position k swap(k, right)
5. Recursively call Quicksort on the new left and right sub-lists
• quicksort(A, left, k-1)
• quicksort(A, k+1, right)
Most of the real work happens in the function/code to partition
the list.
The partitioning of the list, for Quicksort to be an in-place sort,
must work
by swapping values in-place in the list of items. All values
smaller than the
pivot value must end up on the left side, and all values greater
on the right.
The algorithm to partition the list is traditionally done with
these steps:
2
1. do a linear search from the left of list, stop at first value on
left that is
>= pivot
2. do a linear search from the right of list, stop at first value
that is <
than the pivot.
3. swap(left, right) assuming you were incrementing left and
decre-
menting right to point to indexes of values that are on wrong
sides
4. if left < right goto 1
Conceptually we search from both ends of the list, and when we
find
values that are on wrong sides with respect to the pivot value,
we swap
them. Eventually the search from both ends will meet
somewhere. The
location where they meet should be the index of the first value
that is >=
to the pivot (going from the left side of the list). This is the
index where the
pivot value should actually go in the list, because all values
before this are
smaller than the pivot, and all values at or after are greater than
the pivot.
Thus at the end of partitioning the list on some pivot value, we
are able to
swap 1 value each time to its correct final position in the list.
But the values
to the left and right will not be sorted, thus we call Quicksort
recursively on
these sub-lists to get them sorted.
In order to help you implement your own version of Quicksort,
we have
broken the problem down into useful sub-functions. If you
implement the
sub-functions as specified, in the order given, the final
implementation of the
Quicksort function is relatively straight forward, using these
smaller func-
tions.
For this assignment you need to perform the following tasks.
1. Write a function called swapListValues(). This functions
takes an
array of integers as its first parameter, and two indexes (the left
and
right indexes). This function does not return a value explicitly.
Recall
arrays are passed by reference. As the name implies, the two
values
in the array at the indicated left and right indexes are to be
swapped,
and since the array is passed by reference, after returning they
will be
swapped for the caller of this function.
2. Write a function called findAndSwapPivot(). This function
takes the
same 3 parameters, an array of integers, and two indexes
indicating the
left and right sides of a sub-portion of the list. The function
should find
the value in the middle of the left and right ends, which will be
chosen
as the pivot. The function should use the previous
swapListValues()
3
function to swap the chosen pivot value to the end of the list of
integers.
This function returns a value. This is different from how the
textbook
implements the find pivot function. Our function should return
the
actual pivot value that was chosen (not the pivotIndex, which
we know
should be the last index of the sub-list after calling this
function).
3. Write a function called partitionList(). This will implement
the al-
gorithm described preciously. This functions takes the 3 same
param-
eters for the previous functions, an integer array, and left and
right
indexes for the sub-portion of the list we are currently
partitioning.
In addition, this function takes a fourth parameter, the pivot
value).
This function should make use of the swapListValues() function
de-
fined previously when swapping values in-place in the list of
integers.
When this function is called, the pivot has been swapped to the
end
of the sub-portion of the list, so the right index will be one less
than
this. This function needs to correctly return the index, described
as
k above, where the pivot value should actually go. At the end,
the
location where the left search and right search meet will be this
index,
the final location found for the pivot value.
4. Finally write a function called quickSort() using the
described algo-
rithm above. This function will use all of the 3 previous
functions to do
its work. If implemented correctly, there is almost nothing to be
done
in this function besides calling the other 3 functions, and
recursively
calling itself (except to check for the base case of your
recursion).
You will again be given 3 starting template files like before, an
assg-
05.cpp file of tests of your code, and a QuickSort.hpp and
QuickSort.cpp
header and implementation file. As before, you should practice
incremental
development, and uncomment the tests in the assg-05.cpp file
one at a time,
and implement the functions in the order specified. If you
implement your
code correctly and uncomment all of the tests, you should get
the following
correct output:
Test swapListValues() ------------------------------------------
swapListValues() test 1, swap 2 values
expected: List length: 2 [10, 5]
actual : List length: 2 [10, 5]
swapListValues() test 2, same index
expected: List length: 2 [8, 6]
4
actual : List length: 2 [8, 6]
swapListValues() test 3, swap 2 values from inside of list
expected: List length: 12 [2, 7, 6, 3, 8, 4, 2, 9, 5, 8, 9, 1]
actual : List length: 12 [2, 7, 6, 3, 8, 4, 2, 9, 5, 8, 9, 1]
swapListValues() test 4, reverse the previous swap
expected: List length: 12 [2, 7, 9, 3, 8, 4, 2, 9, 5, 8, 6, 1]
actual : List length: 12 [2, 7, 9, 3, 8, 4, 2, 9, 5, 8, 6, 1]
swapListValues() test 5, swap with index 0
expected: List length: 12 [4, 7, 9, 3, 8, 2, 2, 9, 5, 8, 6, 1]
actual : List length: 12 [4, 7, 9, 3, 8, 2, 2, 9, 5, 8, 6, 1]
swapListValues() test 6, swap with last index
expected: List length: 12 [4, 7, 9, 3, 8, 2, 1, 9, 5, 8, 6, 2]
actual : List length: 12 [4, 7, 9, 3, 8, 2, 1, 9, 5, 8, 6, 2]
Test findAndSwapPivot() ----------------------------------------
findAndSwapPivot() test 1, basic test
expected: List length: 3 [5, 8, 3]
expected pivot: 3
actual: List length: 3 [5, 8, 3]
actual pivot: 3
findAndSwapPivot() test 2, test list size 1
expected: List length: 1 [5]
expected pivot: 5
actual: List length: 1 [5]
actual pivot: 5
findAndSwapPivot() test 3, bigger list with even number of
values
expected: List length: 10 [5, 2, 8, 7, 8, 9, 1, 4, 5, 3]
expected pivot: 3
actual: List length: 10 [5, 2, 8, 7, 8, 9, 1, 4, 5, 3]
actual pivot: 3
findAndSwapPivot() test 4, bigger list with odd number of
values
expected: List length: 15 [5, 2, 8, 7, 3, 9, 1, 18, 5, 8, 10, 11, 15,
22, 42]
5
expected pivot: 42
actual: List length: 15 [5, 2, 8, 7, 3, 9, 1, 18, 5, 8, 10, 11, 15,
22, 42]
actual pivot: 42
Test partitionList() -------------------------------------------
partitionList() test 1, basic test
expected: List length: 10 [1, 3, 4, 2, 9, 6, 7, 8, 8, 5]
expected pivot: 4
actual: List length: 10 [1, 3, 4, 2, 9, 6, 7, 8, 8, 5]
actual pivot: 4
partitionList() test 2, bigger test and some duplicates of the
pivot
expected: List length: 15 [4, 7, 6, 6, 19, 18, 12, 15, 10, 10, 12,
13, 11, 17, 10]
expected pivot: 4
actual: List length: 15 [4, 7, 6, 6, 19, 18, 12, 15, 10, 10, 12, 13,
11, 17, 10]
actual pivot: 4
partitionList() test 3, everything is smaller than pivot value
expected: List length: 6 [4, 3, 7, 6, 5, 10]
expected pivot: 5
actual: List length: 6 [4, 3, 7, 6, 5, 10]
actual pivot: 5
partitionList() test 4, everything is bigger than pivot value
expected: List length: 6 [4, 3, 7, 6, 5, 2]
expected pivot: 0
actual: List length: 6 [4, 3, 7, 6, 5, 2]
actual pivot: 0
partitionList() test 5, small list that needs a swap
expected: List length: 3 [2, 8, 4]
expected pivot: 1
actual: List length: 3 [2, 8, 4]
actual pivot: 1
partitionList() test 6, list of size 1 (and pivot value)
expected: List length: 2 [8, 4]
expected pivot: 0
actual: List length: 2 [8, 4]
6
actual pivot: 0
partitionList() test 7, list of size 1, pivot value is larger
expected: List length: 2 [8, 12]
expected pivot: 1
actual: List length: 2 [8, 12]
actual pivot: 1
partitionList() test 8 (sort by hand)
after first partition: List length: 5 [5, 3, 6, 7, 8]
after second partition (left): List length: 5 [3, 5, 6, 7, 8]
after third partition (right): List length: 5 [3, 5, 6, 7, 8]
expected: List length: 5 [3, 5, 6, 7, 8]
actual: List length: 5 [3, 5, 6, 7, 8]
Test quickSort() -----------------------------------------------
quickSort() test 1, sort list of size 1
expected: List length: 1 [8]
actual: List length: 1 [8]
quickSort() test 2, sort list of size 2 already ordered
expected: List length: 2 [3, 9]
actual: List length: 2 [3, 9]
quickSort() test 3, sort list of size 2 out of order
expected: List length: 2 [3, 9]
actual: List length: 2 [3, 9]
quickSort() test 4, sort odd sized list
expected: List length: 9 [2, 3, 4, 5, 5, 6, 7, 8, 9]
actual: List length: 9 [2, 3, 4, 5, 5, 6, 7, 8, 9]
quickSort() test 5, sort even sized list
expected: List length: 14 [2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 8, 9, 10, 11]
actual: List length: 14 [2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 8, 9, 10, 11]
quickSort() test 6, sort already sorted list
expected: List length: 17 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17]
actual: List length: 17 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17]
7
quickSort() test 7, sort a reversed list
expected: List length: 17 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17]
actual: List length: 17 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17]
quickSort() test 7, sort a reversed list
expected: List length: 14 [4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8]
actual: List length: 14 [4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8]
Assignment Submission
A MyLeoOnline submission folder has been created for this
assignment. You
should attach and upload your completed .cpp source files to the
submission
folder to complete this assignment. You really do not need to
give me the
assg-05.cpp file again, as I will have my own file with
additional tests of
your functions. However, please leave the names of the other
two files as
QuickSort.hpp and QuickSort.cpp when you submit them.
Requirements and Grading Rubrics
Program Execution, Output and Functional Requirements
1. Your program must compile, run and produce some sort of
output to
be graded. 0 if not satisfied.
2. (15 pts.) swapListValues() function implemented as specified
and
working.
3. (15 pts.) findAndSwapPivot() function implemented as
specified and
working.
4. (30 pts.) partitionList() function implemented as specified
and
working.
5. (30 pts.) quickSort() function implemented as specified and
working.
6. (5 pts.) All output is correct and matches the correct example
output.
7. (5 pts.) Followed class style guidelines, especially those
mentioned
below.
8
Program Style
Your programs must conform to the style and formatting
guidelines given
for this class. The following is a list of the guidelines that are
required for
the assignment to be submitted this week.
1. Most importantly, make sure you figure out how to set your
indentation
settings correctly. All programs must use 2 spaces for all
indentation
levels, and all indentation levels must be correctly indented.
Also all
tabs must be removed from files, and only 2 spaces used for
indentation.
2. A function header must be present for member functions you
define.
You must give a short description of the function, and document
all of
the input parameters to the function, as well as the return value
and
data type of the function if it returns a value for the member
functions,
just like for regular functions. However, setter and getter
methods do
not require function headers.
3. You should have a document header for your class. The class
header
document should give a description of the class. Also you
should doc-
ument all private member variables that the class manages in
the class
document header.
4. Do not include any statements (such as system("pause") or
inputting
a key from the user to continue) that are meant to keep the
terminal
from going away. Do not include any code that is specific to a
single
operating system, such as the system("pause") which is
Microsoft
Windows specific.
9
/**
* @author Jane Programmer
* @cwid 123 45 678
* @class COSC 2336, Spring 2019
* @ide Visual Studio Community 2017
* @date January 23, 2019
* @assg Assignment 05
*
* @description Assignment 05 Quick Sort
*/
#include "QuickSort.hpp"
// function implementations go here
/**
* @author Jane Programmer
* @cwid 123 45 678
* @class COSC 2336, Spring 2019
* @ide Visual Studio Community 2017
* @date January 23, 2019
* @assg Assignment 05
*
* @description Assignment 05 Quick Sort
*/
#ifndef _QUICKSORT_H_
#define _QUICKSORT_H_
// function prototypes go here
#endif // _QUICKSORT_H_
/**
* @author Jane Programmer
* @cwid 123 45 678
* @class COSC 2336, Spring 2019
* @ide Visual Studio Community 2017
* @date January 23, 2019
* @assg Assignment 05
*
* @description Assignment 05 Quick Sort
*/
#include <iostream>
#include <sstream>
#include <cassert>
#include <cstring>
#include "QuickSort.hpp"
using namespace std;
/** list to string
* Represent array as a string time, useful for output.
*
* @param list[] The list, an array of integers, to be converted
to
* a string.
* @param length The length of the list.
*
* @returns string Returns a string with a representation of the
list
* state and it contents.
*/
string tostring(int list[], int length)
{
ostringstream out;
out << "List length: " << length << " [";
// output first value, so we can remove , at end
if (length >= 1)
{
out << list[0];
}
// output each follow with a preceeding comma,
// which allows us to end list without trailing ,
for (int index = 1; index < length; index++)
{
out << ", " << list[index];
}
out << "]";
return out.str();
}
/** compare lists equal
* This function compares if the two lists (arrays of integers)
* given as parameters are equal or not. Result is boolean true
* if lists all have the same values, false otherwise.
*
* @param a[], b[] The lists, both of int and both the same size,
* that are to be compared.
* @param length The length of both of the lists.
*
* @returns bool Returns true if the lists are equal (have all the
* same values at all the same positions) and false otherwise.
*/
bool listsAreEqual(int a[], int b[], int length)
{
// compare each item in a and b
for (int index = 0; index < length; index++)
{
// as soon as we find 1 value that differs, the answer is false,
// the lists are not equal
if (a[index] != b[index])
{
return false;
}
}
// at this point we compared every value and they were all the
// same, thus the lists must be equal
return true;
}
/** main
* The main entry point for this program. Execution of this
program
* will begin with this main function.
*
* @param argc The command line argument count which is the
number of
* command line arguments provided by user when they
started
* the program.
* @param argv The command line arguments, an array of
character
* arrays.
*
* @returns An int value indicating program exit status.
Usually 0
* is returned to indicate normal exit and a non-zero value
* is returned to indicate an error condition.
*/
int main(int argc, char** argv)
{
// variables used for the function/unit tests
int length;
// test of swap function ---------------------------------------------
---
cout << "Test swapListValues() -----------------------------------
-------" << endl;
// basic test
length = 2;
int testvals1[] = {5, 10};
int expected1[] = {10, 5};
// swapListValues(testval1, 0, 1);
// cout << "swapListValues() test 1, swap 2 values" << endl
// << " expected: " << tostring(expected1, length) << endl
// << " actual : " << tostring(testvals1, length) << endl
// << endl;
// assert(listsAreEqual(testvals1, expected1, length));
// test if indexes are equal, important, should not cause any
change
length= 2;
int testvals2[] = {8, 6};
int expected2[] = {8, 6};
// swapListValues(testvals2, 1, 1);
// cout << "swapListValues() test 2, same index" << endl
// << " expected: " << tostring(expected2, length) << endl
// << " actual : " << tostring(testvals2, length) << endl
// << endl;
// assert(listsAreEqual(testvals2, expected2, length));
// more general tests, swap values in middle of list
length = 12;
int testvals3[] = {2, 7, 9, 3, 8, 4, 2, 9, 5, 8, 6, 1};
int expected3[] = {2, 7, 6, 3, 8, 4, 2, 9, 5, 8, 9, 1};
// swapListValues(testvals3, 2, 10);
// cout << "swapListValues() test 3, swap 2 values from inside
of list" << endl
// << " expected: " << tostring(expected3, length) << endl
// << " actual : " << tostring(testvals3, length) << endl
// << endl;
// assert(listsAreEqual(testvals3, expected3, length));
// swap back, reverse indexes in function call
// continuing to use testvals after previous test here
length = 12;
int expected4[] = {2, 7, 9, 3, 8, 4, 2, 9, 5, 8, 6, 1};
// swapListValues(testvals3, 10, 2);
// cout << "swapListValues() test 4, reverse the previous
swap" << endl
// << " expected: " << tostring(expected4, length) << endl
// << " actual : " << tostring(testvals3, length) << endl
// << endl;
// assert(listsAreEqual(testvals3, expected4, length));
// swap with index 0 on a bigger list
// still using previous testvals
length = 12;
int expected5[] = {4, 7, 9, 3, 8, 2, 2, 9, 5, 8, 6, 1};
// swapListValues(testvals3, 5, 0);
// cout << "swapListValues() test 5, swap with index 0" <<
endl
// << " expected: " << tostring(expected5, length) << endl
// << " actual : " << tostring(testvals3, length) << endl
// << endl;
// assert(listsAreEqual(testvals3, expected5, length));
// swap with last index on a bigger list
// still using previous testvals
length = 12;
int expected6[] = {4, 7, 9, 3, 8, 2, 1, 9, 5, 8, 6, 2};
// swapListValues(testvals3, 6, 11);
// cout << "swapListValues() test 6, swap with last index" <<
endl
// << " expected: " << tostring(expected6, length) << endl
// << " actual : " << tostring(testvals3, length) << endl
// << endl;
// assert(listsAreEqual(testvals3, expected6, length));
// test of findAndSwapPivot function -----------------------------
-------
cout << endl;
cout << "Test findAndSwapPivot() --------------------------------
--------" << endl;
int expectedPivotValue;
int actualPivotValue;
// basic test on a small list
length = 3;
int testvals7[] = {5, 3, 8};
int expected7[] = {5, 8, 3};
expectedPivotValue = 3;
// actualPivotValue = findAndSwapPivot(testvals7, 0, length-
1);
// cout << "findAndSwapPivot() test 1, basic test" << endl
// << " expected: " << tostring(expected7, length) <<
endl
// << " expected pivot: " << expectedPivotValue << endl
// << " actual: " << tostring(testvals7, length) <<
endl
// << " actual pivot: " << actualPivotValue << endl
// << endl;
// assert(listsAreEqual(testvals7, expected7, length));
// assert(actualPivotValue == expectedPivotValue);
// test on list of length 1, should work nothing will be done
length = 1;
int testvals8[] = {5};
int expected8[] = {5};
expectedPivotValue = 5;
// actualPivotValue = findAndSwapPivot(testvals8, 0, length-
1);
// cout << "findAndSwapPivot() test 2, test list size 1" << endl
// << " expected: " << tostring(expected8, length) <<
endl
// << " expected pivot: " << expectedPivotValue << endl
// << " actual: " << tostring(testvals8, length) <<
endl
// << " actual pivot: " << actualPivotValue << endl
// << endl;
// assert(listsAreEqual(testvals8, expected8, length));
// assert(actualPivotValue == expectedPivotValue);
// general test on a bigger list, even number of values
length = 10;
int testvals9[] = {5, 2, 8, 7, 3, 9, 1, 4, 5, 8};
int expected9[] = {5, 2, 8, 7, 8, 9, 1, 4, 5, 3};
expectedPivotValue = 3;
// actualPivotValue = findAndSwapPivot(testvals9, 0, length-
1);
// cout << "findAndSwapPivot() test 3, bigger list with even
number of values" << endl
// << " expected: " << tostring(expected9, length) <<
endl
// << " expected pivot: " << expectedPivotValue << endl
// << " actual: " << tostring(testvals9, length) <<
endl
// << " actual pivot: " << actualPivotValue << endl
// << endl;
// assert(listsAreEqual(testvals9, expected9, length));
// assert(actualPivotValue == expectedPivotValue);
// general test on a bigger list, odd number of values
length = 15;
int testvals10[] = {5, 2, 8, 7, 3, 9, 1, 42, 5, 8, 10, 11, 15, 22,
18};
int expected10[] = {5, 2, 8, 7, 3, 9, 1, 18, 5, 8, 10, 11, 15, 22,
42};
expectedPivotValue = 42;
// actualPivotValue = findAndSwapPivot(testvals10, 0, length-
1);
// cout << "findAndSwapPivot() test 4, bigger list with odd
number of values" << endl
// << " expected: " << tostring(expected10, length) <<
endl
// << " expected pivot: " << expectedPivotValue << endl
// << " actual: " << tostring(testvals10, length) <<
endl
// << " actual pivot: " << actualPivotValue << endl
// << endl;
// assert(listsAreEqual(testvals10, expected10, length));
// assert(actualPivotValue == expectedPivotValue);
// test of partitionList function -------------------------------------
--
cout << endl;
cout << "Test partitionList() ---------------------------------------
----" << endl;
int expectedPivotIndex;
int actualPivotIndex;
// work from general to more specific stress tests.
// most general test, partition list approximately in middle.
// note that pivot needs to be on the end of list, and for a list of
size n
// the valid indexes are from 0 to n-1, but the partition values
is at index
// n-1, so we call partitionList from indexes 0 to n-2
length = 10;
int testvals11[] = {8, 3, 7, 6, 9, 2, 4, 8, 1, 5};
int expected11[] = {1, 3, 4, 2, 9, 6, 7, 8, 8, 5};
expectedPivotIndex = 4;
// actualPivotIndex = partitionList(testvals11, 0, length-2,
testvals11[length-1]);
// cout << "partitionList() test 1, basic test" << endl
// << " expected: " << tostring(expected11, length) <<
endl
// << " expected pivot: " << expectedPivotIndex <<endl
// << " actual: " << tostring(testvals11, length) <<
endl
// << " actual pivot: " << actualPivotIndex << endl
// << endl;
// assert(listsAreEqual(testvals11, expected11, length));
// assert(actualPivotIndex == expectedPivotIndex);
// another general test, also this tests that all values == pivot
end
// up being moved to the right
length = 15;
int testvals12[] = {12, 7, 10, 19, 6, 18, 12, 15, 6, 10, 4, 13, 11,
17, 10};
int expected12[] = {4, 7, 6, 6, 19, 18, 12, 15, 10, 10, 12, 13,
11, 17, 10};
expectedPivotIndex = 4;
// actualPivotIndex = partitionList(testvals12, 0, length-2,
testvals12[length-1]);
// cout << "partitionList() test 2, bigger test and some
duplicates of the pivot" << endl
// << " expected: " << tostring(expected12, length) <<
endl
// << " expected pivot: " << expectedPivotIndex <<endl
// << " actual: " << tostring(testvals12, length) <<
endl
// << " actual pivot: " << actualPivotIndex << endl
// << endl;
// assert(listsAreEqual(testvals12, expected12, length));
// assert(actualPivotIndex == expectedPivotIndex);
// test everything is to left of pivot is working
length = 6;
int testvals13[] = {4, 3, 7, 6, 5, 10};
int expected13[] = {4, 3, 7, 6, 5, 10};
expectedPivotIndex = 5;
// actualPivotIndex = partitionList(testvals13, 0, length-2,
testvals13[length-1]);
// cout << "partitionList() test 3, everything is smaller than
pivot value" << endl
// << " expected: " << tostring(expected13, length) <<
endl
// << " expected pivot: " << expectedPivotIndex <<endl
// << " actual: " << tostring(testvals13, length) <<
endl
// << " actual pivot: " << actualPivotIndex << endl
// << endl;
// assert(listsAreEqual(testvals13, expected13, length));
// assert(actualPivotIndex == expectedPivotIndex);
// test everything is to right of pivot is working
length = 6;
int testvals14[] = {4, 3, 7, 6, 5, 2};
int expected14[] = {4, 3, 7, 6, 5, 2};
expectedPivotIndex = 0;
// actualPivotIndex = partitionList(testvals14, 0, length-2,
testvals14[length-1]);
// cout << "partitionList() test 4, everything is bigger than
pivot value" << endl
// << " expected: " << tostring(expected14, length) <<
endl
// << " expected pivot: " << expectedPivotIndex <<endl
// << " actual: " << tostring(testvals14, length) <<
endl
// << " actual pivot: " << actualPivotIndex << endl
// << endl;
// assert(listsAreEqual(testvals14, expected14, length));
// assert(actualPivotIndex == expectedPivotIndex);
// test small list that needs to be swapped
length = 3;
int testvals15[] = {8, 2, 4};
int expected15[] = {2, 8, 4};
expectedPivotIndex = 1;
// actualPivotIndex = partitionList(testvals15, 0, length-2,
testvals15[length-1]);
// cout << "partitionList() test 5, small list that needs a swap"
<< endl
// << " expected: " << tostring(expected15, length) <<
endl
// << " expected pivot: " << expectedPivotIndex <<endl
// << " actual: " << tostring(testvals15, length) <<
endl
// << " actual pivot: " << actualPivotIndex << endl
// << endl;
// assert(listsAreEqual(testvals15, expected15, length));
// assert(actualPivotIndex == expectedPivotIndex);
// list of only 1 item (besides pivot value), still needs to work
length = 2;
int testvals16[] = {8, 4};
int expected16[] = {8, 4};
expectedPivotIndex = 0;
// actualPivotIndex = partitionList(testvals16, 0, length-2,
testvals16[length-1]);
// cout << "partitionList() test 6, list of size 1 (and pivot
value)" << endl
// << " expected: " << tostring(expected16, length) <<
endl
// << " expected pivot: " << expectedPivotIndex <<endl
// << " actual: " << tostring(testvals16, length) <<
endl
// << " actual pivot: " << actualPivotIndex << endl
// << endl;
// assert(listsAreEqual(testvals16, expected16, length));
// assert(actualPivotIndex == expectedPivotIndex);
// list of only 1 item, but the pivot is bigger
length = 2;
int testvals17[] = {8, 12};
int expected17[] = {8, 12};
expectedPivotIndex = 1;
// actualPivotIndex = partitionList(testvals17, 0, length-2,
testvals17[length-1]);
// cout << "partitionList() test 7, list of size 1, pivot value is
larger" << endl
// << " expected: " << tostring(expected17, length) <<
endl
// << " expected pivot: " << expectedPivotIndex <<endl
// << " actual: " << tostring(testvals17, length) <<
endl
// << " actual pivot: " << actualPivotIndex << endl
// << endl;
// assert(listsAreEqual(testvals17, expected17, length));
// assert(actualPivotIndex == expectedPivotIndex);
// sort by hand using partitionList()
length = 5;
int testvals18[] = {7, 8, 3, 5, 6};
// actualPivotIndex = partitionList(testvals18, 0, length-2,
testvals18[length-1]);
// swapListValues(testvals18, length-1, actualPivotIndex);
// cout << "partitionList() test 8 (sort by hand)" << endl;
// cout << " after first partition: " << tostring(testvals18,
length) << endl;
// partition left
// actualPivotIndex = partitionList(testvals18, 0, 0,
testvals18[1]);
// swapListValues(testvals18, 1, actualPivotIndex);
// cout << " after second partition (left): " <<
tostring(testvals18, length) << endl;
// partition right
// actualPivotIndex = partitionList(testvals18, 3, 3,
testvals18[4]);
// swapListValues(testvals18, 4, actualPivotIndex);
// cout << " after third partition (right): " <<
tostring(testvals18, length) << endl;
int expected18[] = {3, 5, 6, 7, 8};
// cout << " expected: " << tostring(expected18, length) <<
endl
// << " actual: " << tostring(testvals18, length) <<
endl
// << endl;
// assert(listsAreEqual(testvals18, expected18, length));
// test of quickSort function ----------------------------------------
---
cout << endl;
cout << "Test quickSort() ------------------------------------------
-----" << endl;
// sort a list of size 1
length = 1;
int testvals19[] = {8};
int expected19[] = {8};
// quickSort(testvals19, 0, length-1);
// cout << "quickSort() test 1, sort list of size 1" << endl
// << " expected: " << tostring(expected19, length) <<
endl
// << " actual: " << tostring(testvals19, length) << endl
// << endl;
// assert(listsAreEqual(testvals19, expected19, length));
// sort a list of size 2, already in order
length = 2;
int testvals20[] = {3, 9};
int expected20[] = {3, 9};
// quickSort(testvals20, 0, length-1);
// cout << "quickSort() test 2, sort list of size 2 already
ordered" << endl
// << " expected: " << tostring(expected20, length) <<
endl
// << " actual: " << tostring(testvals20, length) << endl
// << endl;
// assert(listsAreEqual(testvals20, expected20, length));
// sort a list of size 2, not in order
length = 2;
int testvals21[] = {9, 3};
int expected21[] = {3, 9};
// quickSort(testvals21, 0, length-1);
// cout << "quickSort() test 3, sort list of size 2 out of order"
<< endl
// << " expected: " << tostring(expected21, length) <<
endl
// << " actual: " << tostring(testvals21, length) << endl
// << endl;
// assert(listsAreEqual(testvals21, expected21, length));
// sort an odd sized list
length = 9;
int testvals22[] = {9, 3, 2, 7, 5, 8, 6, 5, 4};
int expected22[] = {2, 3, 4, 5, 5, 6, 7, 8, 9};
// quickSort(testvals22, 0, length-1);
// cout << "quickSort() test 4, sort odd sized list" << endl
// << " expected: " << tostring(expected22, length) <<
endl
// << " actual: " << tostring(testvals22, length) << endl
// << endl;
// assert(listsAreEqual(testvals22, expected22, length));
// sort an even sized list
length = 14;
int testvals23[] = {9, 3, 2, 7, 5, 8, 6, 5, 4, 2, 10, 8, 11, 5};
int expected23[] = {2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 8, 9, 10, 11};
// quickSort(testvals23, 0, length-1);
// cout << "quickSort() test 5, sort even sized list" << endl
// << " expected: " << tostring(expected23, length) <<
endl
// << " actual: " << tostring(testvals23, length) << endl
// << endl;
// assert(listsAreEqual(testvals23, expected23, length))1;
// sort an already sorted list
length = 17;
int testvals24[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17};
int expected24[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17};
// quickSort(testvals24, 0, length-1);
// cout << "quickSort() test 6, sort already sorted list" << endl
// << " expected: " << tostring(expected24, length) <<
endl
// << " actual: " << tostring(testvals24, length) << endl
// << endl;
// assert(listsAreEqual(testvals24, expected24, length));
// sort a reversed list
length = 17;
int testvals25[] = {17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5,
4, 3, 2, 1};
int expected25[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17};
// quickSort(testvals25, 0, length-1);
// cout << "quickSort() test 7, sort a reversed list" << endl
// << " expected: " << tostring(expected25, length) <<
endl
// << " actual: " << tostring(testvals25, length) << endl
// << endl;
// assert(listsAreEqual(testvals25, expected25, length));
// sort a list with lots of duplicates
length = 14;
int testvals26[] = {4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8};
int expected26[] = {4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8};
// quickSort(testvals26, 0, length-1);
// cout << "quickSort() test 7, sort a reversed list" << endl
// << " expected: " << tostring(expected26, length) <<
endl
// << " actual: " << tostring(testvals26, length) << endl
// << endl;
// assert(listsAreEqual(testvals26, expected26, length));
// return 0 to indicate successful completion
return 0;
}

More Related Content

Similar to Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx

Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdfAmuthachenthiruK
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.jstimourian
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfMCCMOTOR
 
best first-sort
best first-sortbest first-sort
best first-sortRajendran
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdfMemeMiner
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptxskilljiolms
 
A Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort AlgorithmsA Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort AlgorithmsCSCJournals
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptxssuser8e50d8
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfinfo335653
 
Notes3
Notes3Notes3
Notes3hccit
 
Skipl List implementation - Part 1
Skipl List implementation - Part 1Skipl List implementation - Part 1
Skipl List implementation - Part 1Amrith Krishna
 
introduction_dst.pptx
introduction_dst.pptxintroduction_dst.pptx
introduction_dst.pptxHammadTariq51
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfclearvisioneyecareno
 

Similar to Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx (20)

Sortsearch
SortsearchSortsearch
Sortsearch
 
day 13.pptx
day 13.pptxday 13.pptx
day 13.pptx
 
Data Structures Design Notes.pdf
Data Structures Design Notes.pdfData Structures Design Notes.pdf
Data Structures Design Notes.pdf
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.js
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Python Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdfPython Unit 5 Questions n Notes.pdf
Python Unit 5 Questions n Notes.pdf
 
best first-sort
best first-sortbest first-sort
best first-sort
 
Data Structure.pdf
Data Structure.pdfData Structure.pdf
Data Structure.pdf
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptx
 
A Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort AlgorithmsA Survey of Adaptive QuickSort Algorithms
A Survey of Adaptive QuickSort Algorithms
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
 
Data Structures 3
Data Structures 3Data Structures 3
Data Structures 3
 
Notes3
Notes3Notes3
Notes3
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
Skipl List implementation - Part 1
Skipl List implementation - Part 1Skipl List implementation - Part 1
Skipl List implementation - Part 1
 
introduction_dst.pptx
introduction_dst.pptxintroduction_dst.pptx
introduction_dst.pptx
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
 
Loops_in_Rv1.2b
Loops_in_Rv1.2bLoops_in_Rv1.2b
Loops_in_Rv1.2b
 

More from festockton

Learning ResourcesRequired ReadingsToseland, R. W., & Ri.docx
Learning ResourcesRequired ReadingsToseland, R. W., & Ri.docxLearning ResourcesRequired ReadingsToseland, R. W., & Ri.docx
Learning ResourcesRequired ReadingsToseland, R. W., & Ri.docxfestockton
 
LeamosEscribamos Completa el párrafo con las formas correctas de lo.docx
LeamosEscribamos Completa el párrafo con las formas correctas de lo.docxLeamosEscribamos Completa el párrafo con las formas correctas de lo.docx
LeamosEscribamos Completa el párrafo con las formas correctas de lo.docxfestockton
 
Leadership via vision is necessary for success. Discuss in detail .docx
Leadership via vision is necessary for success. Discuss in detail .docxLeadership via vision is necessary for success. Discuss in detail .docx
Leadership via vision is necessary for success. Discuss in detail .docxfestockton
 
Learning about Language by Observing and ListeningThe real.docx
Learning about Language by Observing and ListeningThe real.docxLearning about Language by Observing and ListeningThe real.docx
Learning about Language by Observing and ListeningThe real.docxfestockton
 
Learning Accomplishment Profile-Diagnostic Spanish Language Edit.docx
Learning Accomplishment Profile-Diagnostic Spanish Language Edit.docxLearning Accomplishment Profile-Diagnostic Spanish Language Edit.docx
Learning Accomplishment Profile-Diagnostic Spanish Language Edit.docxfestockton
 
Learning about Language by Observing and ListeningThe real voy.docx
Learning about Language by Observing and ListeningThe real voy.docxLearning about Language by Observing and ListeningThe real voy.docx
Learning about Language by Observing and ListeningThe real voy.docxfestockton
 
LEARNING OUTCOMES1. Have knowledge and understanding of the pri.docx
LEARNING OUTCOMES1. Have knowledge and understanding of the pri.docxLEARNING OUTCOMES1. Have knowledge and understanding of the pri.docx
LEARNING OUTCOMES1. Have knowledge and understanding of the pri.docxfestockton
 
Leadership Style What do people do when they are leadingAssignme.docx
Leadership Style What do people do when they are leadingAssignme.docxLeadership Style What do people do when they are leadingAssignme.docx
Leadership Style What do people do when they are leadingAssignme.docxfestockton
 
Leadership Throughout HistoryHistory is filled with tales of leade.docx
Leadership Throughout HistoryHistory is filled with tales of leade.docxLeadership Throughout HistoryHistory is filled with tales of leade.docx
Leadership Throughout HistoryHistory is filled with tales of leade.docxfestockton
 
Lean Inventory Management1. Why do you think lean inventory manage.docx
Lean Inventory Management1. Why do you think lean inventory manage.docxLean Inventory Management1. Why do you think lean inventory manage.docx
Lean Inventory Management1. Why do you think lean inventory manage.docxfestockton
 
Leadership varies widely by culture and personality. An internationa.docx
Leadership varies widely by culture and personality. An internationa.docxLeadership varies widely by culture and personality. An internationa.docx
Leadership varies widely by culture and personality. An internationa.docxfestockton
 
Leadership is the ability to influence people toward the attainment .docx
Leadership is the ability to influence people toward the attainment .docxLeadership is the ability to influence people toward the attainment .docx
Leadership is the ability to influence people toward the attainment .docxfestockton
 
Lawday. Court of Brightwaltham holden on Monday next after Ascension.docx
Lawday. Court of Brightwaltham holden on Monday next after Ascension.docxLawday. Court of Brightwaltham holden on Monday next after Ascension.docx
Lawday. Court of Brightwaltham holden on Monday next after Ascension.docxfestockton
 
law43665_fm_i-xx i 010719 1032 AMStakeholders, Eth.docx
law43665_fm_i-xx i 010719  1032 AMStakeholders, Eth.docxlaw43665_fm_i-xx i 010719  1032 AMStakeholders, Eth.docx
law43665_fm_i-xx i 010719 1032 AMStakeholders, Eth.docxfestockton
 
Leaders face many hurdles when leading in multiple countries. There .docx
Leaders face many hurdles when leading in multiple countries. There .docxLeaders face many hurdles when leading in multiple countries. There .docx
Leaders face many hurdles when leading in multiple countries. There .docxfestockton
 
Last year Angelina Jolie had a double mastectomy because of re.docx
Last year Angelina Jolie had a double mastectomy because of re.docxLast year Angelina Jolie had a double mastectomy because of re.docx
Last year Angelina Jolie had a double mastectomy because of re.docxfestockton
 
Leaders face many hurdles when leading in multiple countries. Ther.docx
Leaders face many hurdles when leading in multiple countries. Ther.docxLeaders face many hurdles when leading in multiple countries. Ther.docx
Leaders face many hurdles when leading in multiple countries. Ther.docxfestockton
 
Leaders today must be able to create a compelling vision for the org.docx
Leaders today must be able to create a compelling vision for the org.docxLeaders today must be able to create a compelling vision for the org.docx
Leaders today must be able to create a compelling vision for the org.docxfestockton
 
Law enforcement professionals and investigators use digital fore.docx
Law enforcement professionals and investigators use digital fore.docxLaw enforcement professionals and investigators use digital fore.docx
Law enforcement professionals and investigators use digital fore.docxfestockton
 
LAW and Economics 4 questionsLaw And EconomicsTextsCoote.docx
LAW and Economics 4 questionsLaw And EconomicsTextsCoote.docxLAW and Economics 4 questionsLaw And EconomicsTextsCoote.docx
LAW and Economics 4 questionsLaw And EconomicsTextsCoote.docxfestockton
 

More from festockton (20)

Learning ResourcesRequired ReadingsToseland, R. W., & Ri.docx
Learning ResourcesRequired ReadingsToseland, R. W., & Ri.docxLearning ResourcesRequired ReadingsToseland, R. W., & Ri.docx
Learning ResourcesRequired ReadingsToseland, R. W., & Ri.docx
 
LeamosEscribamos Completa el párrafo con las formas correctas de lo.docx
LeamosEscribamos Completa el párrafo con las formas correctas de lo.docxLeamosEscribamos Completa el párrafo con las formas correctas de lo.docx
LeamosEscribamos Completa el párrafo con las formas correctas de lo.docx
 
Leadership via vision is necessary for success. Discuss in detail .docx
Leadership via vision is necessary for success. Discuss in detail .docxLeadership via vision is necessary for success. Discuss in detail .docx
Leadership via vision is necessary for success. Discuss in detail .docx
 
Learning about Language by Observing and ListeningThe real.docx
Learning about Language by Observing and ListeningThe real.docxLearning about Language by Observing and ListeningThe real.docx
Learning about Language by Observing and ListeningThe real.docx
 
Learning Accomplishment Profile-Diagnostic Spanish Language Edit.docx
Learning Accomplishment Profile-Diagnostic Spanish Language Edit.docxLearning Accomplishment Profile-Diagnostic Spanish Language Edit.docx
Learning Accomplishment Profile-Diagnostic Spanish Language Edit.docx
 
Learning about Language by Observing and ListeningThe real voy.docx
Learning about Language by Observing and ListeningThe real voy.docxLearning about Language by Observing and ListeningThe real voy.docx
Learning about Language by Observing and ListeningThe real voy.docx
 
LEARNING OUTCOMES1. Have knowledge and understanding of the pri.docx
LEARNING OUTCOMES1. Have knowledge and understanding of the pri.docxLEARNING OUTCOMES1. Have knowledge and understanding of the pri.docx
LEARNING OUTCOMES1. Have knowledge and understanding of the pri.docx
 
Leadership Style What do people do when they are leadingAssignme.docx
Leadership Style What do people do when they are leadingAssignme.docxLeadership Style What do people do when they are leadingAssignme.docx
Leadership Style What do people do when they are leadingAssignme.docx
 
Leadership Throughout HistoryHistory is filled with tales of leade.docx
Leadership Throughout HistoryHistory is filled with tales of leade.docxLeadership Throughout HistoryHistory is filled with tales of leade.docx
Leadership Throughout HistoryHistory is filled with tales of leade.docx
 
Lean Inventory Management1. Why do you think lean inventory manage.docx
Lean Inventory Management1. Why do you think lean inventory manage.docxLean Inventory Management1. Why do you think lean inventory manage.docx
Lean Inventory Management1. Why do you think lean inventory manage.docx
 
Leadership varies widely by culture and personality. An internationa.docx
Leadership varies widely by culture and personality. An internationa.docxLeadership varies widely by culture and personality. An internationa.docx
Leadership varies widely by culture and personality. An internationa.docx
 
Leadership is the ability to influence people toward the attainment .docx
Leadership is the ability to influence people toward the attainment .docxLeadership is the ability to influence people toward the attainment .docx
Leadership is the ability to influence people toward the attainment .docx
 
Lawday. Court of Brightwaltham holden on Monday next after Ascension.docx
Lawday. Court of Brightwaltham holden on Monday next after Ascension.docxLawday. Court of Brightwaltham holden on Monday next after Ascension.docx
Lawday. Court of Brightwaltham holden on Monday next after Ascension.docx
 
law43665_fm_i-xx i 010719 1032 AMStakeholders, Eth.docx
law43665_fm_i-xx i 010719  1032 AMStakeholders, Eth.docxlaw43665_fm_i-xx i 010719  1032 AMStakeholders, Eth.docx
law43665_fm_i-xx i 010719 1032 AMStakeholders, Eth.docx
 
Leaders face many hurdles when leading in multiple countries. There .docx
Leaders face many hurdles when leading in multiple countries. There .docxLeaders face many hurdles when leading in multiple countries. There .docx
Leaders face many hurdles when leading in multiple countries. There .docx
 
Last year Angelina Jolie had a double mastectomy because of re.docx
Last year Angelina Jolie had a double mastectomy because of re.docxLast year Angelina Jolie had a double mastectomy because of re.docx
Last year Angelina Jolie had a double mastectomy because of re.docx
 
Leaders face many hurdles when leading in multiple countries. Ther.docx
Leaders face many hurdles when leading in multiple countries. Ther.docxLeaders face many hurdles when leading in multiple countries. Ther.docx
Leaders face many hurdles when leading in multiple countries. Ther.docx
 
Leaders today must be able to create a compelling vision for the org.docx
Leaders today must be able to create a compelling vision for the org.docxLeaders today must be able to create a compelling vision for the org.docx
Leaders today must be able to create a compelling vision for the org.docx
 
Law enforcement professionals and investigators use digital fore.docx
Law enforcement professionals and investigators use digital fore.docxLaw enforcement professionals and investigators use digital fore.docx
Law enforcement professionals and investigators use digital fore.docx
 
LAW and Economics 4 questionsLaw And EconomicsTextsCoote.docx
LAW and Economics 4 questionsLaw And EconomicsTextsCoote.docxLAW and Economics 4 questionsLaw And EconomicsTextsCoote.docx
LAW and Economics 4 questionsLaw And EconomicsTextsCoote.docx
 

Recently uploaded

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Recently uploaded (20)

Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 

Assg 05 QuicksortCOSC 2336 Data StructuresObjectives.docx

  • 1. Assg 05: Quicksort COSC 2336 Data Structures Objectives • Practice writing functions • Practice writing recursive functions. • Learn about Analysis of algorithms and O(n log n) sorts Description In this assignment we will be implementing one of the most popular sorting algorithms used in libraries (like the C++ STL library, and the UNIX qsort function) to provide basic sorting abilities, the Quicksort algorithm. I would recommend that you at least read section 7.5 from our supplemental Shaffer textbook on Quicksort, if not sections 7.1-7.5 talking about three well known O(n log n) sorting algorithms, and the 3 O(n2) algorithms we discussed last week. Quicksort, when properly implemented, is very attractive because it pro- vides a way to do a fast sort completely in-place (without having to allocate
  • 2. additional memory to do the sort, beyond a single value needed when swap- ping two values in the list being sorted). In the worst case, Quicksort is actually O(n2), no better than bubble sort. But this worse case only occurs when every pivot selected is the wort possible, and does not divide the list at all. This is very unlikely to happen, unless you know how the pivot is selected, and specifically design the input list to always choose the worst possible pivot. On average the cost of Quicksort is O(n log n), and it is usually very likely that average case performance will result when lists to be sorted are relatively random. The most direct implementation of Quicksort is as a recursive algorithm. Quicksort is an example of a divide and conquer approach to solving the problem of sorting the list. We are given a list of items, A and indexes left 1 and right that indicate a sub-portion of the list to be sorted. left and right indicate the actual indexes, so if the list is a regular C array of integers, and the array is of size 10 int left;
  • 3. int right; const inst SIZE = 10; int A[size]; Then to sort the whole list we set left = 0 and right = 9 to initially call the Quicksort function: left = 0; right = size-1; quicksort(A, left, right); Conceptually the steps of the Quicksort algorithm are as follows: 1. if list size is 0 or 1 (left <= right) return (lists of this size are sorted by definition). 2. Choose a pivot value and swap the pivot value to the end of the list swap(pivotIndex, right) 3. Partition the list. Partitioning means all values less than the pivot value should end up on the left of the list, and all values greater will be on the right. The first index k where a value >= to the pivot value is at indicates the new left and right side sub-lists. 4. Swap the pivot value to its correct position k swap(k, right) 5. Recursively call Quicksort on the new left and right sub-lists • quicksort(A, left, k-1)
  • 4. • quicksort(A, k+1, right) Most of the real work happens in the function/code to partition the list. The partitioning of the list, for Quicksort to be an in-place sort, must work by swapping values in-place in the list of items. All values smaller than the pivot value must end up on the left side, and all values greater on the right. The algorithm to partition the list is traditionally done with these steps: 2 1. do a linear search from the left of list, stop at first value on left that is >= pivot 2. do a linear search from the right of list, stop at first value that is < than the pivot. 3. swap(left, right) assuming you were incrementing left and decre- menting right to point to indexes of values that are on wrong sides 4. if left < right goto 1 Conceptually we search from both ends of the list, and when we find values that are on wrong sides with respect to the pivot value,
  • 5. we swap them. Eventually the search from both ends will meet somewhere. The location where they meet should be the index of the first value that is >= to the pivot (going from the left side of the list). This is the index where the pivot value should actually go in the list, because all values before this are smaller than the pivot, and all values at or after are greater than the pivot. Thus at the end of partitioning the list on some pivot value, we are able to swap 1 value each time to its correct final position in the list. But the values to the left and right will not be sorted, thus we call Quicksort recursively on these sub-lists to get them sorted. In order to help you implement your own version of Quicksort, we have broken the problem down into useful sub-functions. If you implement the sub-functions as specified, in the order given, the final implementation of the Quicksort function is relatively straight forward, using these smaller func- tions. For this assignment you need to perform the following tasks. 1. Write a function called swapListValues(). This functions takes an array of integers as its first parameter, and two indexes (the left and right indexes). This function does not return a value explicitly.
  • 6. Recall arrays are passed by reference. As the name implies, the two values in the array at the indicated left and right indexes are to be swapped, and since the array is passed by reference, after returning they will be swapped for the caller of this function. 2. Write a function called findAndSwapPivot(). This function takes the same 3 parameters, an array of integers, and two indexes indicating the left and right sides of a sub-portion of the list. The function should find the value in the middle of the left and right ends, which will be chosen as the pivot. The function should use the previous swapListValues() 3 function to swap the chosen pivot value to the end of the list of integers. This function returns a value. This is different from how the textbook implements the find pivot function. Our function should return the actual pivot value that was chosen (not the pivotIndex, which we know should be the last index of the sub-list after calling this function). 3. Write a function called partitionList(). This will implement
  • 7. the al- gorithm described preciously. This functions takes the 3 same param- eters for the previous functions, an integer array, and left and right indexes for the sub-portion of the list we are currently partitioning. In addition, this function takes a fourth parameter, the pivot value). This function should make use of the swapListValues() function de- fined previously when swapping values in-place in the list of integers. When this function is called, the pivot has been swapped to the end of the sub-portion of the list, so the right index will be one less than this. This function needs to correctly return the index, described as k above, where the pivot value should actually go. At the end, the location where the left search and right search meet will be this index, the final location found for the pivot value. 4. Finally write a function called quickSort() using the described algo- rithm above. This function will use all of the 3 previous functions to do its work. If implemented correctly, there is almost nothing to be done in this function besides calling the other 3 functions, and recursively calling itself (except to check for the base case of your recursion).
  • 8. You will again be given 3 starting template files like before, an assg- 05.cpp file of tests of your code, and a QuickSort.hpp and QuickSort.cpp header and implementation file. As before, you should practice incremental development, and uncomment the tests in the assg-05.cpp file one at a time, and implement the functions in the order specified. If you implement your code correctly and uncomment all of the tests, you should get the following correct output: Test swapListValues() ------------------------------------------ swapListValues() test 1, swap 2 values expected: List length: 2 [10, 5] actual : List length: 2 [10, 5] swapListValues() test 2, same index expected: List length: 2 [8, 6] 4 actual : List length: 2 [8, 6] swapListValues() test 3, swap 2 values from inside of list expected: List length: 12 [2, 7, 6, 3, 8, 4, 2, 9, 5, 8, 9, 1] actual : List length: 12 [2, 7, 6, 3, 8, 4, 2, 9, 5, 8, 9, 1] swapListValues() test 4, reverse the previous swap expected: List length: 12 [2, 7, 9, 3, 8, 4, 2, 9, 5, 8, 6, 1] actual : List length: 12 [2, 7, 9, 3, 8, 4, 2, 9, 5, 8, 6, 1]
  • 9. swapListValues() test 5, swap with index 0 expected: List length: 12 [4, 7, 9, 3, 8, 2, 2, 9, 5, 8, 6, 1] actual : List length: 12 [4, 7, 9, 3, 8, 2, 2, 9, 5, 8, 6, 1] swapListValues() test 6, swap with last index expected: List length: 12 [4, 7, 9, 3, 8, 2, 1, 9, 5, 8, 6, 2] actual : List length: 12 [4, 7, 9, 3, 8, 2, 1, 9, 5, 8, 6, 2] Test findAndSwapPivot() ---------------------------------------- findAndSwapPivot() test 1, basic test expected: List length: 3 [5, 8, 3] expected pivot: 3 actual: List length: 3 [5, 8, 3] actual pivot: 3 findAndSwapPivot() test 2, test list size 1 expected: List length: 1 [5] expected pivot: 5 actual: List length: 1 [5] actual pivot: 5 findAndSwapPivot() test 3, bigger list with even number of values expected: List length: 10 [5, 2, 8, 7, 8, 9, 1, 4, 5, 3] expected pivot: 3 actual: List length: 10 [5, 2, 8, 7, 8, 9, 1, 4, 5, 3] actual pivot: 3 findAndSwapPivot() test 4, bigger list with odd number of
  • 10. values expected: List length: 15 [5, 2, 8, 7, 3, 9, 1, 18, 5, 8, 10, 11, 15, 22, 42] 5 expected pivot: 42 actual: List length: 15 [5, 2, 8, 7, 3, 9, 1, 18, 5, 8, 10, 11, 15, 22, 42] actual pivot: 42 Test partitionList() ------------------------------------------- partitionList() test 1, basic test expected: List length: 10 [1, 3, 4, 2, 9, 6, 7, 8, 8, 5] expected pivot: 4 actual: List length: 10 [1, 3, 4, 2, 9, 6, 7, 8, 8, 5] actual pivot: 4 partitionList() test 2, bigger test and some duplicates of the pivot expected: List length: 15 [4, 7, 6, 6, 19, 18, 12, 15, 10, 10, 12, 13, 11, 17, 10] expected pivot: 4 actual: List length: 15 [4, 7, 6, 6, 19, 18, 12, 15, 10, 10, 12, 13, 11, 17, 10] actual pivot: 4 partitionList() test 3, everything is smaller than pivot value expected: List length: 6 [4, 3, 7, 6, 5, 10]
  • 11. expected pivot: 5 actual: List length: 6 [4, 3, 7, 6, 5, 10] actual pivot: 5 partitionList() test 4, everything is bigger than pivot value expected: List length: 6 [4, 3, 7, 6, 5, 2] expected pivot: 0 actual: List length: 6 [4, 3, 7, 6, 5, 2] actual pivot: 0 partitionList() test 5, small list that needs a swap expected: List length: 3 [2, 8, 4] expected pivot: 1 actual: List length: 3 [2, 8, 4] actual pivot: 1 partitionList() test 6, list of size 1 (and pivot value) expected: List length: 2 [8, 4] expected pivot: 0 actual: List length: 2 [8, 4] 6 actual pivot: 0 partitionList() test 7, list of size 1, pivot value is larger expected: List length: 2 [8, 12]
  • 12. expected pivot: 1 actual: List length: 2 [8, 12] actual pivot: 1 partitionList() test 8 (sort by hand) after first partition: List length: 5 [5, 3, 6, 7, 8] after second partition (left): List length: 5 [3, 5, 6, 7, 8] after third partition (right): List length: 5 [3, 5, 6, 7, 8] expected: List length: 5 [3, 5, 6, 7, 8] actual: List length: 5 [3, 5, 6, 7, 8] Test quickSort() ----------------------------------------------- quickSort() test 1, sort list of size 1 expected: List length: 1 [8] actual: List length: 1 [8] quickSort() test 2, sort list of size 2 already ordered expected: List length: 2 [3, 9] actual: List length: 2 [3, 9] quickSort() test 3, sort list of size 2 out of order expected: List length: 2 [3, 9] actual: List length: 2 [3, 9] quickSort() test 4, sort odd sized list expected: List length: 9 [2, 3, 4, 5, 5, 6, 7, 8, 9] actual: List length: 9 [2, 3, 4, 5, 5, 6, 7, 8, 9] quickSort() test 5, sort even sized list
  • 13. expected: List length: 14 [2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 8, 9, 10, 11] actual: List length: 14 [2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 8, 9, 10, 11] quickSort() test 6, sort already sorted list expected: List length: 17 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] actual: List length: 17 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] 7 quickSort() test 7, sort a reversed list expected: List length: 17 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] actual: List length: 17 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] quickSort() test 7, sort a reversed list expected: List length: 14 [4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8] actual: List length: 14 [4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8] Assignment Submission A MyLeoOnline submission folder has been created for this assignment. You should attach and upload your completed .cpp source files to the submission folder to complete this assignment. You really do not need to give me the assg-05.cpp file again, as I will have my own file with
  • 14. additional tests of your functions. However, please leave the names of the other two files as QuickSort.hpp and QuickSort.cpp when you submit them. Requirements and Grading Rubrics Program Execution, Output and Functional Requirements 1. Your program must compile, run and produce some sort of output to be graded. 0 if not satisfied. 2. (15 pts.) swapListValues() function implemented as specified and working. 3. (15 pts.) findAndSwapPivot() function implemented as specified and working. 4. (30 pts.) partitionList() function implemented as specified and working. 5. (30 pts.) quickSort() function implemented as specified and working. 6. (5 pts.) All output is correct and matches the correct example output. 7. (5 pts.) Followed class style guidelines, especially those mentioned below. 8
  • 15. Program Style Your programs must conform to the style and formatting guidelines given for this class. The following is a list of the guidelines that are required for the assignment to be submitted this week. 1. Most importantly, make sure you figure out how to set your indentation settings correctly. All programs must use 2 spaces for all indentation levels, and all indentation levels must be correctly indented. Also all tabs must be removed from files, and only 2 spaces used for indentation. 2. A function header must be present for member functions you define. You must give a short description of the function, and document all of the input parameters to the function, as well as the return value and data type of the function if it returns a value for the member functions, just like for regular functions. However, setter and getter methods do not require function headers. 3. You should have a document header for your class. The class header document should give a description of the class. Also you should doc-
  • 16. ument all private member variables that the class manages in the class document header. 4. Do not include any statements (such as system("pause") or inputting a key from the user to continue) that are meant to keep the terminal from going away. Do not include any code that is specific to a single operating system, such as the system("pause") which is Microsoft Windows specific. 9 /** * @author Jane Programmer * @cwid 123 45 678 * @class COSC 2336, Spring 2019 * @ide Visual Studio Community 2017 * @date January 23, 2019 * @assg Assignment 05 * * @description Assignment 05 Quick Sort */ #include "QuickSort.hpp" // function implementations go here /** * @author Jane Programmer * @cwid 123 45 678 * @class COSC 2336, Spring 2019
  • 17. * @ide Visual Studio Community 2017 * @date January 23, 2019 * @assg Assignment 05 * * @description Assignment 05 Quick Sort */ #ifndef _QUICKSORT_H_ #define _QUICKSORT_H_ // function prototypes go here #endif // _QUICKSORT_H_ /** * @author Jane Programmer * @cwid 123 45 678 * @class COSC 2336, Spring 2019 * @ide Visual Studio Community 2017 * @date January 23, 2019 * @assg Assignment 05 * * @description Assignment 05 Quick Sort */ #include <iostream> #include <sstream> #include <cassert> #include <cstring> #include "QuickSort.hpp" using namespace std; /** list to string * Represent array as a string time, useful for output.
  • 18. * * @param list[] The list, an array of integers, to be converted to * a string. * @param length The length of the list. * * @returns string Returns a string with a representation of the list * state and it contents. */ string tostring(int list[], int length) { ostringstream out; out << "List length: " << length << " ["; // output first value, so we can remove , at end if (length >= 1) { out << list[0]; } // output each follow with a preceeding comma, // which allows us to end list without trailing , for (int index = 1; index < length; index++) { out << ", " << list[index]; } out << "]"; return out.str(); } /** compare lists equal
  • 19. * This function compares if the two lists (arrays of integers) * given as parameters are equal or not. Result is boolean true * if lists all have the same values, false otherwise. * * @param a[], b[] The lists, both of int and both the same size, * that are to be compared. * @param length The length of both of the lists. * * @returns bool Returns true if the lists are equal (have all the * same values at all the same positions) and false otherwise. */ bool listsAreEqual(int a[], int b[], int length) { // compare each item in a and b for (int index = 0; index < length; index++) { // as soon as we find 1 value that differs, the answer is false, // the lists are not equal if (a[index] != b[index]) { return false; } } // at this point we compared every value and they were all the // same, thus the lists must be equal return true; } /** main * The main entry point for this program. Execution of this program * will begin with this main function. * * @param argc The command line argument count which is the
  • 20. number of * command line arguments provided by user when they started * the program. * @param argv The command line arguments, an array of character * arrays. * * @returns An int value indicating program exit status. Usually 0 * is returned to indicate normal exit and a non-zero value * is returned to indicate an error condition. */ int main(int argc, char** argv) { // variables used for the function/unit tests int length; // test of swap function --------------------------------------------- --- cout << "Test swapListValues() ----------------------------------- -------" << endl; // basic test length = 2; int testvals1[] = {5, 10}; int expected1[] = {10, 5}; // swapListValues(testval1, 0, 1); // cout << "swapListValues() test 1, swap 2 values" << endl // << " expected: " << tostring(expected1, length) << endl // << " actual : " << tostring(testvals1, length) << endl // << endl; // assert(listsAreEqual(testvals1, expected1, length)); // test if indexes are equal, important, should not cause any change
  • 21. length= 2; int testvals2[] = {8, 6}; int expected2[] = {8, 6}; // swapListValues(testvals2, 1, 1); // cout << "swapListValues() test 2, same index" << endl // << " expected: " << tostring(expected2, length) << endl // << " actual : " << tostring(testvals2, length) << endl // << endl; // assert(listsAreEqual(testvals2, expected2, length)); // more general tests, swap values in middle of list length = 12; int testvals3[] = {2, 7, 9, 3, 8, 4, 2, 9, 5, 8, 6, 1}; int expected3[] = {2, 7, 6, 3, 8, 4, 2, 9, 5, 8, 9, 1}; // swapListValues(testvals3, 2, 10); // cout << "swapListValues() test 3, swap 2 values from inside of list" << endl // << " expected: " << tostring(expected3, length) << endl // << " actual : " << tostring(testvals3, length) << endl // << endl; // assert(listsAreEqual(testvals3, expected3, length)); // swap back, reverse indexes in function call // continuing to use testvals after previous test here length = 12; int expected4[] = {2, 7, 9, 3, 8, 4, 2, 9, 5, 8, 6, 1}; // swapListValues(testvals3, 10, 2); // cout << "swapListValues() test 4, reverse the previous swap" << endl // << " expected: " << tostring(expected4, length) << endl // << " actual : " << tostring(testvals3, length) << endl // << endl; // assert(listsAreEqual(testvals3, expected4, length)); // swap with index 0 on a bigger list // still using previous testvals
  • 22. length = 12; int expected5[] = {4, 7, 9, 3, 8, 2, 2, 9, 5, 8, 6, 1}; // swapListValues(testvals3, 5, 0); // cout << "swapListValues() test 5, swap with index 0" << endl // << " expected: " << tostring(expected5, length) << endl // << " actual : " << tostring(testvals3, length) << endl // << endl; // assert(listsAreEqual(testvals3, expected5, length)); // swap with last index on a bigger list // still using previous testvals length = 12; int expected6[] = {4, 7, 9, 3, 8, 2, 1, 9, 5, 8, 6, 2}; // swapListValues(testvals3, 6, 11); // cout << "swapListValues() test 6, swap with last index" << endl // << " expected: " << tostring(expected6, length) << endl // << " actual : " << tostring(testvals3, length) << endl // << endl; // assert(listsAreEqual(testvals3, expected6, length)); // test of findAndSwapPivot function ----------------------------- ------- cout << endl; cout << "Test findAndSwapPivot() -------------------------------- --------" << endl; int expectedPivotValue; int actualPivotValue; // basic test on a small list length = 3; int testvals7[] = {5, 3, 8}; int expected7[] = {5, 8, 3}; expectedPivotValue = 3;
  • 23. // actualPivotValue = findAndSwapPivot(testvals7, 0, length- 1); // cout << "findAndSwapPivot() test 1, basic test" << endl // << " expected: " << tostring(expected7, length) << endl // << " expected pivot: " << expectedPivotValue << endl // << " actual: " << tostring(testvals7, length) << endl // << " actual pivot: " << actualPivotValue << endl // << endl; // assert(listsAreEqual(testvals7, expected7, length)); // assert(actualPivotValue == expectedPivotValue); // test on list of length 1, should work nothing will be done length = 1; int testvals8[] = {5}; int expected8[] = {5}; expectedPivotValue = 5; // actualPivotValue = findAndSwapPivot(testvals8, 0, length- 1); // cout << "findAndSwapPivot() test 2, test list size 1" << endl // << " expected: " << tostring(expected8, length) << endl // << " expected pivot: " << expectedPivotValue << endl // << " actual: " << tostring(testvals8, length) << endl // << " actual pivot: " << actualPivotValue << endl // << endl; // assert(listsAreEqual(testvals8, expected8, length)); // assert(actualPivotValue == expectedPivotValue); // general test on a bigger list, even number of values length = 10; int testvals9[] = {5, 2, 8, 7, 3, 9, 1, 4, 5, 8}; int expected9[] = {5, 2, 8, 7, 8, 9, 1, 4, 5, 3}; expectedPivotValue = 3;
  • 24. // actualPivotValue = findAndSwapPivot(testvals9, 0, length- 1); // cout << "findAndSwapPivot() test 3, bigger list with even number of values" << endl // << " expected: " << tostring(expected9, length) << endl // << " expected pivot: " << expectedPivotValue << endl // << " actual: " << tostring(testvals9, length) << endl // << " actual pivot: " << actualPivotValue << endl // << endl; // assert(listsAreEqual(testvals9, expected9, length)); // assert(actualPivotValue == expectedPivotValue); // general test on a bigger list, odd number of values length = 15; int testvals10[] = {5, 2, 8, 7, 3, 9, 1, 42, 5, 8, 10, 11, 15, 22, 18}; int expected10[] = {5, 2, 8, 7, 3, 9, 1, 18, 5, 8, 10, 11, 15, 22, 42}; expectedPivotValue = 42; // actualPivotValue = findAndSwapPivot(testvals10, 0, length- 1); // cout << "findAndSwapPivot() test 4, bigger list with odd number of values" << endl // << " expected: " << tostring(expected10, length) << endl // << " expected pivot: " << expectedPivotValue << endl // << " actual: " << tostring(testvals10, length) << endl // << " actual pivot: " << actualPivotValue << endl // << endl; // assert(listsAreEqual(testvals10, expected10, length)); // assert(actualPivotValue == expectedPivotValue);
  • 25. // test of partitionList function ------------------------------------- -- cout << endl; cout << "Test partitionList() --------------------------------------- ----" << endl; int expectedPivotIndex; int actualPivotIndex; // work from general to more specific stress tests. // most general test, partition list approximately in middle. // note that pivot needs to be on the end of list, and for a list of size n // the valid indexes are from 0 to n-1, but the partition values is at index // n-1, so we call partitionList from indexes 0 to n-2 length = 10; int testvals11[] = {8, 3, 7, 6, 9, 2, 4, 8, 1, 5}; int expected11[] = {1, 3, 4, 2, 9, 6, 7, 8, 8, 5}; expectedPivotIndex = 4; // actualPivotIndex = partitionList(testvals11, 0, length-2, testvals11[length-1]); // cout << "partitionList() test 1, basic test" << endl // << " expected: " << tostring(expected11, length) << endl // << " expected pivot: " << expectedPivotIndex <<endl // << " actual: " << tostring(testvals11, length) << endl // << " actual pivot: " << actualPivotIndex << endl // << endl; // assert(listsAreEqual(testvals11, expected11, length)); // assert(actualPivotIndex == expectedPivotIndex); // another general test, also this tests that all values == pivot end // up being moved to the right length = 15;
  • 26. int testvals12[] = {12, 7, 10, 19, 6, 18, 12, 15, 6, 10, 4, 13, 11, 17, 10}; int expected12[] = {4, 7, 6, 6, 19, 18, 12, 15, 10, 10, 12, 13, 11, 17, 10}; expectedPivotIndex = 4; // actualPivotIndex = partitionList(testvals12, 0, length-2, testvals12[length-1]); // cout << "partitionList() test 2, bigger test and some duplicates of the pivot" << endl // << " expected: " << tostring(expected12, length) << endl // << " expected pivot: " << expectedPivotIndex <<endl // << " actual: " << tostring(testvals12, length) << endl // << " actual pivot: " << actualPivotIndex << endl // << endl; // assert(listsAreEqual(testvals12, expected12, length)); // assert(actualPivotIndex == expectedPivotIndex); // test everything is to left of pivot is working length = 6; int testvals13[] = {4, 3, 7, 6, 5, 10}; int expected13[] = {4, 3, 7, 6, 5, 10}; expectedPivotIndex = 5; // actualPivotIndex = partitionList(testvals13, 0, length-2, testvals13[length-1]); // cout << "partitionList() test 3, everything is smaller than pivot value" << endl // << " expected: " << tostring(expected13, length) << endl // << " expected pivot: " << expectedPivotIndex <<endl // << " actual: " << tostring(testvals13, length) << endl // << " actual pivot: " << actualPivotIndex << endl // << endl; // assert(listsAreEqual(testvals13, expected13, length));
  • 27. // assert(actualPivotIndex == expectedPivotIndex); // test everything is to right of pivot is working length = 6; int testvals14[] = {4, 3, 7, 6, 5, 2}; int expected14[] = {4, 3, 7, 6, 5, 2}; expectedPivotIndex = 0; // actualPivotIndex = partitionList(testvals14, 0, length-2, testvals14[length-1]); // cout << "partitionList() test 4, everything is bigger than pivot value" << endl // << " expected: " << tostring(expected14, length) << endl // << " expected pivot: " << expectedPivotIndex <<endl // << " actual: " << tostring(testvals14, length) << endl // << " actual pivot: " << actualPivotIndex << endl // << endl; // assert(listsAreEqual(testvals14, expected14, length)); // assert(actualPivotIndex == expectedPivotIndex); // test small list that needs to be swapped length = 3; int testvals15[] = {8, 2, 4}; int expected15[] = {2, 8, 4}; expectedPivotIndex = 1; // actualPivotIndex = partitionList(testvals15, 0, length-2, testvals15[length-1]); // cout << "partitionList() test 5, small list that needs a swap" << endl // << " expected: " << tostring(expected15, length) << endl // << " expected pivot: " << expectedPivotIndex <<endl // << " actual: " << tostring(testvals15, length) << endl // << " actual pivot: " << actualPivotIndex << endl
  • 28. // << endl; // assert(listsAreEqual(testvals15, expected15, length)); // assert(actualPivotIndex == expectedPivotIndex); // list of only 1 item (besides pivot value), still needs to work length = 2; int testvals16[] = {8, 4}; int expected16[] = {8, 4}; expectedPivotIndex = 0; // actualPivotIndex = partitionList(testvals16, 0, length-2, testvals16[length-1]); // cout << "partitionList() test 6, list of size 1 (and pivot value)" << endl // << " expected: " << tostring(expected16, length) << endl // << " expected pivot: " << expectedPivotIndex <<endl // << " actual: " << tostring(testvals16, length) << endl // << " actual pivot: " << actualPivotIndex << endl // << endl; // assert(listsAreEqual(testvals16, expected16, length)); // assert(actualPivotIndex == expectedPivotIndex); // list of only 1 item, but the pivot is bigger length = 2; int testvals17[] = {8, 12}; int expected17[] = {8, 12}; expectedPivotIndex = 1; // actualPivotIndex = partitionList(testvals17, 0, length-2, testvals17[length-1]); // cout << "partitionList() test 7, list of size 1, pivot value is larger" << endl // << " expected: " << tostring(expected17, length) << endl // << " expected pivot: " << expectedPivotIndex <<endl // << " actual: " << tostring(testvals17, length) <<
  • 29. endl // << " actual pivot: " << actualPivotIndex << endl // << endl; // assert(listsAreEqual(testvals17, expected17, length)); // assert(actualPivotIndex == expectedPivotIndex); // sort by hand using partitionList() length = 5; int testvals18[] = {7, 8, 3, 5, 6}; // actualPivotIndex = partitionList(testvals18, 0, length-2, testvals18[length-1]); // swapListValues(testvals18, length-1, actualPivotIndex); // cout << "partitionList() test 8 (sort by hand)" << endl; // cout << " after first partition: " << tostring(testvals18, length) << endl; // partition left // actualPivotIndex = partitionList(testvals18, 0, 0, testvals18[1]); // swapListValues(testvals18, 1, actualPivotIndex); // cout << " after second partition (left): " << tostring(testvals18, length) << endl; // partition right // actualPivotIndex = partitionList(testvals18, 3, 3, testvals18[4]); // swapListValues(testvals18, 4, actualPivotIndex); // cout << " after third partition (right): " << tostring(testvals18, length) << endl; int expected18[] = {3, 5, 6, 7, 8}; // cout << " expected: " << tostring(expected18, length) << endl // << " actual: " << tostring(testvals18, length) << endl // << endl;
  • 30. // assert(listsAreEqual(testvals18, expected18, length)); // test of quickSort function ---------------------------------------- --- cout << endl; cout << "Test quickSort() ------------------------------------------ -----" << endl; // sort a list of size 1 length = 1; int testvals19[] = {8}; int expected19[] = {8}; // quickSort(testvals19, 0, length-1); // cout << "quickSort() test 1, sort list of size 1" << endl // << " expected: " << tostring(expected19, length) << endl // << " actual: " << tostring(testvals19, length) << endl // << endl; // assert(listsAreEqual(testvals19, expected19, length)); // sort a list of size 2, already in order length = 2; int testvals20[] = {3, 9}; int expected20[] = {3, 9}; // quickSort(testvals20, 0, length-1); // cout << "quickSort() test 2, sort list of size 2 already ordered" << endl // << " expected: " << tostring(expected20, length) << endl // << " actual: " << tostring(testvals20, length) << endl // << endl; // assert(listsAreEqual(testvals20, expected20, length)); // sort a list of size 2, not in order length = 2;
  • 31. int testvals21[] = {9, 3}; int expected21[] = {3, 9}; // quickSort(testvals21, 0, length-1); // cout << "quickSort() test 3, sort list of size 2 out of order" << endl // << " expected: " << tostring(expected21, length) << endl // << " actual: " << tostring(testvals21, length) << endl // << endl; // assert(listsAreEqual(testvals21, expected21, length)); // sort an odd sized list length = 9; int testvals22[] = {9, 3, 2, 7, 5, 8, 6, 5, 4}; int expected22[] = {2, 3, 4, 5, 5, 6, 7, 8, 9}; // quickSort(testvals22, 0, length-1); // cout << "quickSort() test 4, sort odd sized list" << endl // << " expected: " << tostring(expected22, length) << endl // << " actual: " << tostring(testvals22, length) << endl // << endl; // assert(listsAreEqual(testvals22, expected22, length)); // sort an even sized list length = 14; int testvals23[] = {9, 3, 2, 7, 5, 8, 6, 5, 4, 2, 10, 8, 11, 5}; int expected23[] = {2, 2, 3, 4, 5, 5, 5, 6, 7, 8, 8, 9, 10, 11}; // quickSort(testvals23, 0, length-1); // cout << "quickSort() test 5, sort even sized list" << endl // << " expected: " << tostring(expected23, length) << endl // << " actual: " << tostring(testvals23, length) << endl // << endl; // assert(listsAreEqual(testvals23, expected23, length))1; // sort an already sorted list
  • 32. length = 17; int testvals24[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; int expected24[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; // quickSort(testvals24, 0, length-1); // cout << "quickSort() test 6, sort already sorted list" << endl // << " expected: " << tostring(expected24, length) << endl // << " actual: " << tostring(testvals24, length) << endl // << endl; // assert(listsAreEqual(testvals24, expected24, length)); // sort a reversed list length = 17; int testvals25[] = {17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; int expected25[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}; // quickSort(testvals25, 0, length-1); // cout << "quickSort() test 7, sort a reversed list" << endl // << " expected: " << tostring(expected25, length) << endl // << " actual: " << tostring(testvals25, length) << endl // << endl; // assert(listsAreEqual(testvals25, expected25, length)); // sort a list with lots of duplicates length = 14; int testvals26[] = {4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8, 4, 8}; int expected26[] = {4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8}; // quickSort(testvals26, 0, length-1); // cout << "quickSort() test 7, sort a reversed list" << endl // << " expected: " << tostring(expected26, length) << endl // << " actual: " << tostring(testvals26, length) << endl
  • 33. // << endl; // assert(listsAreEqual(testvals26, expected26, length)); // return 0 to indicate successful completion return 0; }