HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

A function that finds the kth smallest integer in array A (unsorted), of size n

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
smallestthearrayunsortedsizefunctionthatfindskthinteger

Problem

int kth_smallest_number(int A[], int n, int k){
    /*kth smallest number is smaller than pr equal to n-k numbers in A*/
    int smaller_than_count;
    int i,j;
    for(i=0; i<n; i++){
        /*Check whether the number A[i] satisfies
        the condition*/
        smaller_than_count = 0;
        for(j=0; j<n; j++){
            if(A[i]<=A[j]){
                smaller_than_count++;
            }
        }
        if(smaller_than_count == n-k){
            return A[i];
        }
    }
}


I just wanted to know if this solution is right, I am not worried about efficiency right now just correctness.

Solution

The compiler is your friend

Activate all warnings and you'll get pretty good hints :

selection.c: In function ‘kth_smallest_number’:
selection.c:17:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^


Test your code

Here's what I have written to test your code :

int A[] = {4,4,56,78,54,6512,21,8,7,2,321,87,78,54,2,21,5,4,321,456,5,4,35,5,4,1,67,8,1,54,4,654,7,6,54,321,7,32,4,21,54,7};
int n = sizeof(A)/sizeof(A[0]);
for (int k=0; k<n; k++)
{
    printf("%d %d\n",k,kth_smallest_number(A,n,k));
}


I'd expect the returned value to be sorted and it's clearly not the case here. Thus, I expect something to be wrong.

Playing with other inputs : int A[] = {10,9,8,7,6,5,4,3,2,1,0}; , int A[] = {10,9,8,7,6,5,5,4,3,2,1,0};, it seems like your solution does not handle properly duplicated value.

A matter of style

The indentation looks weird on your question.

You should try to declare your local variable in the smallest possible scope. Among other things, you can define your loop indices in the loop syntax in you use the C99 mode (-std=c99).

Once rewritten (but still wrong), your code looks like :

int kth_smallest_number(int A[], int n, int k){
    for(int i=0; i<n; i++){
        /*Check whether the number A[i] satisfies
          the condition*/
        int smaller_than_count = 0;
        for(int j=0; j<n; j++){
            if(A[i]<=A[j]){
                smaller_than_count++;
            }
        }
        if(smaller_than_count == n-k){
            return A[i];
        }
    }
}


Edit :

An attempt to correctness

Here what I have written , it seems to work fine but I won't promise anything.

int kth_smallest_number(int A[], int n, int k){
    for(int i=0; i= n-k) break; // Optimisation
            } else if (ai > aj) {
                bigger_than_count++;
                if (bigger_than_count > k) break; // Optimisation
            }
        }
        if(smaller_than_count < n-k && bigger_than_count <= k){
            return ai;
        }
    }
    return -1;
}

Code Snippets

selection.c: In function ‘kth_smallest_number’:
selection.c:17:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
int A[] = {4,4,56,78,54,6512,21,8,7,2,321,87,78,54,2,21,5,4,321,456,5,4,35,5,4,1,67,8,1,54,4,654,7,6,54,321,7,32,4,21,54,7};
int n = sizeof(A)/sizeof(A[0]);
for (int k=0; k<n; k++)
{
    printf("%d %d\n",k,kth_smallest_number(A,n,k));
}
int kth_smallest_number(int A[], int n, int k){
    for(int i=0; i<n; i++){
        /*Check whether the number A[i] satisfies
          the condition*/
        int smaller_than_count = 0;
        for(int j=0; j<n; j++){
            if(A[i]<=A[j]){
                smaller_than_count++;
            }
        }
        if(smaller_than_count == n-k){
            return A[i];
        }
    }
}
int kth_smallest_number(int A[], int n, int k){
    for(int i=0; i<n; i++){
        /*Check whether the number A[i] satisfies the condition*/
        int ai = A[i];
        int smaller_than_count = 0;
        int bigger_than_count = 0;
        for(int j=0; j<n; j++){
            int aj = A[j];
            if (ai < aj) {
                smaller_than_count++;
                if (smaller_than_count >= n-k) break; // Optimisation
            } else if (ai > aj) {
                bigger_than_count++;
                if (bigger_than_count > k) break; // Optimisation
            }
        }
        if(smaller_than_count < n-k && bigger_than_count <= k){
            return ai;
        }
    }
    return -1;
}

Context

StackExchange Code Review Q#46632, answer score: 4

Revisions (0)

No revisions yet.