Selection Sort is a simple comparison-based sorting algorithm. It works by repeatedly selecting the smallest (or largest) element from the unsorted portion of the array and placing it in its correct position in the sorted portion.
Key Concepts of Selection Sort:
- Selection:
- Find the smallest (or largest) element in the unsorted portion of the array.
- Swapping:
- Swap the smallest (or largest) element with the first element of the unsorted portion.
- Iteration:
- Repeat the process for the remaining unsorted portion until the entire array is sorted.
Characteristics of Selection Sort:
- Time Complexity:
- Best Case: O(n2)O(n^2)O(n2)
- Average Case: O(n2)O(n^2)O(n2)
- Worst Case: O(n2)O(n^2)O(n2)
- The number of comparisons is always n(n−1)/2n(n-1)/2n(n−1)/2, regardless of input.
- Space Complexity:
- O(1)O(1)O(1) (in-place sorting).
- Stability:
- Selection Sort is not stable by default because swapping may change the relative order of equal elements.
Implementation of Selection Sort in C:
C Code Example:
#include <stdio.h>
// Function to perform Selection Sort
void selectionSort(int arr[], int n) {
for (int i = 0; i < n – 1; i++) {
int minIdx = i; // Index of the smallest element
// Find the smallest element in the remaining array
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
// Swap the smallest element with the first element of the unsorted part
if (minIdx != i) {
int temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
}
}
// Utility function to print an array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
}
// Main function
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printf(“Original array: “);
printArray(arr, n);
selectionSort(arr, n);
printf(“Sorted array: “);
printArray(arr, n);
return 0;
}
How It Works:
Example Walkthrough:
For the array {64, 25, 12, 22, 11}:
- First Pass:
- Find the smallest element in the array: 11.
- Swap 11 with the first element: {11, 25, 12, 22, 64}.
- Second Pass:
- Find the smallest element in the remaining array: 12.
- Swap 12 with the second element: {11, 12, 25, 22, 64}.
- Third Pass:
- Find the smallest element in the remaining array: 22.
- Swap 22 with the third element: {11, 12, 22, 25, 64}.
- Fourth Pass:
- Find the smallest element in the remaining array: 25.
- Swap 25 with the fourth element: {11, 12, 22, 25, 64}.
- Final Array:
- {11, 12, 22, 25, 64}
Advantages and Disadvantages:
Advantages:
- Simple:
- Easy to understand and implement.
- In-Place Sorting:
- Requires minimal extra memory.
- Predictable Performance:
- The number of comparisons is consistent.
Disadvantages:
- Inefficient:
- Poor performance on large datasets due to O(n2)O(n^2)O(n2) complexity.
- Not Stable:
- May not preserve the relative order of equal elements.
- Always Performs n(n−1)/2n(n-1)/2n(n−1)/2 Comparisons:
- Even if the array is already sorted.
Example Output:
For the input array {64, 25, 12, 22, 11}:
Original array: 64 25 12 22 11
Sorted array: 11 12 22 25 64
When to Use Selection Sort:
- Small Datasets:
- Suitable for small arrays due to simplicity.
- Learning:
- Useful for educational purposes to understand the concept of sorting.
- Memory Constraints:
- Requires minimal additional memory.
Selection Sort is a straightforward algorithm but is inefficient for large datasets. For better performance, consider using more advanced algorithms like Merge Sort or Quick Sort for larger arrays.