diff --git a/README.md b/README.md index 1da6d329..b9fd4817 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ This repository contains examples of various algorithms written on different pro | [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | | | | [:octocat:](heap_sort/python) | | [Maximum Subarray Problem](https://en.wikipedia.org/wiki/Maximum_subarray_problem) | | | | [:octocat:](/maximum_subarray/Python)| | [Knapsack Problem](https://en.wikipedia.org/wiki/Knapsack_problem) | | | | [:octocat:](knapsack_problem/Python)| -| [Selecton Sort](https://en.wikipedia.org/wiki/Selection_sort) | [:octocat:](selection_sort/C) | | | | +| [Selecton Sort](https://en.wikipedia.org/wiki/Selection_sort) | [:octocat:](selection_sort/C) | [:octocat:](selection_sort/Cpp) | | ## Implemented Data Structures @@ -71,4 +71,4 @@ Feel free to contact us at our [Gitter channel](https://gitter.im/NITSkmOS/algo) ## Notes Only project maintainers should merge a PR. -Other members can add their reviews to a PR but the merging should be done by only a project maintainer. +Other members can add their reviews to a PR but the merging should be done by only a project maintainer. \ No newline at end of file diff --git a/selection_sort/Cpp/selection_sort.cpp b/selection_sort/Cpp/selection_sort.cpp new file mode 100644 index 00000000..3ce2f692 --- /dev/null +++ b/selection_sort/Cpp/selection_sort.cpp @@ -0,0 +1,28 @@ +#include +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) { + //This function implements the main selection sort algorithm. + 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; + } + } +}