Order of Algorithm
-
Upload
ravneet-singh -
Category
Documents
-
view
217 -
download
0
description
Transcript of Order of Algorithm
University Institute of Engineering (UIE)
Medians and Order Statistics• The ith order statistic of a set of n elements
is the ith smallest element. • For example, the minimum of a set of
elements is the first order statistic (i = 1), and the maximum is the nth order statistic (i = n).
• A median, informally, is the “halfway point” of the set. When n is odd, the median is unique, occurring at i = (n+1)/2.
• When n is even, there are two medians, occurring at i = n/2 and i = n/2 + 1.
University Institute of Engineering (UIE)
Medians and Order Statistics
Straight MaxMin(a,n,max,min){ max:=min:=a[1];
for(i:=2 to n do{
if(a[i]>max) then max:=a[i];if (a[i]<min) then min:=a[i];
}}
University Institute of Engineering (UIE)
Medians and Order Statistics
MINIMUM(A)1 min← A[1]2 for i ← 2 to length[A]3 do if min > A[i ]4 then min← A[i ]5 return min
University Institute of Engineering (UIE)
Finding the Max and MinAlgorithm MaxMin (i,j,Max,min){ If (i=j) then max:=min:=a[i];
else if (i=j-1) then{ if(a[i]<a[j]) then{ max:=a[j]; min;=a[i]; }else{ max:=a[i]; min:=a[j]; } }
else { mid:=(i+j)/2; MaxMin (i,mid,max,min);MaxMin ( mid+1,j,max1,min1);If(max<max1) then max:=max1;If(min>min1) then min:=min1;}
}
University Institute of Engineering (UIE)
Medians and Order Statistics
Example a: [1] [2] [3] [4] [5] [6] [7] [8] [9] 22 13 -5 -8 15 60 17 31 47
University Institute of Engineering (UIE)
Example
1,9,60,-8
1,5,22,-8
8,9,47,31
6,9,60,17
1,3,22,-5 6,7,60,174,5,15,-8
3,3,-5,-51,2,22,13
1 2
3 4 6 7
58
9
University Institute of Engineering (UIE)
References
• Fundamental of Computer Algorithms ,2nd Edition,Sartaj Sahni, Ellis Horowitz, Sanguthevar Rajasekaran, Chapter No.3 page no.153-158.