Sie sind auf Seite 1von 3

codescracker main buttonbutton

C++ Program Selection Sort

« Previous ProgramNext Program »


Selection Sort in C++
To sort an array in ascending order using selection sort technique in C++ programming, then
you have to ask to the user to enter the array size and array elements, now start comparing the
array elements and start placing the smaller elements before bigger to arrange all the array
elements in ascending order.

Here, we are using swapping of the elements with the help of a variable say temp of same type.
That is, on found (bigger element before smaller element), start swapping with temp variable,
place the first number (bigger) in the temp and second number (smaller) in the first (bigger),
then temp in the second number (bigger). Now at the place of bigger element, you have a
smaller element and at the place of smaller element, you have a bigger element. After
performing this, continue to next match to sort the whole array in ascending order.

C++ Programming Code for Selection Sort


Following C++ program ask to the user to enter array size and array element, then it will sort the
array is ascending order and display the sorted array:

/* C++ Program - Selection Sort */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size, arr[50], i, j, temp, index, small, count=0;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
cin>>arr[i];
cout<<"Sorting array using selection sort...\n";
for(i=0; i<(size-1); i++)
{
small = arr[i];
for(j=(i+1); j<size; j++)
{
if(small>arr[j])
{
small = arr[j];
count++;
index = j;
}
}
if(count!=0)
{
temp = arr[i];
arr[i] = small;
arr[index] = temp;
}
count=0;
}
cout<<"Now the Array after sorting is :\n";
for(i=0; i<size; i++)
cout<<arr[i]<<" ";
getch();
}
When the above C++ program is compile and executed, it will produce the following result:

selection sort in C++ Programming


Same Program in Other Language
You may like the same program in other programming languages:

C Selection Sort
Java Selection Sort
C++ Online Test

« Previous ProgramNext Program »


FacebookWhatsAppTwitterShare

enter email id

Tools
Calculator

Quick Links
Signup - Login - Give Online Test
Top Tutorials
Java Tutorial
C++ Tutorial
HTML Tutorial
PHP Tutorial
Python Tutorial
Computer Network Tutorial
Operating System Tutorial
Online Tests
All Test
Computer Fundamental Test
Java Test
C Test
C++ Test
HTML Test
PHP Test
Online Tests
Python Test
Operating System Test
Networking Test
Microsoft Word Test
Microsoft Excel Test
Computer Hardware Test
Linux Test
Examples
Java Examples
C Examples
C++ Examples
Python Examples
Join CodesCracker
Subscribe Us
Login
© Copyright 2019. All Rights Reserved.

CodesCracker

Das könnte Ihnen auch gefallen