facebook like button

06 April, 2011

program 29: sorting in increasing order (selection sort)

The need:
     In previous posts you have seen that character arrays exist. This program was built to show that we can have arrays of integers also. This program takes some integers as input stores them in an array, sorts them in increasing order and prints back for the user.

The code: 
--------------------------------------------
#define MAX 20
#include<stdio.h>
int main()
{
    int i,j,k,n,a[MAX];
    printf("How many numbers do you want to sort?\n");
    scanf("%d",&n);
    printf("Now keep on giving %d numbers.\n",n);
    /*  taking numbers one by one*/
    for(i=0;i<n;i++)
    {
      scanf("%d",&a[i]);   
    }

    printf("\nYou have given\n");
    for(i=0;i<n;i++)
    {
      printf("%d  ",a[i]);
    /* for printing given array*/
    }
    /*below is code for sorting */
    for(i=0;i<n;i++)
    {
        for(j=i;j<n;j++)
           {
           if(a[i]>a[j])
           {
            k=a[i];
            a[i]=a[j];
            a[j]=k;
           }
         }
    }

    /*above is code for sorting */


    printf("\n\nSorted in increasing order\n");
    for(i=0;i<n;i++)
    {
      printf("%d  ",a[i]);
    /* for printing sorted array*/
    }
    getch();
    return 0;
}

-----------------------------------------
 
The approach:     
    The simplest idea to sort an array in increasing order is given by following steps:
1. Find out smallest number of the array.
2. Exchange this smallest number with first element number in the array.
3. Now skip position 1 and take rest of array as array.
4. repeat from step1.

    This program implements exact this approach. I think taking input, storing in array and printing array is trivial for you at this point of time. Now comes sorting which is explained in next paragraph.
    Here 2 for() loops one inside the other are used to get our task done. Here for for() loops I have used 2 variables 'i' and 'j'. The loop with variable is in side of loop so I'll be calling that the inner loop and the other the outer loop. Now see that inner loop runs completely in each run of outer loop. Throughout single run of outer loop value of 'i' does not change.

Remarks:
1. In this program you have seen use of integer array. In general in C we can have array for any data-type.
2. There are other methods also for sorting.
3. Here in the end of program I have used a getch() statement. This halts the program screen to until you press ENTER key. This is not necessary in "C-free" and "Unix built-in compiler" because C-free has built-in facility to halt the program screen until you press any key. but in dev-C++ and turbo-C++ this getch()is necessary to halt the program screen otherwise these compilers close the screen as the program finishes final output whether user is able to see the output in that short time of spell or not.

3 comments:

  1. Hi,

    Is there a way to find the second largest element in an array in a single loop ?

    ReplyDelete
    Replies
    1. To my knowledge we can't do it in single loop but it can be done in O(n).

      Delete
    2. Can you tell me which is the most efficient way?
      The only method coming to my mind is to find the maximum number and run another loop to find the second highest number.But this method seems a little cumbersome.
      Any other methods to do this?

      Delete

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