Selection Sort of array in C++

Using Visual Studio 2015

===============================================================


#include<iostream>
using namespace std;
#include<conio.h>
int main()
{
 int i, j, n, loc, temp, min, arr[30];
 cout << "Enter the number of elements:";
 cin >> n;
 for (i = 0;i<n;i++)
 {
  cout << "Enter the number=";
  cin >> arr[i];
 }
 for (i = 0;i<n - 1;i++)
 {
  min = arr[i];
  loc = i;
  for (j = i + 1; j<n; j++)
  {
   if (min>arr[j])
   {
    min = arr[j];
    loc = j;
   }
  }
  temp = arr[i];
  arr[i] = arr[loc];
  arr[loc] = temp;
 }
 cout << "\nSorted list is as follows"<<endl;
 for (i = 0;i<n; i++)
 {
  cout << arr[i] << " ";
 }
 _getche();
}

    
=========================================================================

Comments

Popular posts from this blog

Suppose you are given a square matrix of size n x n, write a program to determine if this is an identity matrix. 2D array

Bubble sorting of array in C++

Program to create a multiplication table of a given number using while loop.