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

Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort,...
Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort, insertion sort, and merge sort, It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674...
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.
Write a program and test a program that translates the following Bubble Sort algorithm to a...
Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is, void bubblesort(int a[], int size); Bubble Sort The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array. The Bubble sort exchanges elements with adjacent elements as it moves the...
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...
5. (20 marks) Write a recursive Bubble Sort algorithm that takes an array A of n...
5. Write a recursive Bubble Sort algorithm that takes an array A of n numbers as input. Analyze its time complexity using a recursion tree. Implement your algorithm in Java
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting .Arrival time, burst time and priority values.The code should also display: (i) Gantt chart and determine the following: (ii) Determine the Turnaround time(TAT), waiting time(WT) of each process (iii) Determine the Average Waiting Time (AWT) and Average Turnaround Time (ATAT) of all processes. please write the comments
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT