Selection Sort Algorithm PrincipleThe selection sort algorithm in C language is one of its basic algorithms. The principle of sorting is to traverse through nested loops, marking the index position of the current element and the minimum or maximum (satisfying specific conditions) element in the subsequent elements, and swapping the values at the index positions.This is a personal summary, which may be a bit convoluted. No worries, you can refer to the example code below. I have printed the process of the selection sort algorithm using the printf() function, and I believe this makes it quite clear to understand.Example Code for Selection Sort AlgorithmThe key steps can be referenced in the comments within the code, which provide clearer explanations.
#include <stdio.h>// Function to swap using a temporary variablevoid swapNumber(int *a, int *b) { int temp = *a; *a = *b; *b = temp;}// Function to print array elementsvoid printArray(int arr[], int n) { for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n");}// Function for ascending selection sortvoid selectSort(int arr[], int n) { // The key is the nested loop, the first loop marks the value of the current element for (int i = 0; i < n - 1; i++) { int minIndex = i; // Note j = i + 1, thus comparing the current element with the values of the unsorted elements behind printf("Iteration %d, comparing element value: %d\n", i+1, arr[i]); printArray(arr, n); for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { printf("%d is less than %d, setting the index of the minimum element to %d\n", arr[j], arr[minIndex], j); minIndex = j; } } // If the minimum element is not the current element marked by the first loop, swap the values at the corresponding index positions if (minIndex != i) { printf("Swapping the %d-th element marked in the first loop with the %d-th element obtained in the second loop\n", i, minIndex); swapNumber(&arr[i], &arr[minIndex]); } printf("After iteration %d: ", i+1); printArray(arr, n); }}int main() { int arr[] = {6,5,4,3,2,1}; int n = sizeof(arr) / sizeof(arr[0]); selectSort(arr, n); return 0;
After compiling and running the code, the output is:
Iteration 1, comparing element value: 6 6 5 4 3 2 1 5 is less than 6, setting the index of the minimum element to 1 4 is less than 5, setting the index of the minimum element to 2 3 is less than 4, setting the index of the minimum element to 3 2 is less than 3, setting the index of the minimum element to 4 1 is less than 2, setting the index of the minimum element to 5 Swapping the 0-th element marked in the first loop with the 5-th element obtained in the second loop After iteration 1: 1 5 4 3 2 6 Iteration 2, comparing element value: 5 1 5 4 3 2 6 4 is less than 5, setting the index of the minimum element to 2 3 is less than 4, setting the index of the minimum element to 3 Swapping the 1-th element marked in the first loop with the 4-th element obtained in the second loop After iteration 2: 1 2 4 3 5 6 Iteration 3, comparing element value: 4 1 2 4 3 5 6 3 is less than 4, setting the index of the minimum element to 3 Swapping the 2-th element marked in the first loop with the 3-th element obtained in the second loop After iteration 3: 1 2 3 4 5 6 Iteration 4, comparing element value: 4 1 2 3 4 5 6 After iteration 4: 1 2 3 4 5 6 Iteration 5, comparing element value: 5 1 2 3 4 5 6 After iteration 5: 1 2 3 4 5 6
Disclaimer: The content is for reference only and does not guarantee accuracy; it should not be used as the basis for any decisions!