Selection Sort repeatedly selects the smallest element from the unsorted part and places it at the beginning.
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[j] < arr[minIndex])
minIndex = j;
swap(arr[minIndex], arr[i]);
}
}
Selection sort is conceptually simple but inefficient for large datasets.
Take quizzes related to this topic and see where you stand!
Start Quiz Now