Show the call stack for selectionSort("abcba") using the function defined in Listing.
Listing RecursiveSelectionSort.cpp
1#include
2#include
3using namespace std;
4
5void sort(string& s, int high)
6 {
7 if (high 0)
8 {
9 // Find the largest element and its index
10 int indexOfMax = 0;
11 char max = s[0 ];
12 for (int i = 1 ; i <= high; i++)
13 {
14 if (s[i] > max)
15 {
16 max = s[i];
17 indexOfMax = i;
18 }
19 }
20
21 // Swap the largest with the last element in the list
22 s[indexOfMax] = s[high];
23 s[high] = max;
24
25 // Sort the remaining list
26 sort(s, high - 1);
27 }
28 }
29
30 void sort(string& s)
31 {
32 sort(s, s.size() - 1);
33 }
34
35int main()
36 {
37 cout << "Enter a string: ”;
38 string s;
39 get1ine(cin, s);
40
41 sort(s);
42
43 cout << "The sorted string is " << s << endl;
44
45 return 0;
46 }
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.