In: Computer Science
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
// 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: