Skip to content

Instantly share code, notes, and snippets.

@porimol
Created May 23, 2017 08:37
Show Gist options
  • Save porimol/dae96b85224e8a01c4ca08d00449fbe2 to your computer and use it in GitHub Desktop.
Save porimol/dae96b85224e8a01c4ca08d00449fbe2 to your computer and use it in GitHub Desktop.
Selection Sort Algorithm Implementation Using C++
/**
* @file selection_sort.cpp
* @author Porimol Chandro, CSE 32D, World University of Bangladesh(WUB)
* @date 23/05/2017
*
* @brief Selection Sort Algorithm Implementation.
*/
#include <iostream>
using namespace std;
int main()
{
int data[100], n, i, j, minindex, temp;
cout << "Please enter the size of an array: ";
cin >> n;
for(i = 0; i<n; i++){
cout << "Please enter the value of index[" <<i <<"]: ";
cin >> data[i];
}
for(i = 0; i<n; i++){
minindex = i;
for(j = i+1; j<n; j++){
if(data[j] < data[minindex]){
minindex = j;
}
}
temp = data[i];
data[i] = data[minindex];
data[minindex] = temp;
}
cout << "\n Following is the sorted values are:\n-------------------------------------" <<endl;
for(i = 0; i<n; i++){
cout << "The value of index[" <<i <<"] is " <<data[i] <<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment