SlideShare a Scribd company logo
1 of 21
Medians & Order Statistics
Data Structures & Algorithms
Medians & Order Statistics
What are they?
 The ith order statistic of a set of n
elements is defined as the ith smallest
element in the set.
 E.g., the minimum order statistic is the first
order statistic, the max is the last order statistic
 The median is informally the “halfway”
point – there are one (if n is odd) or two (if
n is even)
Medians & Order Statistics
This chapter deals with finding a
particular order statistic in a set
 We know we can use a sorting algorithm to
find an order statistic in O(nlog2n) time, by
sorting the data first
 There are faster algorithms, however, that
don’t require a sort
Minimum and Maximum
A basic algorithm is (O(n)) – just scan the set
and keep track of the smallest
 This is the best we can do – in order to find the
min (or max) we must compare every element;
since we’ve done this in O(n) time, you can’t get
any better
Type FindMin(Type data[], int length)
{
Type min = data[0];
for ( int i = 1 ; i < length ; ++i )
if ( data[i] < min ) min = data[i];
return min;
}
Simultaneous min and max
Sometimes its useful to come up with both at
the same time
We can run separate algorithms, or modify
the original to keep track of both
What about finding the second smallest
element?
Write an algorithm to compute the second smallest
element. How many comparisons are required?
Selection in Expected Linear Time
A general solution would be more useful
 It would seem like the type of problem that
would be hard to solve – at least O(nlog2n)
 In reality it can be accomplished in O(n),
using a divide-and-conquer algorithm
Randomized Select
Recall the Randomized QuickSort algorithm
 As in QuickSort, the idea is to partition the input
recursively
 Unlike QuickSort, Randomized Select only cares
about one of the partitions – the partition
containing the order statistic you’re looking for
 The expected running time for this algorithm is
O(n)
Randomized Select requires the Randomized
Partition algorithm previously discussed
The Randomized Select Algorithm
RandomizedSelect(A, begin, end, i)
{
if (begin == end ) return A[begin];
q = RandomizedPartition(A, begin, end)
k = q – begin + 1
if ( i <= k )
return RandomizedSelect(A, begin, q, i)
else
return RandomizedSelect(A, q+1, end, i-k)
}
The Randomized Select Algorithm
First, partition the array
 This guarantees that all the elements in
A[begin..q] are less than all the elements in
A[q+1..end]
Then compute how many elements are in the
array A[begin..q]
 This is just q-begin+1 (since begin may be non-
zero)
 This also happens to be the order statistic of the
partition element
The Randomized Select Algorithm
Because of the partitioning, we know which
partition the order statistic must be in
 If the order statistic is less than k, then recursively
search the left partition for order statistic i
 If the order statistic is to the right of k, then
recursively search the right partition for order
statistic i – k
 We already know that k values are smaller than the
smallest element in this partition
 We’re looking for the (i-k)th smallest element in that
partition
Analysis of Randomized Select
Worst case, O(n2)
 We could get unlucky and partition around
the largest or smallest remaining element
 This is unlikely, since it’s randomized
The average case is somewhat more
complicated (see the formula on 189)
but amounts to O(n)
Generic Programming
Generic programming is “programming using
types as parameters”
The idea of generic programming is to write
code that is data-type independent
 Many algorithms and data structures that we
discuss will operate independently of data type
 Generic programming provides a way of writing
the code once, then specifying the data type to
operate on later
Reference: The C++ Programming Language, by
Bjarne Stroustrup
Generic Programming in C++
The principle of generic programming in
C++ is implemented via templates
 Templates provide a way to represent a
wide range of general concepts and simple
ways to combine them
Template Functions
The C++ compiler deduces the template arguments
from the function arguments
Calling this function is the same as calling any other
function:
int some_min = FindMin(some_array, SIZE);
template <typename Type>
Type FindMin(Type data[], int length)
{
Type min = data[0];
for ( int i = 1 ; i < length ; ++i )
if ( data[i] < min ) min = data[i];
return min;
}
Template Functions
You are not limited to one template
parameter
 Multiple parameters are listed as a comma
separated list:
template <typename T, typename U> …
Template parameters aren’t even
limited to typenames:
template <typename T, int i> …
Template Functions
There are rare occasions when the compiler
cannot deduce the type of the template
argument
 E.g., when the argument is only used as a return
type
 In these cases, explicit specification can be used
template <typename T>
T* create()
{
return new T;
}
…
SomeClass *p = create<SomeClass>();
Template Functions
Template functions can also be overloaded,
both with other template functions and with
non-template functions
 This may be required in a number of situations:
 For some types, you can use a different (more efficient)
algorithm
 Multiple type deductions can be made, such that the
compiler can’t decide which version to use
template <typename T> T sqrt(T);
template <typename T> complex<T> sqrt(complex<T>);
double sqrt(double);
Template Classes
C++ also provides for template classes
template <typename T>
class SomeArray {
public:
SomeArray();
T& ItemAt(int index);
void SetItemAt(int index, const T& value);
…
private:
T m_data[SIZE];
};
Template Classes
Instantiating an object from a template class
takes a little more work
 You must specify the type
 The resulting object can be used like any other
