Please answer by mathematical language:
An array of n elements is almost sorted if and only if every element is at most k spots away from its actual location.
Assuming that you can only perform pairwise comparisons, formally prove that any algorithm which can sort almost sorted arrays must have running time Ω(n log k), You may assume that n is a multiple of k.
We can sort such arrays more efficiently with the help
of Heap data structure. Following is the detailed process
that uses Heap.
1) Create a Min Heap of size k+1 with first k+1 elements. This will
take O(k) time (See this GFact)
2) One by one remove min element from heap, put it in result array,
and add a new element to heap from remaining elements.
Removing an element and adding a new element to min heap will take Logk time. So overall complexity will be O(k) + O((n-k)*logK).
**Program in c++
#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 arrayn";
printArray(arr, n);
return 0;
}
Output:
Following is sorted array 2 3 6 8 12 56
The Min Heap based method takes O(nLogk) time and uses O(k) auxiliary space.
Please answer by mathematical language: An array of n elements is almost sorted if and only...
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"...
Let us suppose that there are n elements in the un-sorted array. Answer the following? q1: How is merge sort different from quick sort? q2: What is the split ratio in merge sort? q3: What is the worst-case/average-case/best-case running time of Merge Sort? q4: Why is the worst case running time of Merge sort O(n log n) always? q5: Why does Merge Sort use a static tree in the recursion process? (It is worth noting that the Quick Sort use...
4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...
the two problems are related. Please explain your
answer in full detail
Problem 1: On input a Binary Search Tree T show how to output an array that contains all the elements in T in sorted order. What's the running time of your algorithm? Does it perform any comparisons? Problem 2: Your classmate claims that they have an algorithm that on input n elements, constructs a Binary Search Tree T with those n elements using only O(n) comparisons. Can you...
the
programming language is in java
Problem 2 You are given an array A with n distinct elements. Implement an (n log n)-time algorithm that creates an array B where all elements are in range from 0 to n - 1 and where the order of elements is the same as in A. That is, 0 has the same index in B as the smallest element in A, 1 has the same index in B as the second smallest element...
I need help In the lecture you got acquainted with the median algorithm, which calculates the median of an unsorted array with n∈N elements in O (n). But the algorithm can actually do much more: it is not limited to finding only the median, but can generally find the ith element with 0≤i <n. Implement this generic version of the median algorithm by creating a class selector in the ads.set2.select package and implementing the following method: /** * Returns the...
1. Consider an array of n distinct values in which the first n − 1 values are sorted, and the last element is not. (It could be smaller than the first element, larger than element n − 1, or anywhere in between.) Give the worst-case number of comparisons that Insertion Sort will perform in this scenario. You can give your answer in terms of big-Theta if you wish to ignore low-order terms and constants.
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
An array A of n integers is called semi-sorted if it is increasing until some index and then decreasing afterwards. In other words, there is some index 1 ≤ p ≤ n such that: • A[i] < A[i + 1] for 1 ≤ i < p, and • A[i] > A[i + 1] for p ≤ i < n. Note that in this case, A[p] is the maximum element of A. Give an algorithm with running time O(logn) that finds...
Please answer in Java for an immediate upvote :) Given the following array of 8 elements, trace the merge sort algorithm. Assume the array is to be sorted in ascending order. 81 16 4 6 34 11 23 67 ANSWER: (Hint 6 – lines): Why don’t we select the median element of all the n-entries in the array to be our pivot point? ANSWER: