Skip to content

Commit

Permalink
selection_sort.cpp: Add cpp implementation for selection sort.
Browse files Browse the repository at this point in the history
This commit adds the Cpp implementation of selection sort for NITSkmOS/Algorithms.

closes NITSkmOS#482
  • Loading branch information
yashasingh committed Oct 29, 2018
1 parent 94e52aa commit e9a30b1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions selection_sort/Cpp/selection_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
using namespace std;

void selection_sort(int[], int);

int main(){
int n, arr[100];
cout << "Enter the length of the array (less than 100)- ";
cin >> n;
cout << "Enter space separated " << n << " numbers- ";
for(int i = 0;i < n; i++){
cin >> arr[i];
}
selection_sort(arr, n);
cout << "Sorted array after selection sort algorithm- ";
for(int i = 0; i < n; i++){
cout << arr[i] << " ";
}
cout << endl;
return 0;
}

void selection_sort(int arr[], int n){
int i, j, temp;
for(i = 0; i < n; i++)
for(j = i+1; j < n; j++){
if(arr[i]>arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

0 comments on commit e9a30b1

Please sign in to comment.