Question

Please answer by mathematical language: An array of n elements is almost sorted if and only...

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.

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

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.

Add a comment
Know the answer?
Add Answer to:
Please answer by mathematical language: An array of n elements is almost sorted if and only...
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
  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    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:...

    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 t...

    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...

    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...

    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...

    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 las...

    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.

  • please check my answers if it wrong answer me a) (25 points) Suppose that you are...

    please check my answers if it wrong answer me a) (25 points) Suppose that you are asked to analyze the performance. Algorithms operate on 1D array of size nor 2D a of the algorithms below, write down the Big O or order of grow terms of n. If the algorithm cannot be applied to the array, write 0(1), O(log n), O(n), O(n logn), 90), O(n"), O(n!). The will only be given for reasonable Big O answers. & algorithms for their...

  • Please answer in Java for an immediate upvote :) Given the following array of 8 elements,...

    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:

  • 3. Suppose you have an array of n random elements. You are required to perform n...

    3. Suppose you have an array of n random elements. You are required to perform n different searches on the array. What is best big-oh time for your entire task? Explain how to achieve that time. 4. Suppose you are given two sorted integer arrays int[] A and int[] B. Write a method that returns an array which contains only the common elements (elements that are present in both A and B) of these two sorted arrays. Indicate the big-Oh...

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