2024-02-08 14:02:59 +00:00
---
title: Selection Sort
TARGET DECK: Obsidian::STEM
FILE TAGS: algorithm::sorting
tags:
- algorithm
- sorting
---
## Overview
2024-02-09 18:50:56 +00:00
Property | Value
----------- | --------
Best Case | $\Omega(n^2)$
Worst Case | $O(n^2)$
Avg. Case | $O(n^2)$
Aux. Memory | $O(1)$
Stable | No
Adaptive | No
2024-02-08 14:02:59 +00:00
![[selection-sort.gif]]
2024-02-11 12:33:02 +00:00
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Describe `SELECTION_SORT` in a single sentence.
2024-02-11 12:33:02 +00:00
Back: Repeatedly put the smallest unsorted record at the end of a sorted array.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707589393190 -->
END%%
2024-02-08 14:02:59 +00:00
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is `SELECTION_SORT` 's best case runtime?
2024-02-09 18:50:56 +00:00
Back: $\Omega(n^2)$
2024-02-08 14:02:59 +00:00
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707398773323 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is `SELECTION_SORT` 's worst case runtime?
2024-02-08 14:02:59 +00:00
Back: $O(n^2)$
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707398773326 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is `SELECTION_SORT` 's average case runtime?
2024-02-08 14:02:59 +00:00
Back: $O(n^2)$
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707398773327 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Is `SELECTION_SORT` in place?
2024-02-08 14:02:59 +00:00
Back: Yes
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707398773328 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Is `SELECTION_SORT` stable?
2024-02-09 18:50:56 +00:00
Back: No
2024-02-08 14:02:59 +00:00
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707398773330 -->
END%%
2024-02-09 18:50:56 +00:00
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Is `SELECTION_SORT` adaptive?
2024-02-09 18:50:56 +00:00
Back: No
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707504634778 -->
END%%
2024-02-08 14:02:59 +00:00
```c
void swap(int i, int j, int *A) {
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
void selection_sort(const int n, int A[static n]) {
for (int i = 0; i < n - 1 ; + + i ) {
int mini = i;
for (int j = i + 1; j < n ; + + j ) {
if (A[j] < A [ mini ] ) {
mini = j;
}
}
swap(i, mini, A);
}
}
```
%%ANKI
Basic
What sorting algorithm does the following demonstrate?
![[selection-sort.gif]]
2024-03-06 20:25:34 +00:00
Back: `SELECTION_SORT`
2024-02-08 14:02:59 +00:00
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707400943836 -->
END%%
## Loop Invariant
Consider [[loop-invariant|loop invariant]] $P$ given by
2024-02-09 18:50:56 +00:00
> `A[0..i-1]` is a sorted array of the `i` least elements of `A`.
2024-02-08 14:02:59 +00:00
We prove $P$ maintains the requisite properties:
* Initialization
* When `i = 0` , `A[0..-1]` is an empty array. This trivially satisfies $P$.
* Maintenance
2024-02-09 18:50:56 +00:00
* Suppose $P$ holds for some `0 ≤ i < n - 1` . Then `A[0..i-1]` is a sorted array of the `i` least elements of `A` . Our inner loop then finds the smallest element in `A[i..n]` and swaps it with `A[i]` . Therefore `A[0..i]` is a sorted array of the `i + 1` least elements of `A` . At the end of the iteration, `i` is incremented meaning `A[0..i-1]` still satisfies $P$.
2024-02-08 14:02:59 +00:00
* Termination
* On termination, `i = n - 1` and `A[0..n-2]` are the `n - 1` least elements of `A` in sorted order. But, by exhaustion, `A[n-1]` must be the largest element meaning `A[0..n-1]` , the entire array, is in sorted order.
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Given array `A[0..n-1]` , what is `SELECTION_SORT` 's loop invariant?
2024-02-08 14:02:59 +00:00
Back: `A[0..i-1]` is a sorted array of the `i` least elements of `A` .
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707398773331 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is initialization of `SELECTION_SORT` 's loop invariant?
2024-02-08 14:02:59 +00:00
Back: Sorting starts with an empty array which is trivially sorted.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707398773333 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
What is maintenance of `SELECTION_SORT` 's loop invariant?
2024-02-08 14:02:59 +00:00
Back: Each iteration puts the next least element into the sorted subarray.
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707398773334 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
How does `SELECTION_SORT` partition its input array?
2024-02-08 14:02:59 +00:00
Back:
```
[ sorted | unsorted ]
```
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707399790952 -->
END%%
%%ANKI
Basic
2024-03-06 20:25:34 +00:00
Which element will `SELECTION_SORT` move to `sorted` ?
2024-02-08 14:02:59 +00:00
```
[ sorted | unsorted ]
```
Back: The least element in `unsorted` .
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
<!-- ID: 1707399790955 -->
END%%
2024-02-09 18:50:56 +00:00
%%ANKI
Cloze
2024-03-06 20:25:34 +00:00
`SELECTION_SORT` makes fewer {swaps} than `INSERTION_SORT` in the average case.
2024-02-09 18:50:56 +00:00
Reference: Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).
2024-02-15 23:23:53 +00:00
<!-- ID: 1708002177782 -->
2024-02-09 18:50:56 +00:00
END%%
2024-02-08 14:02:59 +00:00
## References
* Thomas H. Cormen et al., *Introduction to Algorithms* , 3rd ed (Cambridge, Mass: MIT Press, 2009).