Introduction:
The
objective of the programming exercise is to write a function to sort a given
list of strings using selection sort.
Program Plan:
· Define header section.
· Define void selectionSort(vector<string>
&arr) function
that take vector of string as pass by reference.
o
Declare array size int n =
arr.size().
o
Use for loop i from 0 to n-2.
§ Declare
int
min_index = i, tracks minimum element index in array arr[i]...arr[n-1].
§ Use for
loop j from i+1
to
n-1.
· If arr[j]
< arr[min_index].
o
Assignment j to min_index.
§ Swap
element at index i and min_index.
· Define main() function
(testing function).
o
Declare int n variable.
o
Take user input for list size n.
o
Declare vector<string> arr(n).
o
Take user input for strings using for
loop.
o
Call to function
selectionSort().
o
Print the list of the string after sorting
using for loop.
0 Comments
If you have any doubt let me know.