Program to sort the numbers using selection sort

Thursday, December 4th, 2008

Here is the program to sort the given integer in ascending order using selection sort method. Please find the link to the pictorial tutor of the sorting. This program needs to enter the length of the entering array, followed by the array to be sorted. The entered integers are stored in the array A.

Logic : Here, to sort the data in ascending order, the first element A[0] is compared with all the other elements till the end of the array. If it is greater than any other the elements then they are interchanged. So after the first iteration of the outer for loop smallest element will be placed at the first position. The same procedure is repeated for the other elements too.

If we complement the if condition in this program, it will give out the sorted array in descending order. Sorting can also be done in other methods, like bubble sorting and insertion sorting, which follows in the next pages.

#include <stdio.h>
        int A[20], N, Temp, i, j;
        printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
        printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
        for(i=1; i<=N; i++)
                scanf("\n\t\t%d", &A[i]);
        for(i=1; i<=N-1; i++)
                for(j=i+1; j<=N;j++)
                                Temp = A[i];
                                A[i] = A[j];
                                A[j] = Temp;
        printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
        for(i=1; i<=N; i++)

Download exe and source code here.

Author Name :

Total : 15 Comments

15 Responses to “Program to sort the numbers using selection sort”

  1. melaku says:

    I want a C++ program that is done by using class.
    the program contains the following:
    1.done by sorting algorithms(selection sorting,bubble sorting or
    insertion sorting one of this is enough).
    2.the program contains students grade,GPA,age,id,name,mark,credithour,course,department etc forfive students .
    3.the program can sort using GPA and name.
    4.please send it if possible today else tomorrow.

    GOOD BY!

  2. siraj ahmed says:

    bubble or insertion or insertion sorting and searching student atributes or data( name ,id, age ,gpa and in one c++ program in classdefinition

  3. keerthu says:

    could u plz say the work of gotoxy(25,11+i);?

  4. ara says:

    what the work of printf(“nt ENTER THE ELEMENTS OF THE ARRAY…:”);
    gotoxy(25, 11+i);?

  5. Ranjith says:

    printf is used to just print the given text. gotoxy(25, 11+i); is used to set the cursor to specific position on screen.

  6. vijay says:

    hello..Rajnith m vijay i require a source code for merge and quick sort in C as wellas in c++…kindly get the source….
    Thank u

  7. kl143 says:

    Hello! how about in sorting names? how can I sort that?

    This is my example of phonebook program:


    typedef struct PhoneBookEntry
    char FirstName[21];
    char LastName[21];
    char PhoneNum[21];
    } person;

    void Add (int*, int, person*);
    void Del (int*, int, person*);
    void Print (int*, int, person*);
    void Search (int*, int, person*);

    int main() {

    int select=0;
    int contact_ctr=0;
    int i=0;

    person contacts[50];
    person* pcontacts;
    pcontacts = (person*) calloc(0, sizeof(person));
    if (pcontacts == NULL)
    {printf(“Out of memory! Aborting…”);
    return 1;}
    else {}

    do {
    printf(“\n\nPhone Book Application:\n\n”);
    printf(“1) Add friend\n”);
    printf(“2) Delete friend\n”);
    printf(“3) Show phone book\n”);
    printf(“4) Search\n”);
    printf(“5) Exit\n”);
    printf(“What do you want to do? “);
    scanf(“%d”, &select);

    switch (select)
    case 1:
    pcontacts = realloc(pcontacts, contact_ctr * sizeof(person));
    Add (&contact_ctr, i, contacts);
    case 2:
    Del (&contact_ctr, i, contacts);
    case 3:
    Print (&contact_ctr, i, contacts);
    case 4:
    Search (&contact_ctr, i, contacts);
    case 5:// Sorting

    case 6:
    printf(“You entered an invalid selection. Try again.\n\n\n”);

    while (select!=6);
    printf(“\nThis phone book will now close. Goodbye!\n\n”);
    return 0;

    void Add (int *contact_ctr, int i, person *contacts)
    int cmpprod(const void *a, const void *b);
    printf(“\nFirst name: “);
    scanf(“%s”, contacts[*contact_ctr-1].FirstName);
    printf(“Last name: “);
    scanf(“%s”, contacts[*contact_ctr-1].LastName);
    printf(“Phone number : “);
    scanf(“%s”, contacts[*contact_ctr-1].PhoneNum);
    printf(“Record added to the phone book\n\n”);


    void Del (int *contact_ctr, int i, person *contacts)
    char name[21];

    printf(“Please enter first name to delete: “);
    scanf(“%s”, name);
    for(i=0;i 0)
    printf(“\n\nPhone Book Entries:\n\n”);
    for(i=0; i<*contact_ctr; i++)
    if (strcmp(nullStr, contacts[i].FirstName) != 0)
    printf("%s %s %s\n", contacts[i].FirstName, contacts[i].LastName, contacts[i].PhoneNum);
    else {}
    printf("\n\nThe phone book is currently empty.\n\n");

    void Search (int *contact_ctr, int i, person *contacts){
    char search[21];
    int z=0,c;
    printf("Please enter name to search: ");
    for(z=0; z<*contact_ctr; z++){
    if(c == 0){
    printf("\nFirst name: %s\n", contacts[z].FirstName);
    printf("Last name: %s\n", contacts[z].LastName);
    printf("Phone number: %s\n", contacts[z].PhoneNum);

  8. bhavna singhal says:

    this is not the selection sort
    this is comparison method

  9. kev says:

    i tried writing the code with the same logic but i do not the get the desired output only the 1st element gets sorted rest all remains unsorted,however i think there’s a problem with the loop plzz check it out and help me with what iv’e been missing,thanx
    int arr[10];
    static int i;
    int j,temp;
    printf(“enter any 10 numbers”);
    printf("unsorted list of numbers are\n");
    printf("sorted list of array elements are\n");
    printf("decending order list");

  10. jamila says:

    i want to learn Selection Sort with Demo

  11. kishore says:

    shell shot program this my contact 9441275859 writing for 3program

  12. Ravi kumar says:

    I found some error in this program.

    initialize i=0 in the for loop when we are writing the code for sorting

  13. javed says:

    It is good. but add more programs

  14. Aks says:

    It is not selection sort for your kind information….it is bubble sort…..

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup