> For the complete documentation index, see [llms.txt](https://chloe-codes1.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chloe-codes1.gitbook.io/til/algorithm/sorting-methods/selection_sort.md).

# Selection Sort

<br>

### Iterative Selection Sort

```python
def SelectionSort(arr):
    N = len(arr)
    for i in range(N-1):
        MIN = i
        for j in range(i+1, N):
            if arr[j] < arr[MIN]:
                MIN = j
        arr[MIN], arr[i] = arr[i], arr[MIN]
```

<br>

### Recursive Selection Sort

```python
def SelectionSort(arr, s):
    N = len(arr)
    if s == N-1:
        return
    MIN = s
    for i in range(s, N):
        if arr[MIN] > arr[i]:
            MIN = i
    arr[s], arr[MIN] = arr[MIN], arr[s]
    SelectionSort(arr, s+1)
```
