Question

Write a version of the bubble sort algorithm in a function called bubbleSort that can be...

Write a version of the bubble sort algorithm in a function called bubbleSort that can be used to sort a string vector object. Also, write a program to test your algorithm. The program should prompt the user for a series of names. The string zzz should end the input stream. Output the sorted list to the console.

*need answer in C++*

Homework Answers

Answer #1

Below is the c++ solution with output screenshot

Code :

// including libraries
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

// bubbleSort function to sort the string vector object
vector<string> bubbleSort(vector<string> arr)
{
    string temp;
    for (int j=0; j<arr.size(); j++)
    {
        for (int i=j+1; i<arr.size(); i++)
        {
            if (strcmp(arr[j].c_str(), arr[i].c_str()) > 0)
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return arr;
}

// main function
int main()
{
    vector<string> names;
    string name;
    // taking series of names input until user enters zzz
    cout<<"Enter series of names (zzz to stop):\n";
    while(1){
        cin>>name;
        if(name == "zzz"){
            break;
        }
        names.push_back(name);
    }

    names = bubbleSort(names);
    // print the sorted names array
    cout<<"\nNames after sorting are :\n";
    for(int i=0;i<names.size();i++){
        cout<<names[i]<<endl;
    }
    return 0;
}

Output :

Note : for better understanding please refer to the code screenshot below

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a version of the selection sort algorithm that can be used to sort a list...
Write a version of the selection sort algorithm that can be used to sort a list of strings alphabetically. (Selection sort for int lists is discussed in Chapter 8.) Write a program to test the function and prompt the user to enter 10 strings. Output the sorted list to the console. *Need answer in C++*
Using Python: 1. A simple sorting algorithm is called a "Bubble Sort" because elements bubble around...
Using Python: 1. A simple sorting algorithm is called a "Bubble Sort" because elements bubble around through the list. It is also called a "Sinking Sort" because the larger values "sink" to the end (bottom) of the list. A bubble sort iterates through a list and swaps adjacent pairs if they are in the wrong order. The sort continues until all elements are correctly ordered. Example: bubbleSort([0, 1, 8, 4, 3, 2, 9]) - as it begins to process will...
# Implement the Bubble Sort algorithm to this code. Add output statements to the code to...
# Implement the Bubble Sort algorithm to this code. Add output statements to the code to verify that it is working properly. Then, add code that will short circuit the sort process if at any time an intermediate pass through the list identifies that the entire list is already sorted. def bubbleSort(theSeq): n = len(theSeq) #perform n - 1 bubble operations on the sequence for i in range(n - 1): #bubble largest item to end for j in range(i +...
Write an application that prompt user to choose Sorting Algorithm (Insertion, Merge, or Heap) in GUI...
Write an application that prompt user to choose Sorting Algorithm (Insertion, Merge, or Heap) in GUI input message, then prompt user to enter numbers in GUI input message also, sort these numbers in descending order using Selected sorting algorithm, and show the result in message dialog as shown in the following figure. The program contains two classes: 1- “Sort” Class which contains 3 methods named as follow: a. InsertionSort(int[] numbers), returns sorted numbers from the passed array using Insertion sort...
Write a Java Program to bubble sort 10,20,15,0,6,7,2,1,-5,55. Including Algorithm flowchart of the program.
Write a Java Program to bubble sort 10,20,15,0,6,7,2,1,-5,55. Including Algorithm flowchart of the program.
Write a function custom sort(v) that takes as input a vector v and as output returns...
Write a function custom sort(v) that takes as input a vector v and as output returns the vector w sorted into increasing order. For example, if the input is [−2 1 3 1 5], the output should be [−2 1 1 3 5]. Don’t use the built-in ”sort” function or anything similar. matlab question
Write an insertion sort algorithm in C language by performing following steps .make sure code is...
Write an insertion sort algorithm in C language by performing following steps .make sure code is work. thanks Prompt the user to enter the number of array elements (say, N). Read the number of elements (N). Implement the insertion sort algorithm (note that insertion sort is an in-place sort that modifies the original array). Print the sorted array.
Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort...
Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot = first index) algorithms. Compute the CPU processing time for all the algorithms for varying input sizes as follows: N = 102, 103, 104, 105, and 106 Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 103, 1 - 106, 1 – 109, 1 - 1012 Write down your results as a table (with...
The binary search algorithm given in this chapter is nonrecursive. Write and implement a recursive version...
The binary search algorithm given in this chapter is nonrecursive. Write and implement a recursive version of the binary search algorithm. The program should prompt Y / y to continue searching and N / n to discontinue. If an item is found in the list display the following message: x found at position y else: x is not in the list Use the following list of integers when testing your program: 2, 6, 8, 13, 25, 33, 39, 42, 53,...
23 30 35 43 4 10 22 4 Complete the following: a) Use the bubble sort...
23 30 35 43 4 10 22 4 Complete the following: a) Use the bubble sort to sort the values showing the order of the values in the list after every pass of the sorting algorithm.   b) What is the time complexity of the sort (use the number of comparisons required as a measure of time.)    c) Modify the algorithm so it stops if there are no swaps in a complete pass. How many comparisons would be needed in your...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT