Selection Sort

 

🌟 Selection Sort 

Selection Sort is a simple sorting algorithm.
It works by repeatedly finding the minimum (or maximum) element from the unsorted part of the array and moving it to the beginning.

✏️ How Selection Sort works step-by-step:

  • Start with the first element of the array.

  • Find the smallest element in the array.

  • Swap it with the first element.

  • Move to the second position, and repeat the process for the remaining elements.

  • Continue until the entire array is sorted.


Selection Sort Algorithm

Input: An array arr[] of size n
Output: Sorted array in ascending order

for i = 0 to i< n-1 do min_index = i for j = i+1 to i < n-1 do if arr[j] < arr[min_index] then min_index = j if min_index ≠ i then swap arr[i] and arr[min_index]

🔥 Let's Break it Down:

  • Outer loop (i): For each position in the array.

  • Inner loop (j): Find the smallest element in the unsorted part.

  • Swap the smallest found with the current ith element.


📜 Simple Example

Suppose the array is:
arr = [64, 25, 12, 22, 11]

Steps:

  1. Find the minimum from index 0 to 4 → 11 → Swap 11 and 64 → [11, 25, 12, 22, 64]

  2. Find the minimum from index 1 to 4 → 12 → Swap 12 and 25 → [11, 12, 25, 22, 64]

  3. Find the minimum from index 2 to 4 → 22 → Swap 22 and 25 → [11, 12, 22, 25, 64]

  4. Find the minimum from index 3 to 4 → 25 already minimum → No swap

  5. Last element is automatically sorted.

Final sorted array: [11, 12, 22, 25, 64]


✅ Key Points

  • Time Complexity:

    • Best Case: O(n²)

    • Average Case: O(n²)

    • Worst Case: O(n²)

  • Space Complexity: O(1) (In-place sorting)

  • Stability:Not stable (unless modified carefully)

  • Useful: When memory is very limited.


C Program Selection Sort


#include <stdio.h>

// Function to perform Selection Sort
void selectionSort(int arr[], int n) {
    int i, j, min_idx, temp;
    
    for (i = 0; i < n-1; i++) {
        min_idx = i;
        // Find the minimum element in the unsorted part
        for (j = i+1; j < n; j++) {
            if (arr[j] < arr[min_idx])
                min_idx = j;
        }
        // Swap the found minimum with the first element
        if (min_idx != i) {
            temp = arr[i];
            arr[i] = arr[min_idx];
            arr[min_idx] = temp;
        }
    }
}

void printArray(int arr[], int n) {
    for (int i=0; i<n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
void readArray(int arr[], int n) {
    for (int i=0; i<n; i++)
        scanf("%d", &arr[i]);
    
}
int main() {
    int arr[50] ;
    int n;
    
    printf("Enter the number of elements \n");
    scanf("%d",&n);
    printf("Enter the array elements\n");
    readArray(arr,n);
    
    printf("Original array: ");
    printArray(arr, n);

    selectionSort(arr, n);

    printf("Sorted array: ");
    printArray(arr, n);

    return 0;
}

Output
Enter the number of elements 
5
Enter the array elements
6 3 9 1 2
Original array: 6 3 9 1 2 
Sorted array: 1 2 3 6 9 

Comments

Popular posts from this blog

Data Structures Lab PCCSL307 KTU 2024 Scheme - Dr Binu V P

Sparse Matrix - Transpose and Addition

Polynomial Addition using Arrays