# Algorithms_in_C++: sorting/selection_sort_recursive.cpp File Reference

Loading...
Searching...
No Matches
selection_sort_recursive.cpp File Reference

Implementation of the Selection sort implementation using recursion. More...

`#include <algorithm>`
`#include <cassert>`
`#include <iostream>`
`#include <vector>`
Include dependency graph for selection_sort_recursive.cpp:

## 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. More...
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. More...
static void | test ()
| Self-test implementations. More...
int | main ()
| Main function. More...

## Detailed Description

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.

### Implementation

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.

Author
  Tushar Khanduri

## Function Documentation

## findMinIndex()

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 Parameters

  T | type of array

Parameters

  in_arr | array whose minimum element is to be returned
  current_position | position/index from where the in_arr starts

Returns
  index of the minimum element

{
if (current_position + 1 == in_arr.size()) {
return current_position;
}
uint64_t answer = findMinIndex(in_arr, current_position + 1);
if (in_arr[current_position] < in_arr[answer]) {
answer = current_position;
}
return answer;
}
sorting::selection_sort_recursive::findMinIndex
uint64_t findMinIndex(const std::vector< T > &in_arr, uint64_t current_position=0)
The main function finds the index of the minimum element.
Definition: selection_sort_recursive.cpp:55
std::vector::size
T size(T... args)
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // run self-test implementations
return 0;
}
test
static void test()
Self-test implementations.
Definition: selection_sort_recursive.cpp:94
Here is the call graph for this function:

## selectionSortRecursive()

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.

Template Parameters

  T | type of array

Parameters

  in_arr | array to be sorted,
  current_position | position/index from where the in_arr starts

Returns
  void

{
if (current_position == in_arr.size()) {
return;
}
uint64_t min_element_idx =
selection_sort_recursive::findMinIndex(in_arr, current_position);
if (min_element_idx != current_position) {
std::swap(in_arr[min_element_idx], in_arr[current_position]);
}
selectionSortRecursive(in_arr, current_position + 1);
}
sorting::selection_sort_recursive::selectionSortRecursive
void selectionSortRecursive(std::vector< T > &in_arr, uint64_t current_position=0)
The main function implements Selection sort.
Definition: selection_sort_recursive.cpp:75
std::swap
T swap(T... args)
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Self-test implementations.

Returns
  void

{
// 1st test
// [1, 0, 2, 1] return [0, 1, 1, 2]
std::vector<uint64_t> array1 = {0, 1, 1, 2};
std::cout << "1st test... ";
sorting::selection_sort_recursive::selectionSortRecursive(array1);
assert(std::is_sorted(std::begin(array1), std::end(array1)));
std::cout << "passed" << std::endl;
// 2nd test
// [1, 0, 0, 1, 1, 0, 2, 1] return [0, 0, 0, 1, 1, 1, 1, 2]
std::vector<uint64_t> array2 = {1, 0, 0, 1, 1, 0, 2, 1};
std::cout << "2nd test... ";
sorting::selection_sort_recursive::selectionSortRecursive(array2);
assert(std::is_sorted(std::begin(array2), std::end(array2)));
std::cout << "passed" << std::endl;
// 3rd test
// [1, 1, 0, 0, 1, 2, 2, 0, 2, 1] return [0, 0, 0, 1, 1, 1, 1, 2, 2, 2]
std::vector<uint64_t> array3 = {1, 1, 0, 0, 1, 2, 2, 0, 2, 1};
std::cout << "3rd test... ";
sorting::selection_sort_recursive::selectionSortRecursive(array3);
assert(std::is_sorted(std::begin(array3), std::end(array3)));
std::cout << "passed" << std::endl;
// 4th test
// [2, 2, 2, 0, 0, 1, 1] return [0, 0, 1, 1, 2, 2, 2]
std::vector<uint64_t> array4 = {2, 2, 2, 0, 0, 1, 1};
std::cout << "4th test... ";
sorting::selection_sort_recursive::selectionSortRecursive(array4);
assert(std::is_sorted(std::begin(array4), std::end(array4)));
std::cout << "passed" << std::endl;
}
std::cout
std::begin
T begin(T... args)
std::end
T end(T... args)
std::endl
T endl(T... args)
std::is_sorted
T is_sorted(T... args)
std::vector
Here is the call graph for this function: