Programming Assignment 05-06: Searching and Sorting
assg-05.cpp
assg-05.cpp
/**
*
@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
;
}
QuickSort.cpp
QuickSort.cpp
/**
*
@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
QuickSort.hpp
/** * @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_
assg-05.pdf
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