Question

In: Computer Science

How do I sort a sequence A = 7,2,3,14,2,8,8,3,7,10 in O(nlogm) time where n is size...

How do I sort a sequence A = 7,2,3,14,2,8,8,3,7,10

in O(nlogm) time where n is size of A and m is the number of distinct elements?

No code, just a good explanation will do

Solutions

Expert Solution

Use QuickSort Algorithm to get it sort in nlogm time.

QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.

  1. Always pick first element as pivot.
  2. Always pick last element as pivot (implemented below)
  3. Pick a random element as pivot.
  4. Pick median as pivot.

The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.

arr[] = {10, 80, 30, 90, 40, 50, 70}
Indexes:  0   1   2   3   4   5   6 

low = 0, high =  6, pivot = arr[h] = 70
Initialize index of smaller element, i = -1

Traverse elements from j = low to high-1
j = 0 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
i = 0 
arr[] = {10, 80, 30, 90, 40, 50, 70} // No change as i and j 
                                     // are same

j = 1 : Since arr[j] > pivot, do nothing
// No change in i and arr[]

j = 2 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
i = 1
arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30 

j = 3 : Since arr[j] > pivot, do nothing
// No change in i and arr[]

j = 4 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
i = 2
arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped
j = 5 : Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j] 
i = 3 
arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped 

We come out of loop because j is now equal to high-1.
Finally we place pivot at correct position by swapping
arr[i+1] and arr[high] (or pivot) 
arr[] = {10, 30, 40, 50, 70, 90, 80} // 80 and 70 Swapped 

Now 70 is at its correct place. All elements smaller than
70 are before it and all elements greater than 70 are after
it.

Related Solutions

how do i find a peak in time complexity of O(log(n)) in a list? input: a...
how do i find a peak in time complexity of O(log(n)) in a list? input: a list of numbers or integers output: a possible local peak in the list for an example: calling function [2,3,8,9,5] would return 3 im thinking about using binary search but it would require a sorted list.
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort,...
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort, or bubble sort) and one faster sort of Big O(n * log n) (mergesort or quicksort). Count the number of moves (a swap counts as one move). With mergesort, you can count the range of the part of the array you are sorting (i.e. last-first+1). Use the code from the textbook (copy from the lecture notes) and put in an extra reference parameter for...
Q1. A. What is the complexity of partition process in quick sort? O(1) O(logN) O(N) O(NlogN)...
Q1. A. What is the complexity of partition process in quick sort? O(1) O(logN) O(N) O(NlogN) B. Evaluate the following postfix expression. 2 3 4 + * C. In an array representation of heap, what are the parent node of a node a[10]? a[9] a[11] a[5] a[20] There is no easy way to access parent node in such representation. D. In an array representation of heap, what are the children nodes (if any) of a node a[10]? a[11] and a[12]...
Write a program (O(n), where n is the number of words) that takes as input a...
Write a program (O(n), where n is the number of words) that takes as input a set of words and returns groups of anagrams for those words. Complete your code here Do not change anything in the test file. CPP File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); vector> findAnagrams(const vector& dict) { // Your code here... } Test File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); int main() { vector word_list...
Given the sequence {an}∞ n=1 where an = 3ne−6n A) Justify whether the sequence is increasing...
Given the sequence {an}∞ n=1 where an = 3ne−6n A) Justify whether the sequence is increasing or decreasing. B) Is the sequence bounded? If yes, what are the bounds? C) Determine whether the sequence converges or diverges. State any reason (i.e result, theorems) for your conclusion.
How much time does an algorithm take to solve a problem of size n if this...
How much time does an algorithm take to solve a problem of size n if this algorithm uses 2n2 + 2n operations, each requiring 10-8 seconds, with these values of n? a) 10: b) 20: c) 50: d) 100
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort...
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort in Java. You have the option to choose but you must label (with comments) the algorithm you choose to implement. Convert that algorithm to a generic algorithm and constraint it to only using numerics. Your method should accept an array as a parameter and sort the content of the array. If you wish, you can throw an exception if the contents of the array...
Using the buildHeap method, write a sorting function that can sort a list in O(nlogn) time....
Using the buildHeap method, write a sorting function that can sort a list in O(nlogn) time. def buildHeap(self,alist): i = len(alist) // 2 self.currentSize = len(alist) self.heapList = [0] + alist[:] while (i > 0): self.percDown(i) i = i - 1 Base on this code please
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the...
the sort below shows an algorithm for sorting aSort(A, i, j) // where A is the array to be sorted; i is the start index and j is the end index. n = j – i + 1 If (n < 18) { sort A[i…j] by insertion-sort return } m1 = i + 2 * n / 3 m2 = i + n / 3 aSort(A, i, m1) aSort(A, m2, j) aSort(A, i, m1) questions: 1) Please state the asymptotic...
In Excel: How do I turn a series of dates into a sequence of numbers (example...
In Excel: How do I turn a series of dates into a sequence of numbers (example 1,2, 3)?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT