Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

.. Design and implement three functions, two of which will use recursion. The fi

ID: 3546414 • Letter: #

Question

..

Design and implement three functions, two of which will use recursion. The first is a sorting function that will sort an array when given the array and the size. The function will use recursion to sort the array using the divide and conquer method. The second is a searching function that also uses recursion and implements binary search on the array. The last function will sort the array iteratively (without recursion). Below are 3 functions that needs implementation: void sortingArray::recursiveSort(/*put your parameters */) { } int sortingArray::binarySearch(int searchval, int begind, int endind) { //this should return the index the searchvalue is in //return -1 if not found return 0; } void sortingArray::iterativeSort() {

Explanation / Answer

#include <iostream>

#include <string>

void DoMerge(int numbers[], int left, int mid, int right)

{

    int temp[25];

    int i, left_end, num_elements, tmp_pos;

    left_end = (mid - 1);

    tmp_pos = left;

    num_elements = (right - left + 1);

    while ((left <= left_end) && (mid <= right))

    {

        if (numbers[left] <= numbers[mid])

            temp[tmp_pos++] = numbers[left++];

        else

            temp[tmp_pos++] = numbers[mid++];

    }

    while (left <= left_end)

        temp[tmp_pos++] = numbers[left++];

    while (mid <= right)

        temp[tmp_pos++] = numbers[mid++];

    for (i=0; i < num_elements; i++)

        numbers[right--] = temp[right];

}

void MergeSort(int numbers[], int left, int right)

{

int mid;

if (right > left)

{

    mid = (right + left) / 2;

   MergeSort(numbers, left, mid);

    MergeSort(numbers, (mid+1), right);

    DoMerge(numbers, left, (mid+1), right);

}

}

void MergeSortHelper(int numbers[], int array_size)

{

    MergeSort(numbers, 0, array_size - 1);

}

void max()

{

    int max;

    std::cout << " Program for Ascending order of Numeric Values using MERGE SORT";

    std::cout << " Enter the total number of elements: ";

    std::cin >> max;   

    int *numarray = new int[max];

    for(int i = 0; i < max; i++)

    {

        std::cout << " Enter [" << i + 1 << "] element: ";

        std::cin >> numarray[i];

    }

    std::cout << "Before Sorting   : ";

    for(int k = 0; k < max; k++)

        std::cout << numarray[k] << " ";

    std::cout << " ";

    MergeSortHelper(numarray,max);

   std::cout << " The numbers in ascending orders are given below: ";

    for(int i = 0; i < max; i++)

    {

        std::cout << "Sorted [" << i + 1 << "] element: ";

        std::cout << numarray[i];

        std::cout << " ";

    }

    delete [] numarray;

}

2.