object
SomeArray<double> array_of_doubles;
array_of_doubles.SetItemAt(0, 2.0);
double d = array_of_doubles.ItemAt(0);
Template Classes
Like function templates, template parameters
are not limited to only generic types
 Other data can also be provided:
template <typename Type, int Storage>
class SomeArray {
public:
SomeArray();
Type& ItemAt(int index);
…
private:
Type m_data[Storage];
};
…
SomeArray<double, 1000> array_of_doubles;
Some Cautions About
Templates
Templates provide a convenient way of writing an algorithm or
data structure only once
For each instantiated template, the compiler creates a separate
piece of compiled code
 E.g., SomeArray<double>, SomeArray<int>, and
SomeArray<SomeClass> creates three different implementations
of SomeArray in memory
 Templates are considered a primary contributor to code bloat
because of this property
Care should be taken in template classes to only include
methods that depend on the templated type
 Other functionality can be moved to a standalone function, or to a
non-template base class

More Related Content

What's hot

Algorithem complexity in data sructure
Algorithem complexity in data sructureAlgorithem complexity in data sructure
Algorithem complexity in data sructure
Kumar
 
Cupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithmCupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithm
TarikuDabala1
 

What's hot (20)

U nit i data structure-converted
U nit   i data structure-convertedU nit   i data structure-converted
U nit i data structure-converted
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Data Structure and Algorithms
Data Structure and AlgorithmsData Structure and Algorithms
Data Structure and Algorithms
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
Data structure
Data structureData structure
Data structure
 
Algorithem complexity in data sructure
Algorithem complexity in data sructureAlgorithem complexity in data sructure
Algorithem complexity in data sructure
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Data structures
Data structuresData structures
Data structures
 
Lecture 1 and 2
Lecture 1 and 2Lecture 1 and 2
Lecture 1 and 2
 
Big o notation
Big o notationBig o notation
Big o notation
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Data structure and algorithm notes
Data structure and algorithm notesData structure and algorithm notes
Data structure and algorithm notes
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Cupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithmCupdf.com introduction to-data-structures-and-algorithm
Cupdf.com introduction to-data-structures-and-algorithm
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0Algorithms with-java-advanced-1.0
Algorithms with-java-advanced-1.0
 

Similar to Cis435 week04

Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
rajesshs31r
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
rajesshs31r
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
irdginfo
 
Module 1 notes of data warehousing and data
Module 1 notes of data warehousing and dataModule 1 notes of data warehousing and data
Module 1 notes of data warehousing and data
vijipersonal2012
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
Qundeel
 
Data Structure
Data StructureData Structure
Data Structure
sheraz1
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
Qundeel
 

Similar to Cis435 week04 (20)

Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
 
VCE Unit 01 (2).pptx
VCE Unit 01 (2).pptxVCE Unit 01 (2).pptx
VCE Unit 01 (2).pptx
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 
Module 1 notes of data warehousing and data
Module 1 notes of data warehousing and dataModule 1 notes of data warehousing and data
Module 1 notes of data warehousing and data
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Data Structure
Data StructureData Structure
Data Structure
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Algorithms.
Algorithms. Algorithms.
Algorithms.
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
 
Introduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptxIntroduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptx
 
L1803016468
L1803016468L1803016468
L1803016468
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
 
Ln liers
Ln liersLn liers
Ln liers
 
test1
test1test1
test1
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
 

More from ashish bansal (13)

Data struters
Data strutersData struters
Data struters
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Cis435 week03
Cis435 week03Cis435 week03
Cis435 week03
 
Cis435 week02
Cis435 week02Cis435 week02
Cis435 week02
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
35 algorithm-types
35 algorithm-types35 algorithm-types
35 algorithm-types
 
32 algorithm-types
32 algorithm-types32 algorithm-types
32 algorithm-types
 
7 stacksqueues
7 stacksqueues7 stacksqueues
7 stacksqueues
 
5 searching
5 searching5 searching
5 searching
 
4 recursion
4 recursion4 recursion
4 recursion
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 

