Order Statistics:
A. Given two sorted arrays X and Y of equal size n, use Quick-Select to find the median of all 2n elements in arrays X and Y. Can you improve Quick-Select by choosing better pivots at every step?(in java)
B. Show some experiments of your improved Quick-Select with arrays of size n=50. You can assume, if you want, that all the elements in the two arrays are distinct.
A:-here two arrays are x and y of the same size n
#include<bits/stdc++.h>
using namespace std;
int median(int [], int); /* to get median of a sorted array */
/* This function returns median of x[] and y[].
Assumptions in this function:
Both x[] and y[] are sorted arrays
Both have n elements */
int getMedian(int x[], int y[], int n)
{
/* return -1 for invalid input */
if (n <= 0)
return -1;
if (n == 1)
return (x[0] + y[0])/2;
if (n == 2)
return (max(x[0], y[0]) + min(ar1[1], y[1])) / 2;
int m1 = median(x n); /* get the median of the first array */
int m2 = median(y, n); /* get the median of the second array */
/* If medians are equal then return either m1 or m2 */
if (m1 == m2)
return m1;
/* if m1 < m2 then median must exist in ar1[m1....] and
y[....m2] */
if (m1 < m2)
{
if (n % 2 == 0)
return getMedian(x + n/2 - 1, y, n - n/2 +1);
return getMedian(x + n/2, y, n - n/2);
}
/* if m1 > m2 then median must exist in x[....m1] and
y[m2...] */
if (n % 2 == 0)
return getMedian(y + n/2 - 1, x, n - n/2 + 1);
return getMedian(y + n/2, x, n - n/2);
}
/* Function to get median of a sorted array */
int median(int arr[], int n)
{
if (n%2 == 0)
return (arr[n/2] + arr[n/2-1])/2;
else
return arr[n/2];
}
/* Driver program to test above function */
int main()
{
int x[] = {1, 2, 3, 6};
int y[] = {4, 6, 8, 10};
int n1 = sizeof(x)/sizeof(x[0]);
int n2 = sizeof(y)/sizeof(y[0]);
if (n1 == n2)
printf("Median is %d", getMedian(x, y, n1));
else
printf("Doesn't work for arrays of unequal size");
return 0;
}
The idea of the quick select is quite simple: just like with
quicksort, select a random element from the list, and place every
item that is smaller to the first half of the array, and every
element that is equal to or greater than the pivot, in the second
half (the ‘half’ is not entirely correct, as it is possible that
the result will not be exactly ‘half’).
So a step would look like this:
Arr = [5 1 4 3 2] Pivot = [4] Steps: swap [5] and [2] as 5>=4 and 2< [2 1 4 3 5] swap [4] and [3] as 4>=4 and 3<4 [2 1 3 4 5]
if we are looking for the first 3 elements, we can stop, we
found them. If we are looking for the 3rd element, we need more
iteration, but we know we must look for it in the first half of the
array hence we can ignore the rest:
Arr = [2 1 3 …] Pivot = [1] Steps: swap [2] and [1] as 2>=2 and 1<2 [1 2 3 …]
So it is quite clear that this algorithm runs in linear time using java
b:-
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/* The main function that implements QuickSort
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);
// Separately sort elements before
// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5,......n};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Order Statistics: A. Given two sorted arrays X and Y of equal size n, use Quick-Select...
Given two sorted arrays A and B of numbers. The size of A is N and the size of B is n + 1. Say that the numbers in A U B are pairwise distinct (no value returns more than once). Note that A U B has odd size because its 2n + 1. Hence the median is unique. Give an algorithm that returns the median. PSEUDOCODE ONLY.
in Java Implement merging two sorted arrays into one sorted array. These arrays are sorted in descending order. For example [5, 4, 2]. Return an array with all the elements of these two arrays sorted, also in descending order. partb: if the two arrays as input are size n each. What is the big-O run time and space complexity of your implementation public int[] merge(int[] arr1, int[] arr2){
I need help with this .java implementation: Implement an algorithm that given two sorted arrays of size m and n creates a sorted array of size (n + m) in O(n + m) time. The script must take as an input a command line argument specifying the name of a .txt file which will contain the arrays to be sorted. Each line in the .txt file will contain two arrays, with elements in arrays separated by commas and the arrays...
Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[1] · · · X[n] and Y [1] · · · Y [n] of integers. For simplicity, assume that n is a power of 2. Problem is to design an algorithm that determines if there is a number p in X and a number q in Y such that p + q is zero. If such numbers exist, the algorithm returns true; otherwise, it returns false....
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...
Suppose that you have two sorted numerical arrays A[1 . . . m] and B[1 . . . n]. You want to compute the k’th smallest number in the merged array of all m + n elements. Please design a divide- and-conquer algorithm that can do so in O(log(m + n)). You can assume for simplicity that k is even.
(25pts) You are given two sorted lists of size m and n. Give an O(log m log n) time algorithm for computing the k-th smallest element in the union of the two lists Note that the only way you can access these values is through queries to the databases. Ina single query, you can specify a value k to one of the two databases, and the chosen database will return the k-th smallest value that it contains. Since queries are...
program in python
Randomness can be used to improve the performance of deterministic algorithms which need to make many choices. Rather than repeatedly making fixed, hard-coded choices, a pseudorandom number generator can be used to make dynamic, unbiased choices. If the benefits of "good" choices outweigh the costs of "bad" choices, a random selection of good and bad choices can improve the performance of an algorithm Let us explore this with the QUICKSELECT algorithm. Discovered by the influential computer science...
3. (20 pts.) You are given two sorted lists of numbers with size m and n. Give an O(logn+ logm) time algorithm for computing the k-th smallest element in the union of the two lists. 4. (20 pts.) Solve the following recurrence relations and give a bound for each of them. CMPSC 465, Fall 2019, HW 2 (a) T(n) = 117(n/5)+13n!.3 (b) T(n) = 2T (n/4)+nlogn (c) T(n) = 5T (n/3) +log-n (d) T(n) = T(n/2) +1.5" (e) T(n) =...
Select all that apply.
Below are summary statistics for the sizes (in acres) of some local vineyards. Using the summary statistics, complete parts a) through c) below. Variable N Mean StDev Minimum Q1 Median Q3 Maximum Acres 36 46.00 47.18 17.75 33.50 53.83 250 OA. 300 225 150 225 75 Size Size Size What additional information would you need to complete the boxplot? Select all that apply A. The largest value inside the fence is needed in order to plot...