Question

In: Computer Science

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.

Solutions

Expert Solution


#include <bits/stdc++.h>
using namespace std;

// Given an array of size n, where every element
// is k away from its target position, sorts the
// array in O(nLogk) time.
int sortK(int arr[], int n, int k)
{
   // Insert first k+1 items in a priority queue (or min heap)
   //(A O(k) operation). We assume, k < n.
   priority_queue<int, vector<int>, greater<int> > pq(arr, arr + k + 1);

   // i is index for remaining elements in arr[] and index
   // is target index of for current minimum element in
   // Min Heapm 'hp'.
   int index = 0;
   for (int i = k + 1; i < n; i++) {
       arr[index++] = pq.top();
       pq.pop();
       pq.push(arr[i]);
   }

   while (pq.empty() == false) {
       arr[index++] = pq.top();
       pq.pop();
   }
}

// A utility function to print array elements
void printArray(int arr[], int size)
{
   for (int i = 0; i < size; i++)
       cout << arr[i] << " ";
   cout << endl;
}

// Driver program to test above functions
int main()
{
   int k = 3;
   int arr[] = { 2, 6, 3, 12, 56, 8 };
   int n = sizeof(arr) / sizeof(arr[0]);
   sortK(arr, n, k);

   cout << "Following is sorted array" << endl;
   printArray(arr, n);

   return 0;
}


Related Solutions

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
Let's say someone gives you an array that is filled with P numbers. Please next see...
Let's say someone gives you an array that is filled with P numbers. Please next see if there are two numbers whose sum equals a given number S and determine if there is two numbers that do this. Take for example, if I give you the input array to be 8, 2, 1, 7, and our variable S is 15, then the answer would be yes because 8 and 7 add up to S which in this case is 15....
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,...
Let's say that you are an athletic trainer, and you are interested in the impact of...
Let's say that you are an athletic trainer, and you are interested in the impact of different types of exercise upon resting heart beats. You run a study comparing people who do yoga, pilates, and runners. You have collected the data and you are ready to analyze it via ANOVA. Using JASP, conduct your ANOVA. Then complete the fill-in-the-blank sentence below. HBM Sport 59 Yoga 60 Yoga 60 Yoga 55 Yoga 61 Yoga 59 Yoga 58 Yoga 60 Yoga 60...
There are 1001 used car sellers, and Let's say the value they give is $0, $100,...
There are 1001 used car sellers, and Let's say the value they give is $0, $100, $200, $300, ... , $9900, $ 10000(that is, their willing price). There are many buyers who value each car higher than its current owner. For example, a car worth $3700 to a seller is worth $4700 to a buyer. In this case, obtain a balanced price. In other words, look for a price from the buyer's point of view that they think they deserve...
1. Let's say that you are looking at a table with output and cost data for...
1. Let's say that you are looking at a table with output and cost data for a firm and you observe the following: At a quantity of 10 units, the firm's marginal cost and marginal revenue both equal $.75. At a quantity of 18 units, the firm's marginal cost reaches its lowest point at $.35. At a quantity of 26 units, the firm's average total cost reaches a minimum of $.50. At a quantity of 28 units, the firm's marginal...
Let's say that you are looking to invest in two stocks A and B. Stock A...
Let's say that you are looking to invest in two stocks A and B. Stock A has a beta of 1.19 and based on your best estimates is expected to have a return of 13%. Stock B has a beta of 1.61 and is expected to earn 7%. If the risk-free rate is currently 2% and the expected return on the market is 12%, which stock(s) should you invest in, if any? Work by hand no financial calculator
3) Let's say you are a forensic chemist and need to determine the identity of an...
3) Let's say you are a forensic chemist and need to determine the identity of an unknown metal with some certainty, not just estimate it. Name at least one source of experimental uncertainty, and discuss how it can be reduced.
Can someone give me a list of conditions for a market to be efficient
Can someone give me a list of conditions for a market to be efficient
So let's say you have a beaker that contains 6.02g ammonium chloride and to this you...
So let's say you have a beaker that contains 6.02g ammonium chloride and to this you add 300.0mL of 0.450 M calcium hydroxide according to the following chemical equation: Ca(OH)2 (aq)+2NH4Cl(s) ---> CaCl2(aq)+2H2O(l)+2NH3    A) Draw the beaker at the end of the reaction - what would e in the beaker and/or around it. What would the products look like on a molecular/atom/ion level? B) How many moles of each reactant does the reaction start with? C)At STP, how many...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT