
Selection Sort
This sorting algorithm works by repeatedly finding the smallest element in an array and swapping it with the first element.
Use Case
void SelectionSort(int arr[], int n){
for (int i = 0; i < n-1; i++){
int minIndex = i;
for (int j = i+1; j < n; j++){
if(arr[minIndex] > arr[j]){ // 0 > j..., 1 > j..., 2 > j..., (n-1) > j
minIndex = j; // minIndex value will be updated, when the value at arr[minIndex(i)] is greater than the value at arr[j] index
}
}
swap(arr[i], arr[minIndex]);
}
}

Last updated