Question

Given an n-element unsorted array A of n integers and an integer k, describe and implement...

Given an n-element unsorted array A of n integers and an integer k, describe and implement a recursive algorithm for rearranging the elements in A so that all elements less than or equal to k come before any elements larger than k. What is the running time of your algorithm? Please prove the running time in the comments . in c++ please

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program screenshots:

Sample output:

Code to copy:

// Include the required header files.

#include <iostream>

// Use the standard namespace.

using namespace std;

// Define the function to rearrange the elements.

void rearrange(int A[], int k, int start, int end)

{

    // Return if the starting index is equal

    // to the last index.

    if(start == end)

    {

        return;

    }

    // Otherwise, do the following.

    else

    {

        // Swap the element at A[start] with the

        // element at A[end] if the value of A[start]

        // is greater than k.

        if(A[start] > k)

        {

            int temp = A[start];

            A[start] = A[end];

            A[end] = temp;

            // Decrement the value of end by 1

            // and call the function recursively.

            rearrange(A, k, start, end-1);

        }

        // Otherwise, increment the value of start

        // by 1 and call the function recursively.

        else

        {

            rearrange(A, k, start+1, end);

        }

    }

}

// Define the main() function.

int main()

{

    // Declare the required variables.

    int A[] = {100,-1, 4,3,2,0,23,34,6,7,102};

    int n = sizeof(A)/sizeof(A[0]);

    int k = 20;

    cout << "k = " << k << endl;

    cout << "The original array is as follows:" << endl;

    // Start the loop to print the original array.

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

    {

        cout << A[i] << " ";

    }

    // Call the function to rearrange the elements.

    rearrange(A, k, 0, n-1);

    cout << endl;

    cout << "\nThe modified array is as follows:" << endl;

    // Start the loop to print the modified array.

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

    {

        cout << A[i] << " ";

    }

    // Return 0 and exit the program.

    return 0;

}

The running time of the above code/algorithm is as follows:

  • The function rearrange() is called initially with the starting value of start = 0 and end = size of the array – 1.
  • In each recursive call, the function is either called by increasing the value of start by 1 or decrement the value of end by 1.
  • The base case returns from the recursive calls when the value of start and end is equal.
  • Therefore, the total number of recursive calls made by the function is equal to n, where n is the size of the input array.
  • All the other operations are performed in constant time.

Hence, the total running time of the algorithm is O(n) + C.

Add a comment
Know the answer?
Add Answer to:
Given an n-element unsorted array A of n integers and an integer k, describe and implement...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT