Question

Full and complete answer in order to get credit. Thank you. Suppose you are given an...

Full and complete answer in order to get credit. Thank you.

Suppose you are given an unsorted list of n distinct numbers. However, the list is close to being sorted in the sense that each number is at most k positions away from its original position in the sorted list. For example, suppose the sorted list is in ascending order, then the smallest element in the original list will lie between position 1 and position k + 1, as position 1 is its position in the original sorted list. Design an efficient algorithm that sorts such a list. The running time of your algorithm should depend on both n and k (a helpful sanity check whether your running time bound makes sense is that, when k is close to n, then the input list is just a generic unsorted list).

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


public static void sortK(int[] arr, int k){
       PriorityQueue<Integer> queue = new PriorityQueue<Integer>(k+1);

       // adding first (k+1) elements in Heap
       for(int i=0; i<=k && i<arr.length; i++)
           queue.add(arr[i]);

       // Processing others elements
       for(int i = k+1, ti = 0; ti < arr.length; i++, ti++){
           // If there are remaining elements, then place root of heap at target index and add arr[i]
           // to Min Heap
           if(i < arr.length){
               int root = queue.poll();
               arr[ti] = root;
               queue.add(arr[i]);
           }

           // Otherwise place root at its target index and reduce heap size
           else{
               int root = queue.poll();
               arr[ti] = root;
           }
       }
   }

Please let me know in case of any issue

Add a comment
Know the answer?
Add Answer to:
Full and complete answer in order to get credit. Thank you. Suppose you are given an...
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
  • 2. Here is a sorting algorithm that I like to use. Given an unsorted list of...

    2. Here is a sorting algorithm that I like to use. Given an unsorted list of size n, let Xx represent the data in location k of the unsorted list. Compare xi to X2 and switch if necessary so that they are in sorted order, smallest first. Compare Xn-1 and Xn and switch if necessary so that they are in sorted order, smallest first. • Compare x3 with its left neighbors, switching if necessary so that the 3 first entries...

  • 7. An alternate version of bubblesort sorts the list in ascending order by moving the smallest va...

    7. An alternate version of bubblesort sorts the list in ascending order by moving the smallest value to the first position on the first pass and so on. The pseudocode for this version is shown below. The procedure swap is used to interchange the values in its two arguments. In the box, rewrite the contents of the list 5, 2, 3, 4, 1 every time it changes, in this5, 2, 3, 4, 1 new version of bubblesort. Walk through each...

  • problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to...

    problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to write its pseudo-code). To sort an array A, you will then call Det-QuickSort(A, 1, n). You also need to provide the worst case time complexity analysis of your algorithm. 2. (20 points) Given a set of n distinct numbers, we wish to find the k largest in sorted order using a comparison-based algorithm. Give an algorithm that implements each of the following methods, and...

  • 5. Answer the following. (a) (5 points) Suppose you are given a maxheap of n unique...

    5. Answer the following. (a) (5 points) Suppose you are given a maxheap of n unique numbers. Explain where will the smallest of these n numbers be located in the maxheap. Explain where will the second largest number be located on this maxheap. Please be specific. (b) (5 points) Suppose you are given an array A of n numbers, where all the elements of the array are already sorted in decreasing order. Is this a max-heap? Explain. (c) (5 points)...

  • Full and complete answer in order to give credit. thank you. We learned in class the...

    Full and complete answer in order to give credit. thank you. We learned in class the hare-tortoise algorithm of cycle-finding in a linked list. Based on this, design an algorithm that is both time and space efficient and finds the starting node of the cycle.

  • Question 2 (8 marks) (From the DPU textbook, Exercise 2.5) Using the Master theorem or recursion...

    Question 2 (8 marks) (From the DPU textbook, Exercise 2.5) Using the Master theorem or recursion tree methods, solve the following recurence relations and give Θ bound for each of them a) T(n) 9T(n/3) +3n2 12n - 4, where n 21 b) T(n)-T(n-1) + n", where n > 1 and c > 0 c) T(n) = T(Vn) + 1, where n > b and T(b) = 2. Question 3: 10 marks) Suppose we have a subroutine merge2 to merge two...

  • Suppose you are given k sorted arrays of size n. Give an algorithm, that runs in...

    Suppose you are given k sorted arrays of size n. Give an algorithm, that runs in O(nk log k)time, that merges them into a single list.

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

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

  • Suppose you have two queues. Queue source is full of random data and queue dest is...

    Suppose you have two queues. Queue source is full of random data and queue dest is empty. To sort the data in the source, you can dequeue and enqueue all items in source (cycling the values back to their original locations), remembering the smallest value you see along the way. Then, you can dequeue all items in source a second time, enqueuing them all back in source except for the smallest, which you enqueue in dest instead. If you repeat...

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