Sorting an Array Using Selection Sort Algorithm in C++

This exercise demonstrates how to implement the Selection Sort algorithm in C++ to sort an array of integers. Selection Sort is a simple and intuitive comparison-based sorting algorithm. It works by repeatedly finding the minimum element from the unsorted part of the array and placing it at the beginning. Although not the most efficient for large datasets due to its O(n²) time complexity, it is a great way to understand basic sorting principles and array manipulation. By completing this exercise, you will learn how to traverse arrays, use nested loops, and swap elements effectively.

Group

Search and Sort Algorithms in C++

Objective

Create a function called selectionSort that takes an integer array and its size as parameters. Implement the logic to find the minimum element in each pass and swap it with the current element. In the main function, define an unsorted array, apply the selectionSort function to sort it, and then print the sorted array.

Write a function that sorts an array using the selection sort algorithm.

Example C++ Exercise

 Copy C++ Code
#include <iostream>   // Include input-output stream library
using namespace std;

// Function to perform selection sort
void selectionSort(int arr[], int n) {
    // Traverse the entire array
    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;  // Assume the current index has the smallest value

        // Search for the smallest element in the unsorted part
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;  // Update index if smaller element is found
            }
        }

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

// Main function
int main() {
    // Define an unsorted array
    int arr[] = {29, 10, 14, 37, 13};
    int size = sizeof(arr) / sizeof(arr[0]);  // Calculate the array size

    // Call the selectionSort function to sort the array
    selectionSort(arr, size);

    // Print the sorted array
    cout << "Sorted array: ";
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    return 0;  // End of program
}

 Output

Sorted array: 10 13 14 29 37 

Share this C++ Exercise


More C++ Programming Exercises of Search and Sort Algorithms in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Finding the Smallest Number in an Array Using Sequential Search in C++

    This exercise focuses on implementing a sequential search algorithm in C++ to identify the smallest number in a given array. Sequential search, also known as linear search, is one ...

  • Linear Search Algorithm in C++

    This exercise focuses on implementing the linear search algorithm in C++. Linear search is a simple searching algorithm that checks each element in a list or array in a sequential ...

  • MergeSort Algorithm in C++

    In this exercise, you will implement the MergeSort algorithm in C++. MergeSort is a highly efficient, divide-and-conquer sorting algorithm that works by dividing the input array in...

  • Bubble Sort Algorithm for Sorting Strings in C++

    In this exercise, you will implement the Bubble Sort algorithm to sort an array of strings alphabetically. Bubble Sort is a simple comparison-based sorting algorithm that repeatedl...

  • Count the Occurrences of a Number in an Array in C++

    In this exercise, you will develop a C++ function that counts how many times a specific number appears in an array. This task involves iterating through the array, comparing each e...

  • Bubble Sort Algorithm Implementation in C++

    This C++ exercise demonstrates the implementation of the Bubble Sort algorithm, a fundamental sorting technique used to order elements in a list. Bubble Sort works by repeatedly st...