forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomized_selection_algorithm.c
76 lines (68 loc) · 2.22 KB
/
randomized_selection_algorithm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//This algorithm is used to select i_th order statistic from the given data in O(n) time complexity and O(1) space complexity
#include <stdio.h>
#include <stdlib.h>
int arr[100000];
int part(int arr[], int lower, int upper)
{
int z = arr[upper];
int i = lower - 1;
for (int j = lower; j <= upper - 1; j++)
{
if (arr[j] <= z)
{
++i;
int change = arr[i]; // Now we are swapping arr[i] and arr[j]
arr[i] = arr[j];
arr[j] = change;
}
}
int change2 = arr[i + 1]; // now we are swapping arr[i+1] and arr[upper]
arr[i + 1] = arr[upper];
arr[upper] = change2;
return i + 1;
}
int random_type(int minimum, int maximum)
{ //this function is used to generate a random number in the given range
int type = maximum - minimum + 1;
int number = minimum + (rand() % type);
return number;
}
int Randomized_type(int arr[], int lower, int upper)
{
int i = random_type(lower, upper);
int change = arr[upper];
arr[upper] = arr[i];
arr[i] = change;
return part(arr, lower, upper);
}
int Randomized_select_algo(int arr[], int lower, int upper, int i)
{
if (lower == upper) //if the array has only one element
return arr[lower];
int x= Randomized_type(arr, lower, upper);
int y= x+1-lower ;
if (i == y) // The value obtained is the answer
return arr[x];
else if (i > y) // the value required lies on the upper side
return Randomized_select_algo(arr, x + 1, upper, i - y);
else // the value required lies on the lower side
return Randomized_select_algo(arr, lower, x- 1, i);
}
int main()
{
int n, i;
printf("Enter number of elements: \n");
scanf("%d",&n);
for (int i = 0; i < n; i++)
scanf("%d",&arr[i]);
printf("Enter i: \n");
scanf("%d",&i);
printf("%d",Randomized_select_algo(arr, 0, n - 1, i));
return 0;
}
/*
sample test case
let n = 6 and array input be [6, 89, 45, 12, 78, 3] and i =4
then Randomized_select_algo will give us answer 45 as 4th element of the sorted array or 45 is the next greater element than the first 3 element i.e. 3,6,12
sorted array [3, 6, 12, 45, 78, 89]
*/