Sort an Array using Selection Sort in C

To sort an array using the Selection Sort algorithm in C, we repeatedly find the minimum element from the unsorted part and swap it with the first element of the unsorted part. This process continues until the entire array is sorted.


Understanding Selection Sort

Selection Sort is an in-place, comparison-based sorting algorithm that works by dividing the array into two parts: the sorted and the unsorted part. In each iteration, the smallest element from the unsorted part is selected and placed in the correct position.

Examples of Selection Sort in C

1. Sorting an Array in Ascending Order

In this example, we will implement the Selection Sort algorithm to sort an array in ascending order.

main.c

</>
Copy
#include <stdio.h>

// Function to perform Selection Sort
void selectionSort(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        int minIndex = i;
        
        // Find the minimum element in the unsorted part
        for (int j = i + 1; j < size; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }

        // Swap the found minimum element with the first element
        int temp = arr[minIndex];
        arr[minIndex] = arr[i];
        arr[i] = temp;
    }
}

// Function to print the array
void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int numbers[] = {64, 25, 12, 22, 11};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    printf("Original array: ");
    printArray(numbers, size);

    selectionSort(numbers, size);

    printf("Sorted array: ");
    printArray(numbers, size);

    return 0;
}

Explanation:

  1. We define a function selectionSort() to implement the Selection Sort algorithm.
  2. We iterate through the array using a loop, setting minIndex to the current index. Reference: For Loop.
  3. A nested loop finds the smallest element in the unsorted part of the array. Reference: Nested Loops.
  4. If a smaller element is found, we update minIndex.
  5. After finding the minimum, we swap it with the first unsorted element.
  6. We define printArray() to print the array before and after sorting.
  7. In main(), we declare and initialize an array, call selectionSort(), and print the sorted result.

Output:

Original array: 64 25 12 22 11 
Sorted array: 11 12 22 25 64

2. Sorting an Array in Descending Order

In this example, we modify the Selection Sort algorithm to sort an array in descending order.

main.c

</>
Copy
#include <stdio.h>

// Function to perform Selection Sort in descending order
void selectionSortDescending(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        int maxIndex = i;
        
        // Find the maximum element in the unsorted part
        for (int j = i + 1; j < size; j++) {
            if (arr[j] > arr[maxIndex]) {
                maxIndex = j;
            }
        }

        // Swap the found maximum element with the first element
        int temp = arr[maxIndex];
        arr[maxIndex] = arr[i];
        arr[i] = temp;
    }
}

int main() {
    int numbers[] = {10, 5, 20, 15, 25};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    printf("Original array: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\n");

    selectionSortDescending(numbers, size);

    printf("Sorted array in descending order: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", numbers[i]);
    }
    printf("\n");

    return 0;
}

Explanation:

  1. We define selectionSortDescending() to perform Selection Sort in descending order.
  2. Instead of finding the minimum element, we find the maximum element and swap it.
  3. We iterate through the array using maxIndex instead of minIndex.
  4. In the nested loop, we update maxIndex when a larger element is found.
  5. After finding the maximum, we swap it with the first unsorted element.
  6. In main(), we call selectionSortDescending() to sort the array.

Output:

Original array: 10 5 20 15 25 
Sorted array in descending order: 25 20 15 10 5

Conclusion

In this tutorial, we covered how to sort an array using Selection Sort in C:

  1. We implemented Selection Sort for ascending order.
  2. We modified it to sort an array in descending order.
  3. We explained the working of each function and its logic step by step.

Selection Sort is simple but inefficient for large datasets. It is useful for educational purposes and small arrays where simplicity is preferred over efficiency.