Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Median Finding Algorithm: A Linear Time Selection Algorithm, Schemes and Mind Maps of Algorithms and Programming

The Median Finding Algorithm, a linear time selection algorithm used to find the k-th smallest element in a set of unordered numbers. The algorithm uses a divide and conquer strategy and elimination to reduce running time. It is compared to a simple sorting algorithm like Heapsort and is shown to have a better running time.

What you will learn

  • What is the divide and conquer strategy used in the Median Finding Algorithm?
  • What is the Median Finding Algorithm and how does it work?
  • What is the time complexity of the Median Finding Algorithm?
  • What is elimination and how is it used in the Median Finding Algorithm?
  • How does the Median Finding Algorithm compare to a simple sorting algorithm like Heapsort?

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 09/12/2022

aseema
aseema 🇺🇸

4.5

(11)

240 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Median Finding Algorithm
Submitted By:
Arjun Saraswat
Nishant Kapoor
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Median Finding Algorithm: A Linear Time Selection Algorithm and more Schemes and Mind Maps Algorithms and Programming in PDF only on Docsity!

Median Finding Algorithm

Submitted By:

Arjun Saraswat

Nishant Kapoor

Problem Definition

 Given a set of "n" unordered numbers we

want to find the "k

th

" smallest number. (k is

a n i n t e g e r b e t w e e n 1 a n d n ).

Linear Time selection

algorithm

 Also called Median Finding Algorithm.

 Find k

th

smallest element in O (n) time

in worst case.

 Uses Divide and Conquer strategy.

 Uses elimination in order to cut down

the running time substantially.

Steps to solve the problem

 Step 1: If n is small, for example n<6,

just sort and return the k

th

smallest

number in constant time i.e; O(1) time.

 Step 2: Group the given number in

s u b s e t s o f 5 i n O ( n ) t i m e.

Arrange the numbers in groups of five

Find median of N/5 groups

Median of each group

Find the sets L and R

 Compare each n-1 elements with the median m and find two

sets L and R such that every element in L is smaller than M and

every element in R is greater than m.

m

L R

3n/10<L<7n/10 3n/10<R<7n/

Description of the Algorithm step

 If n is small, for example n<6, just sort and return the k the smallest

number.( Bound time- 7)

 If n>5, then partition the numbers into groups of 5.(Bound time n/5)

 Sort the numbers within each group. Select the middle elements (the

medians). (Bound time- 7n/5)

 Call your "Selection" routine recursively to find the median of n/

medians and call it m. (Bound time-T

n/

)

 Compare all n-1 elements with the median of medians m and

determine the sets L and R, where L contains all elements <m, and R

contains all elements >m. Clearly, the rank of m is r=|L|+1 (|L| is the

size or cardinality of L). (Bound time- n)

Recursive formula

 T (n)=O (n) + T (n/5) +T (7n/10)

We will solve this equation in order to get the complexity.

We assume that T (n)< C*n

T (n) = a*n + T (n/5) + T (7n/10)

Cn >= T(n/5) +T(7n/10) + an

Cn >= Cn/5+ C7n/10 + a*n

C >= 9*C/10 +a

C/10 >= a

C >= 10*a

There is such a constant that exists….so T (n) = O (n)

Why group of 5 why not some other term??

 If we divide elements into groups of 3 then we will have

T (n) = O (n) + T (n/3) + T (2n/3) so T (n) > O (n)…..

 If we divide elements into groups of more than 5, the value of

constant 5 will be more, so grouping elements in to 5 is the

optimal situation.