forked from Gaurang2908/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomized_and_deterministic.c
161 lines (153 loc) · 3.79 KB
/
randomized_and_deterministic.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void swapping(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition(int *Arr, int *p, int *r)
{
int pivot = *(Arr + *r);
int i = (*p - 1);
for (int j = *p; j <= *r - 1; j++)
{
if (*(Arr + j) < pivot)
{
i++;
swapping(&*(Arr + i), &*(Arr + j));
}
}
swapping(&*(Arr + i + 1), &*(Arr + *r));
return (i + 1);
}
int median_element(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[i])
{
swapping((arr + i), (arr + j));
}
}
}
return arr[n / 2];
}
int deterministic_select(int *arr, int k, int n)
{
if (n == 1)
return arr[0];
int leng = (int)ceil(n / 5.0f),ex = 0,g[leng],i;
for (i = 0; i < leng; i++)
{
int mul = i * 5;
while (mul < ((i * 5) + 5) && mul < n)
{
int temp[5];
for (int k = 0; k < 5; k++)
{
temp[k] = arr[mul++];
}
g[ex++] = median_element(temp, mul - (i * 5));
}
}
int deter = deterministic_select(g, (int)ceil(leng / 2.0f), leng),L[n],E[n],G[n],p = 0, q = 0, r = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] == deter)
E[q++] = arr[i];
else if (arr[i] > deter)
G[r++] = arr[i];
else
L[p++] = arr[i];
}
if (k <= p)
deterministic_select(L, k, p);
else if (k <= (p + q))
return deter;
else
deterministic_select(G, k - p - q, r);
}
int randomizedpartition(int *Arr, int *p, int *r)
{
int q;
int pivot_index = *p + rand() % (*r - *p + 1);
int i = (*p - 1);
int pivot = *(Arr + pivot_index);
swapping(&*(Arr + pivot_index), &*(Arr + *r));
q = partition(Arr, p, r);
return q;
}
int random_select(int *Arr, int *p, int *r, int i)
{
// int i;
if (*p == *r)
{
return *(Arr + *p);
// printf("%d",*(Arr + *p));
}
else
{
int q = randomizedpartition(Arr, p, r);
int k = q - *p + 1;
if (i == k)
{
return Arr[q];
}
else
{
if (i < k)
{
int t = q - 1;
return random_select(Arr, p, &t, i);
}
else
{
int s = q + 1;
return random_select(Arr, &s, r, i - k);
}
}
}
}
int main()
{
int i, j, n, x, z, Arr[20];
printf("Enter the number of array elements");
scanf("%d", &n);
printf("Enter the array elements");
for (j = 0; j < n; j++)
{
scanf("%d", &Arr[j]);
}
do
{
printf("\n1. Randomized Select\n");
printf("2. Deterministic Select\n");
printf("3. Exit from the program\n");
printf("Enter your option");
scanf("%d", &x);
switch (x)
{
case 1:
printf("------>Randomized select<--------\n");
int p = 0;
int q = n - 1;
printf("Enter the minimum element you want to search: ");
scanf("%d", &i);
z = random_select(Arr, &p, &q, i);
printf("The % d minimum element is % d\n", i, z);
break;
case 2:
printf("Deterministice......\n");
printf("Enter the minimum element you want to search: ");
scanf("%d", &i);
int size = n;
printf("The ith minimum element is : %d\n", deterministic_select(Arr, i, size));
break;
}
} while (x <= 2);
return 0;
}