# Randominary search algorithm

This algorithm is derived from the binary algorithm, it's working by searching randomly in the sorted array and Comparision with the searching number instead of slicing array to two parts. Its performance is based on random numbers and chances it gets.

int random(int from, int to){
return rand() % (to - from + 1) + from;
}

int randominary(int arr[], int n, int min, int max){
int rand = random(min, max);
cout << rand;
if(n == arr[rand])
return rand;
if(n < arr[rand])
return randominary(arr, n, min, rand - 1);
if(n > arr[rand])
return randominary(arr, n, rand + 1, max);
return -1;
}

int main(){
srand(time(0));
int arr[] = {1, 6, 9, 11, 13, 15, 16, 64, 65, 231, 232, 4453, 1291};

cout << randominary(arr, 231, 0, sizeof(arr) - 1) << endl;

return 0;
}

## Testing Randominary searching algorithm

Binary search algorithm wins randominary on small arrays, but when there are big arrays, the chance of randominary is bigger than binary to win.

int random(int from, int to){
return rand() % (to - from + 1) + from;
}
int randominary(int arr[], int min, int max, int x, int counter = 1){
int rand = random(min, max);
if(x == arr[rand]){
return counter;
}
if(x < arr[rand]){
return randominary(arr, min, rand - 1, x, counter + 1);
}
if(x > arr[rand]){
return randominary(arr, rand + 1, max, x, counter + 1);
}
return -1;
}

int binarySearch(int arr[], int min, int max, int x, int counter = 1){
if (max >= min){
int mid = min + (max - min)/2;
if (arr[mid] == x)
return counter;
if (arr[mid] > x)
return binarySearch(arr, min, mid - 1, x, counter + 1);
return binarySearch(arr, mid + 1, max, x, counter + 1);
}
return counter;
}

void insertionSort(int array[], int length) {
for (int i = 1; i < length; ++i) {
for (int j = i; j > 0 && array[j - 1] > array[j]; --j) {
int tmp = array[j];
array[j] = array[j - 1];
array[j - 1] = tmp;
}
}
}

int main(){
srand(time(0));

int tests = 1;
int arraySize = 200000;
int randominaryPoint = 0;
int binaryPoint = 0;

for (int n = 0; n < tests; n++){
int p[arraySize];
for(int m = 0; m < arraySize; m++){
p[m] = random(0, 2147483647);
}
insertionSort(p, arraySize);
int x = p[random(0, arraySize - 1)];
int r = randominary(p, 0, arraySize - 1, x);
int b = binarySearch(p, 0, arraySize - 1, x, 1);
randominaryPoint += r;        binaryPoint += b;
}
cout << "total: " << endl;
cout << "randominary: " << randominaryPoint;
cout << ", ";
cout << "binary: " << binaryPoint;

return 0;
}

Randominary search algorithm - 2018 Jul 07