Blank output screen when I run this searching algorithm

1

I have been practicing median search algorithm, and this is what I wrote-

#include <iostream>
#include <stdlib.h>

using namespace std;

int S1[10] = { 0 };
int S2[1]  = { 0 };
int S3[10] = { 0 };

int mediansearch(int A[], int k, int size)
{
    int ran = rand() % size;
    int i = 0;
    int a = 0;
    int b = 0;
    int c = 0;

    for (i = 0; i < size; i++)
    {
        if (A[ran] > A[i])
        {
            S1[a] = A[i];
            a++;
        }
        else if (A[ran] == A[i])
        {
            S2[b] = A[i];
            b++;
        }
        else
        {
            S3[c] = A[i];
            c++;
        }
    }

    if (a <= k)
    {
        return mediansearch(S1, k, a);
    }
    else if (a + b <= k)
    {
        return A[ran];
    }
    else
    {
        return mediansearch(S3, k - a - b, c);
    }
}

int main()
{
    int arr[] = { 6, 5, 4, 8, 99, 74, 23 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = mediansearch(arr, 5, n);

    cout << "5th smallest is:" << x << endl;
}

And I have been getting output as-

Process returned -1073741676 (0xC0000094) execution time : 1.704 s So, what am I doing wrong? Any kind of help will be appreciated.

c++
algorithm
search
median
asked on Stack Overflow Feb 14, 2021 by varun dhar • edited Feb 14, 2021 by Jack Lilhammers

1 Answer

0

There are a few issues with this code, the first one being the naming of variables.
I suggest you choose more significative names in the future, because good naming is fundamental when someone else has to understand your code and your ideas.

Another thing is that the arguments of are in a counterintuitive order because the pair related to the array are separated by the index you want to look for.
I'd write int mediansearch(int A[], int size, int k)

Here the comparisons are reversed, k should be less than rather than greater than equal a

if (a <= k) // (k < a)
{
    return mediansearch(S1, k, a);
}
else if (a + b <= k) // (k < a + b)
{
    return A[ran];
}
else
{
    return mediansearch(S3, k - a - b, c);
}

The other thing is that you're sharing S1, S2, and S3 among all the recursive calls and that causes some error that I wasn't able to identify, maybe someone commenting will help me out.

However, I suggest you read this article that explains in detail the procedure you're trying to implement: https://rcoh.me/posts/linear-time-median-finding/
It's python, but it can be easily ported to C/C++, and in fact that's what I did.

#include <iostream>
#include <stdlib.h>
#include <assert.h>
#include <time.h>

using namespace std;

int medianSearch(int A[], int size, int k)
{
    int *lows = (int *)calloc(size, sizeof(int));
    int  lowsLen = 0;

    int *highs = (int *)calloc(size, sizeof(int));
    int  highsLen = 0;

    int *pivots = (int *)calloc(size, sizeof(int));
    int  pivotsLen = 0;

    int  median;
    int  pivot;
    int  i;

    if (size == 1)
        return A[0];

    // Other ways of randomly picking a pivot
    // pivot = 0;
    // pivot = size-1;
    // pivot = size/2;

    assert(size > 0);
    pivot = rand() % size;

    for (i = 0; i < size; ++i)
    {
        if (A[i] < A[pivot])
        {
            lows[lowsLen] = A[i];
            lowsLen++;
        }
        else if (A[i] > A[pivot])
        {
            highs[highsLen] = A[i];
            highsLen++;
        }
        else
        {
            pivots[pivotsLen] = A[i];
            pivotsLen++;
        }
    }

    if (k < lowsLen)
        median = medianSearch(lows, lowsLen, k);
    else if (k < lowsLen + pivotsLen)
        median = A[pivot];
    else
        median = medianSearch(highs, highsLen, k - lowsLen - pivotsLen);

    free(lows);
    free(highs);
    free(pivots);

    return median;
}

int compare(const void *a, const void *b)
{
    return ( *(int *)a - *(int *)b );
}

int medianSorted(int A[], int size, int k)
{
    qsort(A, size, sizeof(int), compare);

    return A[k];
}

#define N 1000

int main()
{
    int arr[N];
    int brr[N];
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 200;
    int x;
    int y;
    
    for (int i = 0; i < n; ++i)
        arr[i] = brr[i] = rand();
    
    x = medianSearch(arr, n, (k-1)%n);
    y = medianSorted(brr, n, (k-1)%n);
    
    string suffix;

    switch (k % 10)
    {
        case 1: suffix = "st"; break;
        case 2: suffix = "nd"; break;
        case 3: suffix = "rd"; break;
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
        case 0: suffix = "th"; break;
    }
    cout << k << suffix << " smallest is: " << x << endl;
    cout << k << suffix << " smallest is: " << y << endl;
}

https://onlinegdb.com/HJc2V6Lbu

answered on Stack Overflow Feb 14, 2021 by Jack Lilhammers

User contributions licensed under CC BY-SA 3.0