Skip to content

Commit

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

closes NITSkmOS#482
  • Loading branch information
yashasingh committed Oct 30, 2018
1 parent daea6b0 commit a5277bd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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

Expand Down
28 changes: 28 additions & 0 deletions selection_sort/Cpp/selection_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#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) {
//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;
}
}
}

0 comments on commit a5277bd

Please sign in to comment.