Basic Sorting Algorithm
Selection Sort
Procedure
- Find the smallest element in the array
- Swap it to the leftmost position of the unsorted portion of the array
- Mark that position as part of a sorted portion of the array
- Repeat the process for the remaining unsorted portion
- Finish when there's only one element left unsorted
Analysis
- Worst case
comparisons and swaps
- Best case
comparisons
- Operation
- Heavy Comparison
- Light swap
- Runtime consistency
- Easier to code or debug
Code
cpp
void selectionSort(Vector<int>& v) {
for (int sorted = 0; sorted < v.size() – 1; sorted++) {
int minIndex = sorted;
for (int unsorted = sorted + 1; unsorted < v.size(); unsorted++) {
if (v[unsorted] < v[minIndex]) {
minIndex = unsorted;
}
swap(v, sorted, minIndex);
}
}
For arrays (assume all cells are initialized)
cpp
void selectionSort(int *arr, int size) {
int tmp;
for (int sorted = 0; sorted < size - 1; sorted++) {
int minIndex = sorted;
// Find minimum index on unsorted portion
for (int unsorted = sorted + 1; unsorted < size; unsorted++) {
if (arr[unsorted] < arr[minIndex]) {
minIndex = unsorted;
}
}
// Swapping
tmp = arr[sorted];
arr[sorted] = arr[minIndex];
arr[minIndex] = tmp;
}
}
Insertion Sort
Procedure
- Assume the first element is sorted
- Pull the first element of the unsorted partition of the array
- In the sorted partition, scooch everything over that is greater than that element.
- Stick the element into the hole left for it in the sorted portion of the array.
- Finish when no unsorted left
Analysis
- Worst case
- Comparations and head insertion
swaps
- Best case
- Only
comparations
- Only
- Operation
- Swap heavy
- Comparison light
- Faster in special cases
- Potential harder to code
Code
Vector version:
cpp
void insertionSort(Vector<int>& v) {
for (int i = 1; i < v.size(); i++) {
int val = v[i];
int gap = i;
for (int j = gap - 1; j >= 0 && v[j] > val; j--) {
v[j + 1] = v[j];
gap--;
}
v[gap] = val;
}
}
Array version (assume all cells are initialized):
cpp
void insertionSort(int *arr, int size) {
for (int unsorted = 1; unsorted < size; unsorted++) {
val = arr[unsorted]; // Pull the first unsorted
gap = unsorted; // If nothing is greater, stay sill
for (int sorted = unsorted - 1; sorted >= 0; sorted--) {
if (arr[sorted] > val) {
// Move forward the greater element
arr[sorted + 1] = arr[sorted];
// Move back the gap
gap--;
}
}
arr[gap] = val; // Filling the gap
}
}