Question

In: Computer Science

This is c++ code. Create a file sort.cpp. to mix functions with the selection sort algorithm:...

This is c++ code.

Create a file sort.cpp. to mix functions with the selection sort algorithm:

·Write a function int least(vector<string> strs, int start)to return the index of the smallest value in the vector. You are to assume there is at least one value in the vector.

·Write a function void selsort(vector<string> & strs) to use selection sort to sort the vector of strings. It is a worthwhile experiment to try leaving out the & and seeing that the vector stays exactly the way it is, but remember to put it back in before submitting.

·You need to use the above function here

·Once you know the elements you want to swap, call the library swap(T& a, T& b) function to do the swap, which after the 2011 standard, has been moved from <algorithm> to the <utility> library (though I think IDE’s automatically include this, so you probably won’t need to explicitly #include it). The &’s in the prototype of the function allow it to actually move your values, but you should not be using &’s when calling the function

·Write main() to test your selection sort. You just need to output the

·Try at least two calls, one where the elements are already in order and one where you need at least two swaps to order the values. For the following run, the first call was done with “lion”, “tiger”, while the 2nd call was done with “lion”, “tiger”, “zebra”, “bear” (the first swap is of “lion” and “bear”, so the 2nd iteration starts with “bear”, “tiger”, “zebra”, “lion” and “lion” needs to be swapped to the second position)

Before: lion tiger

After: lion tiger

Before: lion tiger zebra bear

After: bear lion tiger zebra

Solutions

Expert Solution

// do comment if any problem arises

//code

#include <iostream>

#include <string>

#include <vector>

#include <utility>

using namespace std;

// this function returns index of string with minimum length in given vector

int least(vector<string> strs, int start)

{

    int min = start;

    // iterate through vector from given start

    for (int i = start; i < strs.size(); i++)

    {

        if (strs[min] > strs[i])

        {

            min = i;

        }

    }

    return min;

}

// this function sorts given vector using selection sort

void selsort(vector<string> &strs)

{

    // current minimum index

    int min;

    // outer loop of i from 1 to n-1

    for (int i = 0; i < strs.size() - 1; i++)

    {

        min = i;

        // compute index with ith minimum element

        min=least(strs,i);

        swap(strs[min], strs[i]);

    }

}

void print(vector<string> temp)

{

    for (int i = 0; i < temp.size(); i++)

        cout << temp[i] << " ";

    cout << endl;

}

int main()

{

    // create first vector

    vector<string> first;

    first.push_back("lion");

    first.push_back("tiger");

    cout<<"Before: ";

    // print vector before sort

    print(first);

    selsort(first);

    cout<<"After: ";

    // print vector after sort

    print(first);

    // create second vector

    vector<string> second;

    second.push_back("lion");

    second.push_back("tiger");

    second.push_back("zebra");

    second.push_back("bear");

    cout<<"Before: ";

    // print vector before sort

    print(second);

    selsort(second);

    cout<<"After: ";

    // print vector after sort

    print(second);

}

Output:


Related Solutions

Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in a list and swapping the first value with it, then finding the second smallest value and swapping the second value with it, continuing until all the values are in order. Implement this algorithm, then determine its growth function, and hence the order of the algorithm. Find how many swaps are made. Use Java Code to create algorithm
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football...
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football League list of current players. It's going to be a big list(over 1000 names). Please identify, in comments, which part of the code is for the Selection Sort and which part of the code is for Insertion Sort. It must have both. Inputting data from a text file named "Players.txt" Only want the name of the player(first name then last name), their team name,...
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort...
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort with insertion sort with respect to performance (consider worst case and best case running times).
PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND...
PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND BUBBLE SORT FUNCTION TO THIS PROGRAM #include <iostream> #include<vector> #include <algorithm >   #include <chrono>    #include <ctime> using namespace std; void bubblesSort() { // Please create Bubble Sort function// Make another for Selection Sort and  Insertion Sort } int main() { // empty vector vector<int> data; // data [0], data [1]... data[N-1] <-- end(data) // set of values to test N for (auto N :...
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers....
(code in C++ language) [Code Bubble sort, Insertion sort Create a Big array with random numbers. Record the time. Run Bubble Check time (compute the processing time) do it 100 times (random numbers) Take the average Insertion: Compare] (some explanations please)
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain size. The list is to be implemented using a dynamic array as well as a singly linked list. You are required to compare the performance (sorting time) for the dynamic array and singly-linked list-based implementations. You are given the startup codes for the dynamic array and singly-linked list based implementations. You are required to implement the Selection Sort function in each of these codes....
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT