TheAlgorithms-C-Plus-Plus/sorting/Selection Sort.cpp

40 lines
589 B
C++
Raw Normal View History

2016-07-16 19:08:05 +08:00
//Selection Sort
2019-08-21 10:10:08 +08:00
#include <iostream>
2016-07-16 19:08:05 +08:00
using namespace std;
int main()
{
int Array[6];
2019-08-21 10:10:08 +08:00
cout << "\nEnter any 6 Numbers for Unsorted Array : ";
2016-07-16 19:08:05 +08:00
//Input
2019-08-21 10:10:08 +08:00
for (int i = 0; i < 6; i++)
2016-07-16 19:08:05 +08:00
{
2019-08-21 10:10:08 +08:00
cin >> Array[i];
2016-07-16 19:08:05 +08:00
}
2019-08-21 10:10:08 +08:00
2016-07-16 19:08:05 +08:00
//Selection Sorting
2019-08-21 10:10:08 +08:00
for (int i = 0; i < 6; i++)
2016-07-16 19:08:05 +08:00
{
2019-08-21 10:10:08 +08:00
int min = i;
for (int j = i + 1; j < 6; j++)
2016-07-16 19:08:05 +08:00
{
2019-08-21 10:10:08 +08:00
if (Array[j] < Array[min])
2016-07-16 19:08:05 +08:00
{
2019-08-21 10:10:08 +08:00
min = j; //Finding the smallest number in Array
2016-07-16 19:08:05 +08:00
}
}
2019-08-21 10:10:08 +08:00
int temp = Array[i];
Array[i] = Array[min];
Array[min] = temp;
2016-07-16 19:08:05 +08:00
}
2019-08-21 10:10:08 +08:00
2016-07-16 19:08:05 +08:00
//Output
2019-08-21 10:10:08 +08:00
cout << "\nSorted Array : ";
for (int i = 0; i < 6; i++)
2016-07-16 19:08:05 +08:00
{
2019-08-21 10:10:08 +08:00
cout << Array[i] << "\t";
2016-07-16 19:08:05 +08:00
}
}