Thread: How to find the smallest 5 numbers in an array?

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

    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
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 11:05 AM
  5. need to find smallest int
    By lankeveil in forum C Programming
    Replies: 3
    Last Post: 11-30-2008, 05:48 AM