Cis435 week04

  • 1. Medians & Order Statistics Data Structures & Algorithms
  • 2. Medians & Order Statistics What are they?  The ith order statistic of a set of n elements is defined as the ith smallest element in the set.  E.g., the minimum order statistic is the first order statistic, the max is the last order statistic  The median is informally the “halfway” point – there are one (if n is odd) or two (if n is even)
  • 3. Medians & Order Statistics This chapter deals with finding a particular order statistic in a set  We know we can use a sorting algorithm to find an order statistic in O(nlog2n) time, by sorting the data first  There are faster algorithms, however, that don’t require a sort
  • 4. Minimum and Maximum A basic algorithm is (O(n)) – just scan the set and keep track of the smallest  This is the best we can do – in order to find the min (or max) we must compare every element; since we’ve done this in O(n) time, you can’t get any better Type FindMin(Type data[], int length) { Type min = data[0]; for ( int i = 1 ; i < length ; ++i ) if ( data[i] < min ) min = data[i]; return min; }
  • 5. Simultaneous min and max Sometimes its useful to come up with both at the same time We can run separate algorithms, or modify the original to keep track of both What about finding the second smallest element? Write an algorithm to compute the second smallest element. How many comparisons are required?
  • 6. Selection in Expected Linear Time A general solution would be more useful  It would seem like the type of problem that would be hard to solve – at least O(nlog2n)  In reality it can be accomplished in O(n), using a divide-and-conquer algorithm
  • 7. Randomized Select Recall the Randomized QuickSort algorithm  As in QuickSort, the idea is to partition the input recursively  Unlike QuickSort, Randomized Select only cares about one of the partitions – the partition containing the order statistic you’re looking for  The expected running time for this algorithm is O(n) Randomized Select requires the Randomized Partition algorithm previously discussed
  • 8. The Randomized Select Algorithm RandomizedSelect(A, begin, end, i) { if (begin == end ) return A[begin]; q = RandomizedPartition(A, begin, end) k = q – begin + 1 if ( i <= k ) return RandomizedSelect(A, begin, q, i) else return RandomizedSelect(A, q+1, end, i-k) }
  • 9. The Randomized Select Algorithm First, partition the array  This guarantees that all the elements in A[begin..q] are less than all the elements in A[q+1..end] Then compute how many elements are in the array A[begin..q]  This is just q-begin+1 (since begin may be non- zero)  This also happens to be the order statistic of the partition element
  • 10. The Randomized Select Algorithm Because of the partitioning, we know which partition the order statistic must be in  If the order statistic is less than k, then recursively search the left partition for order statistic i  If the order statistic is to the right of k, then recursively search the right partition for order statistic i – k  We already know that k values are smaller than the smallest element in this partition  We’re looking for the (i-k)th smallest element in that partition
  • 11. Analysis of Randomized Select Worst case, O(n2)  We could get unlucky and partition around the largest or smallest remaining element  This is unlikely, since it’s randomized The average case is somewhat more complicated (see the formula on 189) but amounts to O(n)
  • 12. Generic Programming Generic programming is “programming using types as parameters” The idea of generic programming is to write code that is data-type independent  Many algorithms and data structures that we discuss will operate independently of data type  Generic programming provides a way of writing the code once, then specifying the data type to operate on later Reference: The C++ Programming Language, by Bjarne Stroustrup
  • 13. Generic Programming in C++ The principle of generic programming in C++ is implemented via templates  Templates provide a way to represent a wide range of general concepts and simple ways to combine them
  • 14. Template Functions The C++ compiler deduces the template arguments from the function arguments Calling this function is the same as calling any other function: int some_min = FindMin(some_array, SIZE); template <typename Type> Type FindMin(Type data[], int length) { Type min = data[0]; for ( int i = 1 ; i < length ; ++i ) if ( data[i] < min ) min = data[i]; return min; }
  • 15. Template Functions You are not limited to one template parameter  Multiple parameters are listed as a comma separated list: template <typename T, typename U> … Template parameters aren’t even limited to typenames: template <typename T, int i> …
  • 16. Template Functions There are rare occasions when the compiler cannot deduce the type of the template argument  E.g., when the argument is only used as a return type  In these cases, explicit specification can be used template <typename T> T* create() { return new T; } … SomeClass *p = create<SomeClass>();
  • 17. Template Functions Template functions can also be overloaded, both with other template functions and with non-template functions  This may be required in a number of situations:  For some types, you can use a different (more efficient) algorithm  Multiple type deductions can be made, such that the compiler can’t decide which version to use template <typename T> T sqrt(T); template <typename T> complex<T> sqrt(complex<T>); double sqrt(double);
  • 18. Template Classes C++ also provides for template classes template <typename T> class SomeArray { public: SomeArray(); T& ItemAt(int index); void SetItemAt(int index, const T& value); … private: T m_data[SIZE]; };
  • 19. Template Classes Instantiating an object from a template class takes a little more work  You must specify the type  The resulting object can be used like any other object SomeArray<double> array_of_doubles; array_of_doubles.SetItemAt(0, 2.0); double d = array_of_doubles.ItemAt(0);
  • 20. Template Classes Like function templates, template parameters are not limited to only generic types  Other data can also be provided: template <typename Type, int Storage> class SomeArray { public: SomeArray(); Type& ItemAt(int index); … private: Type m_data[Storage]; }; … SomeArray<double, 1000> array_of_doubles;
  • 21. Some Cautions About Templates Templates provide a convenient way of writing an algorithm or data structure only once For each instantiated template, the compiler creates a separate piece of compiled code  E.g., SomeArray<double>, SomeArray<int>, and SomeArray<SomeClass> creates three different implementations of SomeArray in memory  Templates are considered a primary contributor to code bloat because of this property Care should be taken in template classes to only include methods that depend on the templated type  Other functionality can be moved to a standalone function, or to a non-template base class