# Program to sort the numbers using selection sort

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> main() { int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d",&N); 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++) if(A[i]>A[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++) printf("\n\t\t\t%d",A[i]); }```

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

1. 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.

THANK YOU !
GOOD BY!

2. siraj ahmed

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

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

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

5. 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

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

This is my example of phonebook program:

#include
#include
#include

typedef struct PhoneBookEntry
{
char FirstName[21];
char LastName[21];
char PhoneNum[21];
} 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(“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);
getchar();

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

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

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);
(*contact_ctr)++;
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 {}
}
}
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: ");
scanf("%s",search);
for(z=0; z<*contact_ctr; z++){
c=strcmp(search,contacts[z].FirstName);
if(c == 0){
printf("*****************************************");
printf("\nFirst name: %s\n", contacts[z].FirstName);
printf("Last name: %s\n", contacts[z].LastName);
printf("Phone number: %s\n", contacts[z].PhoneNum);
printf("****************************************");
}
}
}

7. 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
#include
main()
{
int arr[10];
static int i;
int j,temp;
printf(“enter any 10 numbers”);
for(i=0;i<=9;i++)
{
scanf("%d",&arr[i]);
}
printf("unsorted list of numbers are\n");
for(i=0;i<=9;i++)
{
printf("%d\n",arr[i]);
}
printf("sorted list of array elements are\n");
for(i=0;i<=9;i++)
{
for(j=1;j<=9;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("decending order list");
for(i=0;i<=9;i++)
{
printf("%d\n",arr[i]);
}
}

8. kishore

shell shot program this my contact 9441275859 writing for 3program

9. Ravi kumar

I found some error in this program.
suggestion:-

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

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