Exercise 4: Sorting Algorithm

This exercise requires you to use template type to generalize the function used to sort values in an array in ascending order

We'll cover the following

Problem Statement

In this part, you will put the codes from exercise 2 and exercise 3 to use. Both these codes are already prepended (hence not visible to you) in the code widget below. You only need to call them with appropriate parameters where required.

In the code widget below, two functions both called sort are declared. One finds sorts values for int type array inputs and the other for double type.

In this exercise, you need to define a Template Class type function called sort that will generalize the function such that it sorts values for both int and double type input values.

IMPORTANT NOTE: Remove both the int and double type sort functions and then write the code for the Template Class type sort function there.

In the following exercise:

  • The function sort will take as input:
    • Array
    • Array size
  • The function should be able to sort the given array in ascending order.

Hint: Use the functions from previous examples given in code widget below to implement the sort algorithm.

Down below is what the expected output should look like.

Input 1:

int a[10] = {9,8,7,6,5,1,2,3,0,4}, 
double b[5] = {5.5,4.4,1.1,3.3,2.2};

The function should sort both these arrays.

Expected Output:

The expected output will be displayed as a string with the sorted int array appended first and sorted double array appended after.

0 1 2 3 4 5 6 7 8 9 1.1 2.2 3.3 4.4 5.5 

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy