Question

In: Computer Science

Pseudocode and algorithm of finding median of unordered array in linear time.

Pseudocode and algorithm of finding median of unordered array in linear time.

Solutions

Expert Solution

This is a special case of a selection algorithm that can find the kth smallest element of an array with k is the half of the size of the array. There is an implementation that is linear in the worst case.

Generic selection algorithm

First let's see an algorithm find-kth that finds the kth smallest element of an array:

find-kth(A, k)
  pivot = random element of A
  (L, R) = split(A, pivot)
  if k = |L|+1, return pivot
  if k ≤ |L|  , return find-kth(L, k)
  if k > |L|+1, return find-kth(R, k-(|L|+1))

The function split(A, pivot) returns L,R such that all elements in R are greater than pivot and L all the others (minus one occurrence of pivot). Then all is done recursively.

This is O(n) in average but O(n2) in the worst case.

Linear worst case: the median-of-medians algorithm

A better pivot is the median of all the medians of sub arrays of A of size 5, by using calling the procedure on the array of these medians.

find-kth(A, k)
  B = [median(A[1], .., A[5]), median(A[6], .., A[10]), ..]
  pivot = find-kth(B, |B|/2)
  ...

This guarantees O(n) in all cases.


Related Solutions

1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element...
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element in an array of n numbers. 5. Find the order of growth for solutions of the following recurrences. a . T(n)=4T(n/2)+n,T(1)=1 b. T(n)=4T(n/2)+n2,T(1)=1 c. T(n)=4T(n/2)+n3,T(1)=1
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ).
Use pseudocode to describe an algorithm for the procedure: int[][] find_pairs( int array[], int value ). Given a sorted array of distinct integers and a single integer value, find all pairs of indexes in the list whose product is equal to the value. Return the index pairs on the same row of a two-dimensional array. Note, m will be the number of rows and total number of pairs found, while 2 will be the number of columns along every row...
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as...
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as low as possible to find two indexes i and j such that A[i] + A[j] = 100. Indexes i and j may or may not be the same value.
Problem 1: (10 marks) ALGORITHM COMPLEXITY:Write an algorithm (pseudocode but not a program) that takes a...
Problem 1: ALGORITHM COMPLEXITY:Write an algorithm (pseudocode but not a program) that takes a positive numberias an input, where i=a1a2...an,n>=3(numberwith 3 or more digits, every digit can have the value between 0 and 9–both inclusive). The algorithm should find the largest number jsuch that j=b1..bm, 1<=m<=n(numberwith m digits),andevery bk<=bk+1where1<=k<m and bk,bk+1ε{a1, a2, an}.Also, perform the computational complexity (time complexity)analysis of your algorithm and write the algorithm complexity in Big-Oh notation. Examples:Input: 23720, Output: 237Input: 9871, Output: 9Input: 1789, Output:1789Input: 2372891,...
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ......
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ... n - 1]) if (n = 2) and A[0] > A[1] swap A[0] and A[1] else if n > 2 m = ceiling(2n/3) STOOGESORT(A[0 ... m - 1]) STOOGESORT(A[n - m ... n - 1]) STOOGESORT(A[0 ... m - 1])
Design a linear-time algorithm that verifies that the height information in an AVL tree is correctly...
Design a linear-time algorithm that verifies that the height information in an AVL tree is correctly maintained and that the balance property is in order. (C++)
In Liinear-time selection algorithm where the input array of numbers are cut into groups of size...
In Liinear-time selection algorithm where the input array of numbers are cut into groups of size 5. Show that, when the group size is 7, the algorithm still runs in linear time.
Write a pseudocode or java code implementation for linear search that searches elements based on a key value from an array of undetermined length
Write a pseudocode or java code implementation for linear search that searches elements based on a key value from an array of undetermined lengthJAVA
I'm having a hard time finding this equation: calculate the absolute uncertainty in the linear mass...
I'm having a hard time finding this equation: calculate the absolute uncertainty in the linear mass density ??
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT