added recursive implemenation in c++ in index.md (#26218)
* added recursive implemenation in c++ in index.md * fix: formatting/syntax highlighting
This commit is contained in:
committed by
Christopher McCormack
parent
d6cb576e89
commit
caf6d3446f
@ -21,6 +21,7 @@ Selection sort always takes the same number of key comparisons — N(N − 1)/2.
|
|||||||
|
|
||||||
### Implementation in C/C++
|
### Implementation in C/C++
|
||||||
|
|
||||||
|
#### Iterative Implementation
|
||||||
```C
|
```C
|
||||||
for(int i = 0; i < n-1; i++)
|
for(int i = 0; i < n-1; i++)
|
||||||
{
|
{
|
||||||
@ -40,8 +41,32 @@ for(int i = 0; i < n-1; i++)
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Implementation in Javascript
|
#### Recursive Implementation
|
||||||
|
```C
|
||||||
|
int minIndex(int a[], int i, int j)
|
||||||
|
{
|
||||||
|
if (i == j) return i;
|
||||||
|
|
||||||
|
int k = minIndex(a, i + 1, j);
|
||||||
|
|
||||||
|
return (a[i] < a[k])? i : k;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void recurSelectionSort(int a[], int n, int index = 0)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (index == n) return;
|
||||||
|
|
||||||
|
int k = minIndex(a, index, n-1);
|
||||||
|
|
||||||
|
if (k != index) swap(a[k], a[index]);
|
||||||
|
|
||||||
|
recurSelectionSort(a, n, index + 1);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Implementation in Javascript
|
||||||
``` Javascript
|
``` Javascript
|
||||||
function selection_sort(A) {
|
function selection_sort(A) {
|
||||||
var len = A.length;
|
var len = A.length;
|
||||||
@ -121,19 +146,16 @@ end
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Properties
|
### Properties
|
||||||
|
|
||||||
* Space Complexity: <b>O(n)</b>
|
* Space Complexity: <b>O(n)</b>
|
||||||
* Time Complexity: <b>O(n<sup>2</sup>)</b>
|
* Time Complexity: <b>O(n<sup>2</sup>)</b>
|
||||||
* Sorting in Place: <b>Yes</b>
|
* Sorting in Place: <b>Yes</b>
|
||||||
* Stable: <b>No</b>
|
* Stable: <b>No</b>
|
||||||
|
|
||||||
### Visualization
|
### Visualization
|
||||||
|
|
||||||
* [USFCA](https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html)
|
* [USFCA](https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html)
|
||||||
* [HackerEarth](https://www.hackerearth.com/practice/algorithms/sorting/selection-sort/visualize/)
|
* [HackerEarth](https://www.hackerearth.com/practice/algorithms/sorting/selection-sort/visualize/)
|
||||||
|
|
||||||
### References
|
### References
|
||||||
|
|
||||||
* [Wikipedia](https://en.wikipedia.org/wiki/Selection_sort)
|
* [Wikipedia](https://en.wikipedia.org/wiki/Selection_sort)
|
||||||
* [KhanAcademy](https://www.khanacademy.org/computing/computer-science/algorithms#sorting-algorithms)
|
* [KhanAcademy](https://www.khanacademy.org/computing/computer-science/algorithms#sorting-algorithms)
|
||||||
* [MyCodeSchool](https://www.youtube.com/watch?v=GUDLRan2DWM)
|
* [MyCodeSchool](https://www.youtube.com/watch?v=GUDLRan2DWM)
|
||||||
|
Reference in New Issue
Block a user