Bubble Sort—Test Suite should demonstrate tests on arrays of size 10 § ExampleIn
ID: 3820194 • Letter: B
Question
Bubble Sort—Test Suite should demonstrate tests on arrays of size 10 § ExampleInput: 37110115628910455 § Expected Output: 1, 2, 3, 5, 7, 10, 11, 56, 89, 1045 (15 pts) (5 pts for the algorithm and 10 pts for the code/test cases) The bubble sort is another technique for sorting an array. A bubble sort compares adjacent array elements and exchanges their values if they’re out of order. In this way, the smaller values “bubble” to the top of the array. After the first pass of the bubble sort, the last array element is in the correct position; after the second, the last two items are correctly placed, and so on. Thus, after each pass, the unsorted portion of the array contains 1 less element. Write and test a function that implements this sorting method. Your function should work for arrays of any valid size up a max of 100.C++
Bubble Sort—Test Suite should demonstrate tests on arrays of size 10 § ExampleInput: 37110115628910455 § Expected Output: 1, 2, 3, 5, 7, 10, 11, 56, 89, 1045 (15 pts) (5 pts for the algorithm and 10 pts for the code/test cases) The bubble sort is another technique for sorting an array. A bubble sort compares adjacent array elements and exchanges their values if they’re out of order. In this way, the smaller values “bubble” to the top of the array. After the first pass of the bubble sort, the last array element is in the correct position; after the second, the last two items are correctly placed, and so on. Thus, after each pass, the unsorted portion of the array contains 1 less element. Write and test a function that implements this sorting method. Your function should work for arrays of any valid size up a max of 100.C++
Bubble Sort—Test Suite should demonstrate tests on arrays of size 10 § ExampleInput: 37110115628910455 § Expected Output: 1, 2, 3, 5, 7, 10, 11, 56, 89, 1045 (15 pts) (5 pts for the algorithm and 10 pts for the code/test cases) The bubble sort is another technique for sorting an array. A bubble sort compares adjacent array elements and exchanges their values if they’re out of order. In this way, the smaller values “bubble” to the top of the array. After the first pass of the bubble sort, the last array element is in the correct position; after the second, the last two items are correctly placed, and so on. Thus, after each pass, the unsorted portion of the array contains 1 less element. Write and test a function that implements this sorting method. Your function should work for arrays of any valid size up a max of 100.C++
Explanation / Answer
BubbleSort.cpp consists of a function bubbleSort which sorts given array of integers using bubble sort algorithm (described in problem description).
BubbleSortTests.cpp consists of three unit tests for function bubbleSort.
Note: As no unit test framework is specified in the problem description I didn't use one in below implementation of test suite.
File: BubbleSort.cpp
#include <iostream>
using namespace std;
extern void bubbleSort(int numList[], int numListSize);
void bubbleSort(int numList[], int numListSize) {
int temp;
bool swapped;
for (int i = 0; i < numListSize - 1; i++) {
swapped = false;
for (int j = 0; j < numListSize - 1 - i; j++) {
if (numList[j] > numList[j+1]) { // compare adjacent elements
// Swap
temp = numList[j];
numList[j] = numList[j+1];
numList[j + 1] = temp;
swapped = true;
}
}
if (swapped == false) {
break; // array is sorted now
}
}
}
File: BubbleSortTests.cpp
#include <iostream>
using namespace std;
extern void bubbleSort(int numList[], int numListSize);
const int MAX_SIZE = 100;
int numList[MAX_SIZE];
void logList(string tag, int size) {
cout << tag;
for (int i = 0; i < size; i++) {
cout << numList[i] << " ";
}
cout << endl;
}
void testSetup(int input[], int inputSize) {
for (int i = 0; i < inputSize; i++) {
numList[i] = input[i];
}
logList("Input: ", inputSize);
}
bool testVerify(string testcase, int expected[], int inputSize) {
logList("Output: ", inputSize);
cout << testcase;
for (int i = 0; i < inputSize; i++) {
if (numList[i] != expected[i]) {
cout << ": FAIL" << endl << endl;
return false;
}
}
cout << ": PASS" << endl << endl;
return true;
}
bool testBubbleSortRandom() {
int input[] = { 3, 7, 1, 10, 11, 56, 2, 89, 1045, 5 };
int expected[] = { 1, 2, 3, 5, 7, 10, 11, 56, 89, 1045 };
testSetup(input, 10);
bubbleSort(numList, 10);
testVerify("testBubbleSortRandom", expected, 10);
}
bool testBubbleSortReverseSorted() {
int input[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int expected[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
testSetup(input, 10);
bubbleSort(numList, 10);
testVerify("testBubbleSortReverseSorted", expected, 10);
}
bool testBubbleSortSorted() {
int input[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int expected[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
testSetup(input, 10);
bubbleSort(numList, 10);
testVerify("testBubbleSortSorted", expected, 10);
}
void runTestSuite() {
testBubbleSortRandom();
testBubbleSortReverseSorted();
testBubbleSortSorted();
}
int main()
{
runTestSuite();
}
Compilation:
$ g++ BubbleSort.cpp BubbleSortTests.cpp
Test Execution Output:
$ ./a.out
Input: 3 7 1 10 11 56 2 89 1045 5
Output: 1 2 3 5 7 10 11 56 89 1045
testBubbleSortRandom: PASS
Input: 10 9 8 7 6 5 4 3 2 1
Output: 1 2 3 4 5 6 7 8 9 10
testBubbleSortReverseSorted: PASS
Input: 10 9 8 7 6 5 4 3 2 1
Output: 1 2 3 4 5 6 7 8 9 10
testBubbleSortSorted: PASS
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.