1. Provide the definition of the following class MyString. The test function and
ID: 3832995 • Letter: 1
Question
1. Provide the definition of the following class MyString. The test function and the expected output are provided.
else
}
2. Below is an example of a sorting algorithm that sorts a list of numbers in ascending order. Write a function
void mySort(vector<int>& num_list, int num_elements)
that implements the algorithm for an input vector num list of num element integers. The test function and the expected output are provided.
Here’s the example - Let us take the array of numbers ”5 1 4 2 8”, and sort the array from lowest number to greatest number. In each step, elements written in bold are being compared. Three passes will be required.
int main() {
}
Can someone write the functions and explain the code for both questions?
Also, make the code where it can be copied into a compiler.
Thanks!
Explanation / Answer
First program gives an overview of string comparison
-In first program they are comparing positions of each string.
-It will compare the position of string as well as length of each string
-if first string is longer than second string it will print Prints Hello World!
-If first string is smaller than second string it will print blank
Second program is used for sorting
-This is used to compare a list of number from smaller to larger.
-It is done using swap process
-In each iteration it will compare the value of 2 integers
-I will provide you an algorithm for sort
-In selection sort algorithm, we search for the lowest element and arrange it to the proper location.
-Then We swap the current element with the next lowest number.
-It improves improves on the bubble sort by making only one exchange for every pass through the list.
-In order to do this, a selection sort looks for the largest value as it makes a pass and, after completing the pass, places it in the proper location.
- This process continues and requires n1 passes to sort n items, since the final item must be in place after the (n1) pass.
public static void mySort (int[] num_list){
for (int i = 0; i < num_list.length - 1; i++)
{
int position = i;
for (int j = i + 1; j < num_list.length; j++){
if (num_list [j] < arr[position]){
position = j;//searching for lowest index
}
}
int smallerNumber = num_list [position];
num_list [position] = num_list [i];
num_list [i] = smallerNumber;
}
}
print(num_list)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.