Write a function void sort2(double* p, double* q) that receives two pointers and sorts the values to which they point. If you call sort2(&x, &y) then x <= y after the call.
Question
Write a function void sort2(double* p, double* q) that receives two pointers and sorts the values to which they point. If you call sort2(&x, &y) then x <= y after the call.
Solution
Sure, here is a simple implementation of the function in C++:
void sort2(double* p, double* q) {
if (*p > *q) {
double temp = *p;
*p = *q;
*q = temp;
}
}
This function works by comparing the values pointed to by p and q. If the value at p is greater than the value at q, it swaps the values. After the function call, the value at p will be less than or equal to the value at q.
Similar Questions
Write a function void sort2(int& a, int& b) that swaps the values of a and b if a isgreater than b and otherwise leaves a and b unchanged. For example,int u = 2;int v = 3;int w = 4;int x = 1;sort2(u, v); // u is still 2, v is still 3sort2(w, x); // w is now 1, x is now 4
Program to implement heap sort
C program to sort an array in ascending order
Problem StatementDr. Smith manages a chemistry laboratory where he regularly analyzes the concentration values of various samples. He needs a quick and efficient program to sort the concentration values of 5 different lab samples where each sample is uniquely identified by its floating-point concentration value.To assist Dr. Smith in accomplishing the above task, implement a call-by-reference function called sort for sorting the samples in descending order and display the result.Input format :The input consists of five space-separated floating-point values, representing the concentration of lab samples.Output format :The output prints the sorted concentration values in descending order with two decimal places, separated by spaces.Refer to the sample output for formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:1.00 ≤ Each concentration value ≤ 40.00Sample test cases :Input 1 :34.1 3.4 23.54 1.01 40.0Output 1 :40.00 34.10 23.54 3.40 1.01Input 2 :4.78 5.47 7.42 8.94 7.83Output 2 :8.94 7.83 7.42 5.47 4.78
If one uses a straight two-way merge sort algorithm to sort the following elements in ascending order: 20, 47, 15, 8, 9, 4, 40, 30,12, 17Then the order of these elements after the second pass of the algorithm is ________.*1 point8, 9, 15, 20, 47, 4, 12, 7, 30, 408, 15, 20, 47, 4, 9, 30, 40, 12, 1715, 20, 47, 4, 8, 9, 12, 30, 40, 174, 8, 9, 15, 20, 47, 12, 17, 30, 40
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.