TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Implementation of the Selection sort implementation using recursion. More...
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Go to the source code of this file.
Namespaces | |
namespace | sorting |
for working with vectors | |
namespace | selection_sort_recursive |
Functions for the Selection sort implementation using recursion. | |
Functions | |
template<typename T > | |
uint64_t | sorting::selection_sort_recursive::findMinIndex (const std::vector< T > &in_arr, uint64_t current_position=0) |
The main function finds the index of the minimum element. | |
template<typename T > | |
void | sorting::selection_sort_recursive::selectionSortRecursive (std::vector< T > &in_arr, uint64_t current_position=0) |
The main function implements Selection sort. | |
static void | test () |
Self-test implementations. | |
int | main () |
Main function. | |
Implementation of the Selection sort implementation using recursion.
The selection sort algorithm divides the input list into two parts: a sorted sublist of items which is built up from left to right at the front (left) of the list, and a sublist of the remaining unsorted items that occupy the rest of the list. Initially, the sorted sublist is empty, and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on the sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.
FindMinIndex This function finds the minimum element of the array(list) recursively by simply comparing the minimum element of array reduced size by 1 and compares it to the last element of the array to find the minimum of the whole array.
SelectionSortRecursive Just like selection sort, it divides the list into two parts (i.e.: sorted and unsorted) and finds the minimum of the unsorted array. By calling the FindMinIndex
function, it swaps the minimum element with the first element of the list, and then solves recursively for the remaining unsorted list.
Definition in file selection_sort_recursive.cpp.
uint64_t sorting::selection_sort_recursive::findMinIndex | ( | const std::vector< T > & | in_arr, |
uint64_t | current_position = 0 ) |
The main function finds the index of the minimum element.
T | type of array |
in_arr | array whose minimum element is to be returned |
current_position | position/index from where the in_arr starts |
Definition at line 56 of file selection_sort_recursive.cpp.
int main | ( | void | ) |
Main function.
Definition at line 130 of file selection_sort_recursive.cpp.
void sorting::selection_sort_recursive::selectionSortRecursive | ( | std::vector< T > & | in_arr, |
uint64_t | current_position = 0 ) |
The main function implements Selection sort.
T | type of array |
in_arr | array to be sorted, |
current_position | position/index from where the in_arr starts |
Definition at line 76 of file selection_sort_recursive.cpp.
|
static |
Self-test implementations.
Definition at line 95 of file selection_sort_recursive.cpp.