In: Computer Science
It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much like the previous lab, you will be tasked with prompting the user for a list of values until the user enters 0 (you may use the same initializeVector that you wrote in the last lab). You will then write bubblesort which sorts the vector from smallest to largest. You will then call a function displayVector which displays the vector to the screen. Keep everything in this assignment. We will be using this to build selection sort. Instructions Create these functions (one of which you wrote in the last lab). void initializeVector(vector&); int bubblesort(vector&); void displayVector(vector&); void swap(vector&, int, int); Function Descriptions • initializeVector – This function prompt the user to add elements to the end of a vector until the user enters a 0. – The final 0 entered should not be added to the end of the vector. – Because the function passes the vector by reference, there's no need to return – Preconditions: none – Postconditions: the vector will be populated with values. – @param list the list to be populated with values. • bubblesort – This function will sort the values in the supplied list from smallest to largest. – It should also keep count of the number of times the swap function is called. – Precondition: none – Postcondition: the list will be sorted – @param list the list to be sorted – @return the number of swaps necessary to complete this sort • displayVector – This function will display the values of list. If the list is empty, it will instead say "The list is empty." – Precondition: none – Postcondition: none – @param list the list to be displayed • swap – This function will swap the values at two provided index locations in the provided vector and return nothing. – Precondition: both index locations are greater than or equal to 0 and less than the size of the vector. – Postcondition: The values of the vector at the two locations will be swapped. – @param list the list to have values swapped – @param first the first index to swap – @param second the second index to swap Instructions 1. Document the beginning of the program with your name, class number, section number, date, and description of the program. 2. Craft the functions. 3. Document those functions. 4. In the main, greet the user. For example, "Welcome to the Bubble Sort Number Sorter." 5. Create a vector of integers. 6. Call initializeVector using the vector of integers. 7. Call bubblesort using the vector of integers. 8. Display how many times it took to swap values. 9. Call displayVector to display the vector of integers. Examples Bubble Sort should handle an empty list with no special code. Welcome to the Bubble Sort Number Sorter. Enter values to add to list (Enter 0 to stop): 0 This sorting algorithm requried 0 swaps. Here are the values sorted. The list is empty. Bubble Sort should handle a list that is already in order. This requires 0 swaps. Welcome to the Bubble Sort Number Sorter. Enter values to add to list (Enter 0 to stop): 1 Enter values to add to list (Enter 0 to stop): 2 Enter values to add to list (Enter 0 to stop): 3 Enter values to add to list (Enter 0 to stop): 4 Enter values to add to list (Enter 0 to stop): 5 Enter values to add to list (Enter 0 to stop): 0 This sorting algorithm requried 0 swaps
Screenshot
Program
/*
Your name:
Class number:
Section number:
Date:
Description: Program prompt for integers to store in
to a vector until 0
Then sort vector using bubble sort
Display sorted array
*/
#include <iostream>
#include<vector>
using namespace std;
//Function prototypes
void initializeVector(vector<int>&);
int bubblesort(vector<int>&);
void displayVector(vector<int>&);
void swap(vector<int>&, int, int);
int main()
{
//Welcome message
cout << "Welcome to the Bubble Sort Number
Sorter." << endl;
//Create a vector of integers
vector<int>vArray;
//Call initializeVector using the vector of
integers.
initializeVector(vArray);
//Call bubblesort using the vector of integers.
int swapCount = bubblesort(vArray);
//Display how many times it took to swap values
cout << "Swap count = " << swapCount
<< endl;
//Call displayVector to display the vector of
integers
displayVector(vArray);
}
/*
Function:initializeVector
Preconditions: none
Postconditions: the vector will be populated with values
Description:This function prompt the user to add elements to the
end of a vector
until the user enters a 0
*/
void initializeVector(vector<int>& vArray) {
int val;
cout << "Enter values to add to list (Enter 0 to
stop): ";
cin >> val;
while (val != 0) {
vArray.push_back(val);
cout << "Enter values to add
to list (Enter 0 to stop): ";
cin >> val;
}
}
/*
Function:bubbleSort
Preconditions: none
Postcondition: the list will be sorted and return swap count
Description:This function will sort the values in the supplied
list
from smallest to largest
*/
int bubblesort(vector<int>& vArray) {
int swapCnt = 0;
for (int i = 0; i < vArray.size(); i++) {
for (int j = 0; j <
vArray.size() - 1; j++) {
if (vArray[j]
> vArray[j+1]) {
swapCnt++;
swap(vArray, j, j + 1);
}
}
}
return swapCnt;
}
/*
Function:swap
Precondition: both index locations are greater than or equal to 0
and less than the size of the vector
Postcondition: The values of the vector at the two locations will
be swapped
Description:This function will swap the values at two provided
index locations
in
the provided vector and return nothing
*/
void swap(vector<int>& vArray, int i, int j) {
if (i < 0 || j < 0 || i >= vArray.size() || j
>= vArray.size()) {
cout << "Wrong index"
<< endl;
}
else {
int temp = vArray[i];
vArray[i] = vArray[j];
vArray[j] = temp;
}
}
/*
Function:displayVector
Precondition: none
Postcondition: none
Description:This function will display the values of list.
If the list is empty, it will instead say "The list is
empty."
*/
void displayVector(vector<int>& vArray) {
if (vArray.size() == 0) {
cout << "The list is empty."
<< endl;
}
else {
for (int i = 0; i <
vArray.size(); i++) {
cout <<
vArray[i] << " ";
}
cout << endl;
}
}
Output
Welcome to the Bubble Sort Number Sorter.
Enter values to add to list (Enter 0 to stop): 1
Enter values to add to list (Enter 0 to stop): 10
Enter values to add to list (Enter 0 to stop): 25
Enter values to add to list (Enter 0 to stop): 4
Enter values to add to list (Enter 0 to stop): 8
Enter values to add to list (Enter 0 to stop): -1
Enter values to add to list (Enter 0 to stop): 2
Enter values to add to list (Enter 0 to stop): 0
Swap count = 13
-1 1 2 4 8 10 25