Assignment: Understanding and Implementing the Simplest Sorting Algorithm
Objective:
To understand the basics of sorting by implementing the simplest sorting algorithm (Bubble Sort), create a flow chart representing the algorithm, and implement the algorithm in C++. Additionally, create unit tests to ensure the correctness of the implementation.
Task Breakdown:
-
Define the Simplest Sorting Algorithm (Bubble Sort):
- Bubble Sort is a simple comparison-based sorting algorithm.
- The algorithm works by repeatedly swapping adjacent elements if they are in the wrong order.
- The process is repeated until the list is sorted.
-
Create a Flow Chart:
- Draw a flow chart to visually represent the logic of Bubble Sort.
- The flow chart should include:
- Start and end points
- Decision points for comparing elements
- Steps for swapping elements
- Steps for iterating through the list
-
Implement Bubble Sort in C++:
- Write a C++ program that implements the Bubble Sort algorithm.
-
Create Unit Tests:
- Develop unit tests to verify the correctness of the Bubble Sort implementation.
- Use a unit testing framework like Google Test.
Detailed Instructions:
Step 1: Define Bubble Sort
Bubble Sort Algorithm:
- Start at the beginning of the list.
- Compare the first two elements. If the first is greater than the second, swap them.
- Move to the next pair of elements and repeat the process until the end of the list is reached.
- Repeat the above steps for all elements, reducing the number of elements to be compared by one each time (since the largest element gets sorted to the end in each pass).
- Continue until no more swaps are needed.
Step 2: Create the Flow Chart
Flow Chart:
- Start →
- Initialize
i to 0 →
- Loop: While
i < n-1 (outer loop):
- Initialize
j to 0 →
- Nested Loop: While
j < n-i-1 (inner loop):
- Compare
array[j] and array[j + 1]
- If
array[j] > array[j + 1], swap them
- Increment
j
- Increment
i
- End
Step 3: Implement Bubble Sort in C++
#include <iostream>
#include <vector>
using namespace std;
void bubbleSort(vector<int> &arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
int main() {
vector<int> arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
cout << "Sorted array: ";
for (int i = 0; i < arr.size(); ++i) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Step 4: Create Unit Tests
Unit Tests Using Google Test:
First, install Google Test if it's not already installed. You can usually do this through your package manager or by including the necessary files in your project.
#include <gtest/gtest.h>
#include <vector>
// Function to be tested
void bubbleSort(std::vector<int> &arr) {
int n = arr.size();
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
}
}
}
}
// Test case 1: General case
TEST(BubbleSortTest, GeneralCase) {
std::vector<int> arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
std::vector<int> expected = {11, 12, 22, 25, 34, 64, 90};
EXPECT_EQ(arr, expected);
}
// Test case 2: Already sorted array
TEST(BubbleSortTest, SortedArray) {
std::vector<int> arr = {1, 2, 3, 4, 5};
bubbleSort(arr);
std::vector<int> expected = {1, 2, 3, 4, 5};
EXPECT_EQ(arr, expected);
}
// Test case 3: Array with duplicate elements
TEST(BubbleSortTest, DuplicatesArray) {
std::vector<int> arr = {5, 1, 4, 2, 1};
bubbleSort(arr);
std::vector<int> expected = {1, 1, 2, 4, 5};
EXPECT_EQ(arr, expected);
}
// Test case 4: Empty array
TEST(BubbleSortTest, EmptyArray) {
std::vector<int> arr = {};
bubbleSort(arr);
std::vector<int> expected = {};
EXPECT_EQ(arr, expected);
}
// Test case 5: Single element array
TEST(BubbleSortTest, SingleElementArray) {
std::vector<int> arr = {42};
bubbleSort(arr);
std::vector<int> expected = {42};
EXPECT_EQ(arr, expected);
}
Compiling and Running Tests:
- You can typically compile your tests with your main program and Google Test library.
- Use a command like:
g++ -std=c++11 -isystem /path/to/gtest/include -pthread bubble_sort_test.cpp /path/to/gtest/libgtest.a -o bubble_sort_test.
- Run the binary:
./bubble_sort_test.
Submission Requirements:
- A PDF file with the Flow Chart of the Bubble Sort algorithm.
- A C++ source file (
bubble_sort.cpp) for the Bubble Sort implementation.
- A C++ source file (
bubble_sort_test.cpp) with all unit tests using Google Test.
Deadline:
Please submit all parts of the assignment by [specific deadline date].