SlideShare a Scribd company logo
1 of 18
Medians and Order
Statistics
Order statistics
ith order statistic is the ith smallest element of a set of n elements.
We will consider some special cases of the order statistics :
The minimum, i.e. the first(i=1),
the maximum, i.e. the last(i=n),
and the median, i.e. the “halfway point."
If n is odd, the median is unique, and if n is even, there are two
medians(lower median and upper median).
Selection problem
It is a problem of computing where
Input: A set A of n distinct numbers and a number i, with 1 i  n.
Output: the element x  A that is larger than exactly i – 1 other elements of A.
Naïve Solution
We can simply sort the given numbers using heap or merge sort and then
simply giving the output as ith index of the array.
If we use a sorting algorithm then the selection problem can be solved in in
O(n lg n) time.
But using a sorting is more like using a cannon to shoot a fly since only one
number needs to computed.
So we will just find out that number. Before that let us solve the problem of
selecting the minimum and maximum of a given set of number.
Minimum and Maximum
Minimum (A)
1. min = A[1]
2. for i = 2 to A.length
3. if min > A[i]
4. Min = A[i]
5. return min
Maximum can be determined similarly.
How many comparisons are necessary and
sufficient for computing both the minimum
and the maximum?
Well, to compute the minimum n-1 comparisons are necessary and
sufficient. The same is true for the maximum. So, the number should be
2n-2 for computing both.
Is it possible to find out the minimum and maximum in less than 2n-2
comparisons?
YES , lets see how…..
Simultaneous Minimum and Maximum
Simultaneous computation of maximum and minimum can be done in at
most 3n/2 steps.
Idea: Maintain the variables min and max. Process the n numbers in pairs.
For the first pair, set min to the smaller and max to the other. After that, for
each new pair, compare the smaller with min and the larger with max.
Lets see the algorithm.
MAX-AND-MIN(A , n)
1. max = A[n], min = A[n]
2. for i = 1 to n/2
3. if A[2i - 1] ≥ A[2i]
4. { if A[2i - 1] > max
5. max = A[2i - 1]
6. if A[2i] < min
7. min = A[2i] }
8. else { if A[2i] > max
9. max = A[2i]
10. if A[2i - 1] < min
11. min A[2i - 1] }
12. return max and min
Randomized-Select
Randomized-Select (A, p, r, i)
1. if p = r
2. return A[p]
3. q = Randomized-Partition(A, p, r)
4. k =q – p + 1
5. if i = k
6. return A[q]
7. else if i < k
8. return Randomized-Select(A, p, q – 1, i)
9. else return Randomized-Select(A, q+1, r, i – k)
…
Randomized-Partition(A, p, r)
1. i = random(p,r)
2. exchange A[r] with A[i]
3. return Partition(A, p, r)
Partition(A, p, r)
1. x=A[r]
2. i=p-1
3. for j=p to r-1
4. if A[j]≤x
5. i=i+1
6. exchange A[i] with a[j]
7. exchange A[i+1] with A[r]
8. return i+1
Example
6 10 13 5 8 3 2 11
i=7
Analysis
…
…
…
…
…
Thank You !

More Related Content

What's hot

What's hot (19)

Introduction to matices
Introduction to maticesIntroduction to matices
Introduction to matices
 
Introduction of matrix
Introduction of matrixIntroduction of matrix
Introduction of matrix
 
lecture 10
lecture 10lecture 10
lecture 10
 
Matrix presentation By DHEERAJ KATARIA
Matrix presentation By DHEERAJ KATARIAMatrix presentation By DHEERAJ KATARIA
Matrix presentation By DHEERAJ KATARIA
 
M a t r i k s
M a t r i k sM a t r i k s
M a t r i k s
 
Project business maths
Project business mathsProject business maths
Project business maths
 
Matrices
MatricesMatrices
Matrices
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Lesson 1 matrix
Lesson 1 matrixLesson 1 matrix
Lesson 1 matrix
 
Matrices
MatricesMatrices
Matrices
 
Introduction to Matrices
Introduction to MatricesIntroduction to Matrices
Introduction to Matrices
 
presentation on matrix
 presentation on matrix presentation on matrix
presentation on matrix
 
Matrices & Determinants
Matrices & DeterminantsMatrices & Determinants
Matrices & Determinants
 
