Selection Sort is a simple and intuitive comparison-based sorting algorithm. It repeatedly selects the minimum (or maximum) element from the unsorted part and places it at the beginning.
Initial Array: [29, 10, 14, 37, 14]
#include <stdio.h>
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
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
int main() {
int a[] = {29, 10, 14, 37, 14};
int n = 5;
selectionSort(a, n);
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
return 0;
}
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!