Sorting is a fundamental operation in computer science and programming. One popular sorting algorithm is the Selection Sort, known for its simplicity and ease of implementation. In this article, we will explore the Selection Sort algorithm in C++. We will provide multiple examples and code explanations to help beginners understand and implement this sorting technique.
Selection Sort is an inplace comparisonbased sorting algorithm. It works by dividing the input array into two parts: the sorted and unsorted portions. The algorithm repeatedly selects the smallest (or largest) element from the unsorted part and swaps it with the first element of the unsorted part. This process continues until the entire array is sorted.
Let's dive into the code implementation of the Selection Sort algorithm in C++. We'll start with a simple example and gradually expand our understanding.
Example 1: Sorting an Array of Integers
#include <iostream>
void selectionSort(int arr[], int size) {
for (int i = 0; i < size  1; ++i) {
int minIndex = i;
for (int j = i + 1; j < size; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
std::swap(arr[i], arr[minIndex]);
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int size = sizeof(arr) / sizeof(arr[0]);
std::cout << "Original Array: ";
for (int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
selectionSort(arr, size);
std::cout << "\nSorted Array: ";
for (int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
return 0;
}
Original Array: 64 25 12 22 11
Sorted Array: 11 12 22 25 64
Example 2: Sorting a Vector of Strings
#include <iostream>
#include <vector>
#include <algorithm>
void selectionSort(std::vector<std::string>& vec) {
for (std::size_t i = 0; i < vec.size()  1; ++i) {
std::size_t minIndex = i;
for (std::size_t j = i + 1; j < vec.size(); ++j) {
if (vec[j] < vec[minIndex]) {
minIndex = j;
}
}
std::swap(vec[i], vec[minIndex]);
}
}
int main() {
std::vector<std::string> names = {"Alice", "John", "David", "Bob", "Eve"};
std::cout << "Original Vector: ";
for (const auto& name : names) {
std::cout << name << " ";
}
selectionSort(names);
std::cout << "\nSorted Vector: ";
for (const auto& name : names) {
std::cout << name << " ";
}
return 0;
}
Original Vector: Alice John David Bob Eve
Sorted Vector: Alice Bob David Eve John
1. Write a C++ program to sort an array of integers in descending order using the Selection Sort algorithm.
#include <iostream>
void selectionSortDescending(int arr[], int size) {
for (int i = 0; i < size  1; ++i) {
int maxIndex = i;
for (int j = i + 1; j < size; ++j) {
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
}
}
std::swap(arr[i], arr[maxIndex]);
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int size = sizeof(arr) / sizeof(arr[0]);
std::cout << "Original Array: ";
for (int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
selectionSortDescending(arr, size);
std::cout << "\nSorted Array (Descending): ";
for (int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
return 0;
}
Original Array: 64 25 12 22 11
Sorted Array (Descending): 64 25 22 12 11
2. Write a C++ program to sort a vector of floatingpoint numbers using the Selection Sort algorithm.
#include <iostream>
#include <vector>
#include <algorithm>
void selectionSort(std::vector<double>& vec) {
for (std::size_t i = 0; i < vec.size()  1; ++i) {
std::size_t minIndex = i;
for (std::size_t j = i + 1; j < vec.size(); ++j) {
if (vec[j] < vec[minIndex]) {
minIndex = j;
}
}
std::swap(vec[i], vec[minIndex]);
}
}
int main() {
std::vector<double> numbers = {3.14, 1.23, 2.71, 0.99, 1.88};
std::cout << "Original Vector: ";
for (const auto& number : numbers) {
std::cout << number << " ";
}
selectionSort(numbers);
std::cout << "\nSorted Vector: ";
for (const auto& number : numbers) {
std::cout << number << " ";
}
return 0;
}
Original Vector: 3.14 1.23 2.71 0.99 1.88
Sorted Vector: 0.99 1.23 1.88 2.71 3.14
By understanding and implementing the Selection Sort algorithm in C++, you can easily sort arrays or vectors in ascending or descending order. It's a great starting point for learning about sorting algorithms and lays the foundation for more advanced techniques.
Remember, practice is key to mastering any algorithm. Try solving additional problems and explore other sorting algorithms to deepen your understanding of sorting in C++. Happy coding!
70 videos45 docs15 tests

70 videos45 docs15 tests


Explore Courses for Software Development exam
