Like Tree1Likes
  • 1 Post By sana.iitkgp

How to find the smallest 5 numbers in an array?

This is a discussion on How to find the smallest 5 numbers in an array? within the C Programming forums, part of the General Programming Boards category; I can find the smallest number in an array: Code: #include <stdio.h> int main(){ int array[10] = {22,14,3,6,2,37,19,24,9,12}; int i; ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    32

    How to find the smallest 5 numbers in an array?

    I can find the smallest number in an array:

    Code:
    #include <stdio.h>
    
    int main(){
        int array[10] = {22,14,3,6,2,37,19,24,9,12};
    
        int i;
        int smallest = array[0];
    
         
        for(i = 0; i < 10; i++){ 
    
                   if(array[i] < smallest){
                       smallest = array[i];
                   } 
           }
        printf("%d ", smallest);
    
    }
    ...but how can I get the smallest 5 numbers in an array? I'm struggling with this. Any help? Thanks.

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Sort the array in ascending order; then first N elements of it will be the N smallest numbers in the array.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    You could sort the array to begin with, then the 5 smallest numbers would be the first 5 elements of the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    No need to sort the entire arry for this.

    You can try like this. Do 5 iterations, in each iteration bring the smallest to the front. I mean start first iteration so that smallest comes to a[0], then start 2nd iteration from a[1] so that 2nd smallest comes to a[1]. Do this for 5 times. You'll endup with 5 small integers in the first five elements of array.
    laserlight likes this.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,883
    Quote Originally Posted by sana.iitkgp
    Do 5 iterations, in each iteration bring the smallest to the front. I mean start first iteration so that smallest comes to a[0], then start 2nd iteration from a[1] so that 2nd smallest comes to a[1]. Do this for 5 times. You'll endup with 5 small integers in the first five elements of array.
    In other words, do a partial sort using selection sort. In this case, I think that this is the best approach. If the array was much larger, and n possibly much larger than 5, then the approach that does quicksort-style partitioning, with recursion on the partition that contains the nth element, is likely to be better if order is not important.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. find smallest non-value in an 2-d array !! need help :D
    By newmem011 in forum C Programming
    Replies: 7
    Last Post: 07-10-2012, 10:37 PM
  2. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 01:43 PM
  3. Replies: 6
    Last Post: 09-23-2010, 10:12 PM
  4. find and replace duplicate numbers in array
    By Cathalo in forum C++ Programming
    Replies: 5
    Last Post: 02-17-2009, 10:05 AM
  5. need to find smallest int
    By lankeveil in forum C Programming
    Replies: 3
    Last Post: 11-30-2008, 04:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21