Matrix.
Matrix.Matrix.
Matrix.
 
MATRICES
MATRICESMATRICES
MATRICES
 
Direct Methods For The Solution Of Systems Of
Direct Methods For The Solution Of Systems OfDirect Methods For The Solution Of Systems Of
Direct Methods For The Solution Of Systems Of
 
Determinants
DeterminantsDeterminants
Determinants
 
Matrix algebra
Matrix algebraMatrix algebra
Matrix algebra
 
Matrices And Determinants
Matrices And DeterminantsMatrices And Determinants
Matrices And Determinants
 

Viewers also liked

Multivariate normal proof
Multivariate normal proofMultivariate normal proof
Multivariate normal proofCole Arora
 
Estimation theory 1
Estimation theory 1Estimation theory 1
Estimation theory 1Gopi Saiteja
 
A Measure Of Independence For A Multifariate Normal Distribution And Some Con...
A Measure Of Independence For A Multifariate Normal Distribution And Some Con...A Measure Of Independence For A Multifariate Normal Distribution And Some Con...
A Measure Of Independence For A Multifariate Normal Distribution And Some Con...ganuraga
 
lecture 11
lecture 11lecture 11
lecture 11sajinsc
 
Next higher number with same number of binary bits set
Next higher number with same number of binary bits setNext higher number with same number of binary bits set
Next higher number with same number of binary bits setGowriKumar Chandramouli
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Traian Rebedea
 

Viewers also liked (10)

Multivariate normal proof
Multivariate normal proofMultivariate normal proof
Multivariate normal proof
 
Estimation theory 1
Estimation theory 1Estimation theory 1
Estimation theory 1
 
Augmenting Data Structures
Augmenting Data StructuresAugmenting Data Structures
Augmenting Data Structures
 
Sufficient statistics
Sufficient statisticsSufficient statistics
Sufficient statistics
 
Session 9 10
Session 9 10Session 9 10
Session 9 10
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
A Measure Of Independence For A Multifariate Normal Distribution And Some Con...
A Measure Of Independence For A Multifariate Normal Distribution And Some Con...A Measure Of Independence For A Multifariate Normal Distribution And Some Con...
A Measure Of Independence For A Multifariate Normal Distribution And Some Con...
 
lecture 11
lecture 11lecture 11
lecture 11
 
Next higher number with same number of binary bits set
Next higher number with same number of binary bits setNext higher number with same number of binary bits set
Next higher number with same number of binary bits set
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
 

Similar to median and order statistics

A New Deterministic RSA-Factoring Algorithm
A New Deterministic RSA-Factoring AlgorithmA New Deterministic RSA-Factoring Algorithm
A New Deterministic RSA-Factoring AlgorithmJim Jimenez
 
M269 Data Structures And Computability.docx
M269 Data Structures And Computability.docxM269 Data Structures And Computability.docx
M269 Data Structures And Computability.docxstirlingvwriters
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyappasami
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer Swati Kulkarni Jaipurkar
 
lecture 1
lecture 1lecture 1
lecture 1sajinsc
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
FACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDED
FACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDEDFACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDED
FACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDEDZac Darcy
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforcechidabdu
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0BG Java EE Course
 
Basic of Statistics
Basic of StatisticsBasic of Statistics
Basic of StatisticsMinaxirac
 

Similar to median and order statistics (20)

algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
Ada notes
Ada notesAda notes
Ada notes
 
A New Deterministic RSA-Factoring Algorithm
A New Deterministic RSA-Factoring AlgorithmA New Deterministic RSA-Factoring Algorithm
A New Deterministic RSA-Factoring Algorithm
 
M269 Data Structures And Computability.docx
M269 Data Structures And Computability.docxM269 Data Structures And Computability.docx
M269 Data Structures And Computability.docx
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 
Cs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer keyCs6402 design and analysis of algorithms may june 2016 answer key
Cs6402 design and analysis of algorithms may june 2016 answer key
 
Slide2
Slide2Slide2
Slide2
 
finding Min and max element from given array using divide & conquer
finding Min and max element from given array using  divide & conquer finding Min and max element from given array using  divide & conquer
finding Min and max element from given array using divide & conquer
 
