Question

In: Computer Science

Lets say that I were to give you a sorted list of P elements that was...

Lets say that I were to give you a sorted list of P elements that was followed by randomly ordered elements. Please explain how you would sort the entire, whole list? Please give a detailed reasoning and provide a good explanation (In language C++)

Please type answer if you can

Solutions

Expert Solution

I will use the below function

int sortedIndex(int array[],int value) {
    int low = 0,high = sizeof(array)/sizeof(array[0]);;

    while (low < high) {
        int mid = (low + high) >> 1;
        if (array[mid] < value) low = mid + 1;
        else high = mid;
    }
    return low;
}

Explanation

As intially P elements are sorted so i will pass array from 0 to P elements and the P+1 element to my function, now what the function does is relate to binary search , we are given sorted array and an element which we need to insert to array so that the resulting array is also sorted.

To find the position we divide the array and find mid and then compare mid with the element and until we find the right place of element we keep on doing this(hope you know binary search). Now we return the index, now in main function as we got the index where we need to insert it we insert it there and shift each element. Now again do this your new P is P+1 array and next element is P+2, keep on doing until you reach last element.

 

 

Related Solutions

Let's say someone were to give you a sorted list of P elements that were followed...
Let's say someone were to give you a sorted list of P elements that were followed by f(P) = O(sqrt(P)) randomly ordered elements. Please show how you would sort the whole list? In the c++ language, please show and describe.
Suppose I have a list of 17 sorted elements (A0 ---- A16) and an integer x.....
Suppose I have a list of 17 sorted elements (A0 ---- A16) and an integer x.. Assuming that we compare x with the first element A0, if x is equal to A0 , we found x and we return 1; otherwise we jump 4 elements (to A4). If x is equal to A4, we return 5 (index + 1), otherwise if it is less we back up 1 space to A3; if x is equal to A3 we return 4,...
Lets say I have a 2 firm game with homogenous products with the demand function: P...
Lets say I have a 2 firm game with homogenous products with the demand function: P = A - BQ What would be my theoretical Consumer, Producer and Total Surplus for Cournot, Stackelberg and Bertarand competitions?
lets say i make a PYTHON code that lets a user guess a number between 1-1000,...
lets say i make a PYTHON code that lets a user guess a number between 1-1000, every failed attempt you get a hint (go lower or go higher) how can i penalize the user if they dont follow the hints, example ( go higher!... your next pick: a smaller number... 2 you loose $100 for not following hint) 2 and the user has unlimited attempts, until he guesses the number, this is made using random.randint(1,1000) function THE ONLY THING IM...
Lets say for a sample of data the correlation coefficient is zero. I know this means...
Lets say for a sample of data the correlation coefficient is zero. I know this means there is no linear relationship, and if you tried to fit the data by a least-squares then the slope would also be zero. But if you did this analysis, could you get a nonzero y-intercept? I think you could, but curious to see what other people think.
Lets say I owned a business worth $100 million dollars and I have 25% stake in...
Lets say I owned a business worth $100 million dollars and I have 25% stake in the company. Lets say 100% of the what I own in the company will be taxable and I am married and it will be filed jointly. Company's net income is $17.5 million. If I was selling the company how much estate tax attributable would I be facing filing jointly?
Lets say I owned a business worth $100 million dollars and I have 25% stake in...
Lets say I owned a business worth $100 million dollars and I have 25% stake in the company. Lets say 100% of the what I own in the company will be taxable and I am married and it will be filed jointly. Company's net income is $17.5 million. What would be the estate tax attributable, 100% interest will be taxable and filling jointly? Also can you please mention some tax laws that would be beneficial in this situation?
Lets say you wanted to predict success in a college course - that is, you wanted...
Lets say you wanted to predict success in a college course - that is, you wanted to isolate some variables that have a causal influence on good performance in class. What are some potential INDEPENDENT variables that you might manipulate in an experimental design to test their effect on the DEPENDENT variable of course performance?
Lets say you have a contract awarded and services begin to be delivered, and then the...
Lets say you have a contract awarded and services begin to be delivered, and then the supplier calls you and says he can no longer deliver per the terms of the contract. What do yo do, what do you think about?
Lets say you are a foreign student studying in US and you have to pay $15000...
Lets say you are a foreign student studying in US and you have to pay $15000 tuition next August. You are worried that your home currency may depreciate compare to dollar. What can you do to avoid that risk? PROFESSOR'S GUIDANCE THIS WEEK'S LE: This is an example of an exposure to a foreign currency and ways one can eliminated the risk of that exposure in advance. Remember risk in finance in the uncertainty with respect to the outcome. In...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT