Practical For Dbms
Practical For Dbms
Practical 10
Aim:-Implement ith order Statistic.
Introduction:-
The ith order statistics is defined as the value that comes in the ith position of the N element
sequence when the sequence is sorted in increasing order.
In simple terms, the order statistics is the ith smallest element in the given array. Below are a
few examples to understand the order statistics:
Algorithm :-
if ( k <0|| k>=a.length)
return null;
int I_{0} = 0 hia.length - 1;
while (hi > lo) {return a[k]I_{0} =i+1:
int j partition(a, lo, hi);if ( j ==k)
else if (j > k)
hi = j - 1
else if (j < k)
}
return a[k]
Randomized-Select(A, p, r, i), 1 ≤ i ≤ r − p + 1
if p = r then
return A[p]
end
q = Randomized-Partition(A, p,r) ;
k=q−p+1;
if i = k then return A[q];
// the pivot is the answer
else if i < k then
return Randomized-Select(A, p, q − 1, i)
else
return Randomized-Select(A, q + 1,r, i − k)
end
Example :-
Time complexity :- The expected time complexity of the provided algorithm is O(n), making
it an efficient algorithm for finding the i-th order statistic in an array on average but on worst
case it is O(n2).
Space complexity :- The space complexity is O(log n) in the worst case due to recursion stack
and O(n) due to the array.
Program Code
#include <stdio.h>
#include <stdlib.h>
#include <time.h> int j;
if (i == pivotIndex)
{
return arr[pivotIndex]; } else if (i < pivotIndex) { return randomizedSelect(arr,
low, pivotIndex - 1, i); } else { return randomizedSelect(arr, pivotIndex + 1,
high, i); }
}
int main()
{ int n, i;
printf("Enter the size of the array: ");
scanf("%d", &n);
if (n <= 0)
{
printf("Invalid input for array size.\n"); return 1;
}
int arr[n];
Output:-
Applications :-
Finance and Economics: In finance, order statistics can be used to identify extreme values, such
as the highest or lowest stock prices in a given period. This information is crucial for risk
assessment and decision-making.
Computer Science and Algorithms: Algorithms that involve finding the i-th order statistic are
used in various computer science applications, including databases, searching algorithms, and
certain machine learning algorithms.
Network Analysis: In network analysis, finding the k-th shortest path between two nodes
involves identifying the k-th order statistic in terms of path lengths.
Game Theory: In certain game scenarios, finding the i-th order statistic might be relevant. For
instance, identifying the i-th highest bid in an auction.
Quality Control: In manufacturing and quality control, identifying extreme values in a set of
measurements can help detect defects or anomalies.
Data:-__/__/2023 Signature:-_____________