Beruflich Dokumente
Kultur Dokumente
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.
#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:
C Selection Sort
Java Selection Sort
C++ Online Test
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