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 f587db5
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions selection_sort/Cpp/selection_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
using namespace std;

void selection_sort(int[], int);

int main() {
int n = 10, arr[] = {5, 7, 9, 8, 3, 1, 4, 2, 10, 6};
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 f587db5

Please sign in to comment.