facebook like button

09 December, 2011

finding kth largest number out of n numbers

The need:
     This program gives the kth largest number from the given unsorted list of n numbers. I don't know what could be the application of that but some sites tell that it is asked as interview question sometimes.
The code:
----------------------------------------------------------
/*find kth largest number from a list of n numbers*/
#include<stdio.h>
#include<stdlib.h>
void getMaxToFront(int *p,int front, int end);
void swap(int *, int *);
int main()
{
    int i,*num,n,k;
    printf("This is a program to find kth largest number from a list of numbers\n");
    printf("how many are total numbers\n n = \t");
    scanf("%d",&n);
    if(1>n)
        exit(printf("n cannot be less than 1\n"));
    printf("for kth largest give value of k\n k = \t");
    scanf("%d",&k);
    if(k<1)
        exit(printf("k cannot be less than 1\n"));
    if(k>n)
        exit(printf("k must be less then n\n"));
    num=(int *)malloc(n*sizeof(int));
    printf("now enter %d integers\n",n);
    for(i=0;i<n;i++)
    scanf("%d",num+i);
    for(i=0;i<k;i++)
        getMaxToFront(num,i,n-1);
    printf("\n%d is %dth largest\n",*(num+k-1),k);
    getchar();
    return 0;
}

void getMaxToFront(int *p,int front, int end)
{              
    int i;
    for(i=front+1;i<=end;i++)
        if( *(p+front) < *(p+i) )
        swap(p+front,p+i);
}

void swap(int *a,int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

----------------------------------------------------------
Approach:
   first approach is simple. Pick the largest bring it to 1st position, then pick largest from the rest of list and bring on 2nd position and so on upto kth :-).

Remarks:
Analyze the case:
n=100, k=5;
The above program is fine with that. Just find out biggest 5 numbers and done.
Now have a look at this case:
n=100, k=95;
In this case you will not want to go from front to end because you will have to iterate the loop in main 95 time. Instead we'll find out 6th smallest and go end to front... :-)
so better modify the above program a little bit which is like below:
-------------------------------------------------------------
/*find kth largest number from a list of n numbers*/
#include<stdio.h>
#include<stdlib.h>
void getMaxToFront(int *p,int front, int end);
void getMinToEnd(int *p,int front, int end);
void swap(int *, int *);
int main()
{
    int i,*num,n,k;
    printf("This is a program to find kth largest number from a list of numbers\n");
    printf("how many are total numbers\n n = \t");
    scanf("%d",&n);
    if(1>n)
        exit(printf("n cannot be less than 1\n"));
    printf("for kth largest give value of k\n k = \t");
    scanf("%d",&k);
    if(k<1)
        exit(printf("k cannot be less than 1\n"));
    if(k>n)
        exit(printf("k must be less then n\n"));
    num=(int *)malloc(n*sizeof(int));
    printf("now enter %d integers\n",n);
    for(i=0;i<n;i++)
    scanf("%d",num+i);
    if(k<=n/2){
    printf("\nusing getMaxToFront\n");
    for(i=0;i<k;i++)
        getMaxToFront(num,i,n-1);
    }
    else{
    printf("\nusing getMinToEnd\n");
    for(i=n;i>=k;i--)
        getMinToEnd(num,0,i-1);
    }
    printf("\n%d is %dth largest\n",*(num+k-1),k);
    getchar();
    return 0;
}

void getMaxToFront(int *p,int front, int end)
{                //end is needed as an argument here because we need to use the length of list
    int i;
    for(i=front+1;i<=end;i++)
        if( *(p+front) < *(p+i) )
        swap(p+front,p+i);
}

void getMinToEnd(int *p,int front, int end)
{                //actually we could have skipped front as an argument here observe each time I am passing 0 as front
    int i;
    for(i=end;i>=front;i--)
        if( *(p+end) > *(p+i) )
        swap(p+end,p+i);
}

void swap(int *a,int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
-------------------------------------------------------------
This program uses the method/function which gives less number of loop runs.

5 comments:

  1. can u let me know how to convert a group of characters into a word; for eg i have an array containing the letters n,a,m,e and i want to join them to get the word 'name'.plz help. and excellent work with the site,will surely help a lot of beginners like me..

    ReplyDelete
  2. can't we just to a simple bubble sort and print k elements

    ReplyDelete
  3. if i am not wrong ,even if u join 'name' it will still be 'n''a''m''e'

    ReplyDelete
  4. @TheMicroSoftFanatic:
    In C language there is difference between array of letters(characters) and word(character string) in computer memory. Its up to you how do you want to see them.
    for more details place post on wall of
    https://www.facebook.com/programsimply
    I'll answer in detail.

    ReplyDelete
  5. @Sourabh:
    Of course you can use bubble sort.
    But this program only sort k elements or n-k elements whichever is less hence this program will take less time in execution. Time is the main concern in programming.

    ReplyDelete

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply