Order of Algorithm

8
Medians and Order Statistics

description

order of algorithms in design of algorithms and its analysis

Transcript of Order of Algorithm

Page 2: 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.

Page 3: Order of Algorithm

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];

}}

Page 5: Order of Algorithm

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;}

}

Page 8: Order of Algorithm

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.