Question

In: Computer Science

It's time to write a sorting algorithm! In this lab, you'll be writing Bubble Sort. Much...

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

Solutions

Expert Solution

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


Related Solutions

Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into...
ASSEMBLY PROGRAM!!! QtSpim Sorting Data Add the Bubble Sort to minMaxArray.asm to sort the array into ascending order. Use the Bubble Sort algorithm from the lecture. You can use either Base Addressing or Indexed Addressing for the arrays. For this assignment, make sure you prompt the user for the numbers. Do not hard-code them in the data section. NOTE: Declare the array last in the Data section.
What is the number of comparisons in the bubble sort algorithm, if it is used to...
What is the number of comparisons in the bubble sort algorithm, if it is used to sort a list of n-entries? Justify your formula.
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the...
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the array to be sorted; i is the start index and j is the end index. n = j – i + 1 If (n < 18) { sort A[i…j] by insertion-sort return } m1 = i + 2 * n / 3 m2 = i + n / 3 aSort(A, i, m1) aSort(A, m2, j) aSort(A, i, m1) questions: 1) Please state the asymptotic...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
Using the buildHeap method, write a sorting function that can sort a list in O(nlogn) time....
Using the buildHeap method, write a sorting function that can sort a list in O(nlogn) time. def buildHeap(self,alist): i = len(alist) // 2 self.currentSize = len(alist) self.heapList = [0] + alist[:] while (i > 0): self.percDown(i) i = i - 1 Base on this code please
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort,...
come up with at least 2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT