Skip to content

Commit 5cae1e1

Browse files
authored
Merge pull request deutranium#1 from Grasstown/SelectionSort
Add readme for SelectionSort
2 parents 83d76f5 + af13bf0 commit 5cae1e1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Selection Sort
2+
Selection sort is an algorithm that separates an array into two subsections: a sorted section, and an unsorted section. The array is sorted by moving the lowest value element to the beginning of the unsorted section. It can also be adjusted to sort in reverse order.
3+
```
4+
selection sort(array)
5+
for i <- 0 to indexOfLastElement:
6+
k = i
7+
for j <- i + 1 to indexOfLastElement:
8+
if (array[j] < array[k])
9+
k = j
10+
swap array[k] and array[i]
11+
```
12+
13+
# Time Complexities:
14+
- Worst Case/Average: O(n<sup>2</sup>)
15+
- Best Case: O(n)
16+
Selection sort is inefficient on large lists.
17+
18+
# Space Complexity:
19+
- O(1)
20+
Selection sort has a space complexity of O(1), making it efficient when memory is lacking.
21+
22+
# Instructions for running Code:
23+
- cpp
24+
```
25+
g++ selectionSort.cc
26+
./a.out
27+
```
28+
- python
29+
```
30+
python3 selectionSort.py

0 commit comments

Comments
 (0)