Skip to content

Commit 4351816

Browse files
committed
Initialize repo with README and examples
0 parents  commit 4351816

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Data Structures and Algorithms
2+
For Hacktoberfest 2018! <br/>
3+
4+
Check your own profile stats after registering here: https://hacktoberfest.digitalocean.com/stats/<username>
5+
6+
### About
7+
This Repo consists of data-structures and algorithms sorted by programming language.
8+
9+
### Contributing Guidelines
10+
- The repository is structured language-wise i.e. algorithms in a certain language go in a specific folder.
11+
- The naming convention to be followed is ```algo_name.language_extention``` i.e. the overall pathshould be ```language/algo_name.language_extention```
12+
- Example of above points: heapsort in Java should go into `java/heapsort.java`
13+
- It is your choice to include only the function or the entire program for the algo.
14+
- If folder for your language does not exist, create a new one.
15+
- Don't be afraid to make a PR!
16+
17+
### How to make PR
18+
1. Fork Repo on Web Page.
19+
2. Make your changes on your forked repo.
20+
3. Make Pull Request to master.
21+
22+
Hack on!

cpp/selection_sort.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// perform selection sort on arr[]
5+
void selectionSort(int arr[], int n)
6+
{
7+
// run (n - 1) times
8+
for (int i = 0; i < n - 1; i++)
9+
{
10+
// find the minimum element in the unsorted subarray[i..n-1]
11+
// and swap it with arr[i]
12+
int min = i;
13+
14+
for (int j = i + 1; j < n; j++)
15+
{
16+
// if arr[j] element is less, then it is the new minimum
17+
if (arr[j] < arr[min])
18+
min = j; // update index of min element
19+
}
20+
21+
// swap the minimum element in subarray[i..n-1] with arr[i]
22+
swap(arr[min], arr[i]);
23+
}
24+
}
25+
26+
// Function to print n elements of the array arr
27+
void printArray(int arr[], int n)
28+
{
29+
for (int i = 0; i < n; i++)
30+
cout << arr[i] << " ";
31+
}
32+
33+
// main function
34+
int main()
35+
{
36+
int arr[] = { 3, 5, 8, 4, 1, 9, -2 };
37+
int n = sizeof(arr) / sizeof(arr[0]);
38+
39+
selectionSort(arr, n);
40+
printArray(arr, n);
41+
42+
return 0;
43+
}

0 commit comments

Comments
 (0)