lecture 1
lecture 1lecture 1
lecture 1
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
FACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDED
FACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDEDFACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDED
FACTORING CRYPTOSYSTEM MODULI WHEN THE CO-FACTORS DIFFERENCE IS BOUNDED
 
Es272 ch2
Es272 ch2Es272 ch2
Es272 ch2
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforce
 
Daa notes 2
Daa notes 2Daa notes 2
Daa notes 2
 
Brute force method
Brute force methodBrute force method
Brute force method
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 
Basic of Statistics
Basic of StatisticsBasic of Statistics
Basic of Statistics
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 

More from Shashank Singh

More from Shashank Singh (7)

relational algebra
relational algebrarelational algebra
relational algebra
 
graph theory
graph theorygraph theory
graph theory
 
Network layer
Network layerNetwork layer
Network layer
 
Fibonacci numbers And Lucas numbers
Fibonacci numbers And  Lucas numbersFibonacci numbers And  Lucas numbers
Fibonacci numbers And Lucas numbers
 
Self organising list
Self organising listSelf organising list
Self organising list
 
Micro programmed control
Micro programmed  controlMicro programmed  control
Micro programmed control
 
Execution of functions
Execution of functionsExecution of functions
Execution of functions
 

Recently uploaded

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 

Recently uploaded (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 

median and order statistics

  • 2. Order statistics ith order statistic is the ith smallest element of a set of n elements. We will consider some special cases of the order statistics : The minimum, i.e. the first(i=1), the maximum, i.e. the last(i=n), and the median, i.e. the “halfway point." If n is odd, the median is unique, and if n is even, there are two medians(lower median and upper median).
  • 3. Selection problem It is a problem of computing where Input: A set A of n distinct numbers and a number i, with 1 i  n. Output: the element x  A that is larger than exactly i – 1 other elements of A.
  • 4. Naïve Solution We can simply sort the given numbers using heap or merge sort and then simply giving the output as ith index of the array. If we use a sorting algorithm then the selection problem can be solved in in O(n lg n) time. But using a sorting is more like using a cannon to shoot a fly since only one number needs to computed. So we will just find out that number. Before that let us solve the problem of selecting the minimum and maximum of a given set of number.
  • 5. Minimum and Maximum Minimum (A) 1. min = A[1] 2. for i = 2 to A.length 3. if min > A[i] 4. Min = A[i] 5. return min Maximum can be determined similarly.
  • 6. How many comparisons are necessary and sufficient for computing both the minimum and the maximum? Well, to compute the minimum n-1 comparisons are necessary and sufficient. The same is true for the maximum. So, the number should be 2n-2 for computing both. Is it possible to find out the minimum and maximum in less than 2n-2 comparisons? YES , lets see how…..
  • 7. Simultaneous Minimum and Maximum Simultaneous computation of maximum and minimum can be done in at most 3n/2 steps. Idea: Maintain the variables min and max. Process the n numbers in pairs. For the first pair, set min to the smaller and max to the other. After that, for each new pair, compare the smaller with min and the larger with max. Lets see the algorithm.
  • 8. MAX-AND-MIN(A , n) 1. max = A[n], min = A[n] 2. for i = 1 to n/2 3. if A[2i - 1] ≥ A[2i] 4. { if A[2i - 1] > max 5. max = A[2i - 1] 6. if A[2i] < min 7. min = A[2i] } 8. else { if A[2i] > max 9. max = A[2i] 10. if A[2i - 1] < min 11. min A[2i - 1] } 12. return max and min
  • 9. Randomized-Select Randomized-Select (A, p, r, i) 1. if p = r 2. return A[p] 3. q = Randomized-Partition(A, p, r) 4. k =q – p + 1 5. if i = k 6. return A[q] 7. else if i < k 8. return Randomized-Select(A, p, q – 1, i) 9. else return Randomized-Select(A, q+1, r, i – k)
  • 10. … Randomized-Partition(A, p, r) 1. i = random(p,r) 2. exchange A[r] with A[i] 3. return Partition(A, p, r) Partition(A, p, r) 1. x=A[r] 2. i=p-1 3. for j=p to r-1 4. if A[j]≤x 5. i=i+1 6. exchange A[i] with a[j] 7. exchange A[i+1] with A[r] 8. return i+1
  • 11. Example 6 10 13 5 8 3 2 11 i=7
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.