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 30, 2018
1 parent 94e52aa commit 215a88f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ This repository contains examples of various algorithms written on different pro
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | [:octocat:](shell_sort/C) | | | [:octocat:](shell_sort/Python) |
| [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) | | | |
| [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) | | | | | | [: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 215a88f

Please sign in to comment.