0% found this document useful (0 votes)
16 views

Practical For Dbms

The document discusses implementing the ith order statistic. It defines the ith order statistic as the ith smallest element in a sorted array. It provides examples like the minimum, median, and maximum being order statistics 1, floor(N+1/2), and N. The algorithm uses randomized selection to find the ith order statistic in expected O(n) time using partitioning. Applications are given in finance, algorithms, networks, games, and quality control.

Uploaded by

Akshat Soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Practical For Dbms

The document discusses implementing the ith order statistic. It defines the ith order statistic as the ith smallest element in a sorted array. It provides examples like the minimum, median, and maximum being order statistics 1, floor(N+1/2), and N. The algorithm uses randomized selection to find the ith order statistic in expected O(n) time using partitioning. Applications are given in finance, algorithms, networks, games, and quality control.

Uploaded by

Akshat Soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Analysis And Design of Algorithms(22EB7310) Enrollment No:-220152

Practical 10
Aim:-Implement ith order Statistic.

Objective:- Program for solving ith Order Statistics problem.

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:

Minimum = Order statistics 1

2nd minimum = Order statistics 2

Median = Order statistics floor((N+1)/2)

2nd Maximum = Order Statistics N-1

Maximum = Order statistics N

Algorithm :-

//Find the kth smallest item from an array of distinct elements


// k is zero based, assume array size is N public static Comparable select(Comparable[] a, int k)
{

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]

FEST-ADANI UNIVERSITY CSE-C4 116


Analysis And Design of Algorithms(22EB7310) Enrollment No:-220152

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 :-

FEST-ADANI UNIVERSITY CSE-C4 117


Analysis And Design of Algorithms(22EB7310) Enrollment No:-220152

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.

(a) Randomized Select

Program Code

#include <stdio.h>
#include <stdlib.h>
#include <time.h> int j;

int partition(int arr[], int low, int high)


{ int pivot = arr[high]; int i = low - 1;
for (j = low; j <= high - 1; j++)
{ if (arr[j] <= pivot)
{ i++;
// Swap arr[i] and arr[j] int temp = arr[i];
arr[i] = arr[j]; arr[j] = temp;
}
}
// Swap arr[i + 1] and arr[high] int temp = arr[i +
1]; arr[i + 1] = arr[high]; arr[high] = temp; return i
+ 1;
}

int randomized Partition(int arr[], int low, int high)


{
srand(time(NULL));
int random = low + rand() % (high - low);

// Swap arr[random] and arr[high] int temp =


arr[random]; arr[random] = arr[high]; arr[high] =
temp; return partition(arr, low, high);
}

FEST-ADANI UNIVERSITY CSE-C4 118


Analysis And Design of Algorithms(22EB7310) Enrollment No:-220152

int randomized Select(int arr[], int low, int high, int i) \


{
if (low == high)
{
return arr[low];
}

int pivotIndex = randomized Partition(arr, low, high);

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

printf("\nEnter the array elements: ");


for (j = 0; j < n; j++)
{
scanf("%d", &arr[j]);
}
printf("\nEnter the 'i' value (to find the 'i'th smallest element): "); scanf("%d", &i); if (i < 1 || i >
n)
{ printf("Invalid 'i' value.\n"); return 1;
}
int result = randomizedSelect(arr, 0, n - 1, i - 1); printf("\nThe %dth smallest element is:
%d\n", i, result);
return 0; }

FEST-ADANI UNIVERSITY CSE-C4 119


Analysis And Design of Algorithms(22EB7310) Enrollment No:-220152

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:-_____________

FEST-ADANI UNIVERSITY CSE-C4 120

You might also like