Sorting Algorithms: Understanding Selection Sort

Sorting Algorithms: Understanding Selection Sort

Data Structure Algorithm

Introduction

Sorting is a fundamental operation in computer science, essential for various applications ranging from databases to algorithms. One such sorting algorithm is Selection Sort. In this blog post, we'll explore the concept of Selection Sort, provide pseudocode, analyze its time and space complexity, and finally, implement it in C++.

Selection Sort Overview

Selection Sort is a simple comparison-based sorting algorithm. The idea behind it is to divide the input list into two parts: a sorted and an unsorted region. The algorithm repeatedly selects the smallest (or largest) element from the unsorted region and swaps it with the first element of the unsorted region. This process continues until the entire list is sorted.

Pseudocode for Selection Sort

Here's the pseudocode for Selection Sort:

lessCopy codefunction selectionSort(arr):
    n = length(arr)
    for i from 0 to n-1:
        minIndex = i
        for j from i+1 to n:
            if arr[j] < arr[minIndex]:
                minIndex = j
        swap(arr[i], arr[minIndex])
  • n represents the number of elements in the array.

  • The outer loop iterates through each element of the array.

  • minIndex is used to keep track of the index of the minimum element found during the inner loop.

  • The inner loop starts from the next element after the current one and searches for the minimum element in the unsorted region.

  • If a smaller element is found, minIndex is updated.

  • After the inner loop, the minimum element is swapped with the first element of the unsorted region.

Time Complexity

The time complexity of Selection Sort is O(n^2), where 'n' is the number of elements in the array. This is because, for each element in the array, we iterate through the remaining unsorted elements to find the minimum element.

Space Complexity

The space complexity of Selection Sort is O(1), indicating that the algorithm uses a constant amount of extra memory.

C++ Implementation

Now, let's implement the Selection Sort algorithm in C++:

#include <iostream>

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;
            }
        }
        std::swap(arr[i], arr[minIndex]);
    }
}

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

    selectionSort(arr, n);

    std::cout << "Sorted array: ";
    for (int i = 0; i < n; ++i) {
        std::cout << arr[i] << " ";
    }

    return 0;
}

Conclusion

Selection Sort, although not the most efficient sorting algorithm, provides a clear and simple approach to sorting elements in an array. Understanding its pseudocode, time complexity, space complexity, and implementation in C++ contributes to a foundational understanding of sorting algorithms.

Did you find this article valuable?

Support Xander Billa by becoming a sponsor. Any amount is appreciated!