sorting Namespace Reference

Sorting algorithms. More...

## Functions

template<typename T , size_t N>
std::array< T, N > | shuffle (std::array< T, N > arr)
template<typename T , size_t N>
std::array< T, N > | randomized_bogosort (std::array< T, N > arr)
template<typename T >
void | gnomeSort (T *arr, int size)
template<typename T , size_t size>
std::array< T, size > | gnomeSort (std::array< T, size > arr)
template<typename T >
void | insertionSort (T *arr, int n)
| Insertion Sort Function. More...
template<typename T >
void | insertionSort (std::vector< T > *arr)
template<class Iterator >
void | merge (Iterator l, Iterator r, const Iterator e, char b[])
| merges 2 sorted adjacent segments into a larger sorted segment More...
template<class Iterator >
void | non_recursive_merge_sort (const Iterator first, const Iterator last, const size_t n)
| bottom-up merge sort which sorts elements in a non-decreasing order More...
template<class Iterator >
void | non_recursive_merge_sort (const Iterator first, const size_t n)
| bottom-up merge sort which sorts elements in a non-decreasing order More...
template<class Iterator >
void | non_recursive_merge_sort (const Iterator first, const Iterator last)
| bottom-up merge sort which sorts elements in a non-decreasing order More...
template<std::size_t N>
std::array< int, N > | pigeonSort (std::array< int, N > arr)
int | partition (int arr[], int low, int high)
void | quickSort (int arr[], int low, int high)
template<typename T >
void | quicksort (std::vector< T > *arr, int32_t low, int32_t high)
template<typename T >
std::vector< T > | quicksort (std::vector< T > arr, int32_t low, int32_t high)
template<typename T >
void | shell_sort (T *arr, size_t LEN)
template<typename T , size_t N>
void | shell_sort (T(&arr)[N])
template<typename T >
void | shell_sort (std::vector< T > *arr)

## Detailed Description

Sorting algorithms.

for io operations

Sorting Algorithms.

for std::vector

for assert for typedef datatype uint64_t for IO operations

Sorting algorithms

for std::is_sorted, std::swap for assert for io operations

Sorting algorithms

Sorting algorithms

header files for collection of functions for a macro called assert which can be used to verify assumptions for io operations

Sorting algorithms

## Function Documentation

## gnomeSort() [1/2]

template<typename T , size_t size>

std::array<T, size> sorting::gnomeSort | ( | std::array< T, size > | arr | ) |

This implementation is for a C++-style array input. The function argument is a pass-by-value and hence a copy of the array gets created which is then modified by the function and returned.

Template Parameters

  T | type of data variables in the array
  size | size of the array

Parameters

  [in] | arr | our array of elements.

Returns
  array with elements sorted

{
// few easy cases
if (size <= 1) {
return arr;
}
int index = 0; // initialize loop index
while (index < size) {
// check for swap
if ((index == 0) || (arr[index] >= arr[index - 1])) {
index++;
} else {
std::swap(arr[index], arr[index - 1]); // swap
index--;
}
}
return arr;
}
std::swap
T swap(T... args)

## gnomeSort() [2/2]

template<typename T >

void sorting::gnomeSort | ( | T * | arr,
|  | int | size
| ) |  |

This implementation is for a C-style array input that gets modified in place.

Parameters

  [in,out] | arr | our array of elements.
  | size | size of given array

{
// few easy cases
if (size <= 1) {
return;
}
int index = 0; // initialize some variables.
while (index < size) {
// check for swap
if ((index == 0) || (arr[index] >= arr[index - 1])) {
index++;
} else {
std::swap(arr[index], arr[index - 1]); // swap
index--;
}
}
}

## insertionSort() [1/2]

template<typename T >

void sorting::insertionSort | ( | std::vector< T > * | arr | ) |

Insertion Sort Function

Template Parameters

  T | type of array

Parameters

  [in,out] | arr | pointer to array to be sorted

{
size_t n = arr->size();
for (size_t i = 1; i < n; i++) {
T temp = arr[0][i];
int32_t j = i - 1;
while (j >= 0 && temp < arr[0][j]) {
arr[0][j + 1] = arr[0][j];
j--;
}
arr[0][j + 1] = temp;
}
}
std::vector::size
T size(T... args)
Here is the call graph for this function:

## insertionSort() [2/2]

template<typename T >

void sorting::insertionSort | ( | T * | arr,
|  | int | n
| ) |  |

Insertion Sort Function.

Template Parameters

  T | type of array

Parameters

  [in,out] | arr | Array to be sorted
  | n | Size of Array

{
for (int i = 1; i < n; i++) {
T temp = arr[i];
int j = i - 1;
while (j >= 0 && temp < arr[j]) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}

## merge()

template<class Iterator >

void sorting::merge | ( | Iterator | l,
|  | Iterator | r,
|  | const Iterator | e,
|  | char | b[]
| ) |  |

merges 2 sorted adjacent segments into a larger sorted segment

best-case = worst-case = O(n)

Parameters

  l | points to the left part
  r | points to the right part, end of left part
  e | points to end of right part
  b | points at the buffer

{
// create 2 pointers to point at the buffer
auto p(reinterpret_cast<std::remove_reference_t<decltype(*l)>*>(b)), c(p);
// move the left part of the segment
for (Iterator t(l); r != t; ++t) *p++ = std::move(*t);
// while neither the buffer nor the right part has been exhausted
// move the smallest element of the two back to the container
while (e != r && c != p) *l++ = std::move(*r < *c ? *r++ : *c++);
// notice only one of the two following loops will be executed
// while the right part hasn't bee exhausted, move it back
while (e != r) *l++ = std::move(*r++);
// while the buffer hasn't bee exhausted, move it back
while (c != p) *l++ = std::move(*c++);
}
std::move
T move(T... args)
Here is the call graph for this function:

## non_recursive_merge_sort() [1/3]

template<class Iterator >

void sorting::non_recursive_merge_sort | ( | const Iterator | first,
|  | const Iterator | last
| ) |  |

bottom-up merge sort which sorts elements in a non-decreasing order

Parameters

  first | points to the first element
  last | points to 1-step past the last element

{
non_recursive_merge_sort(first, last, last - first);
}
sorting::non_recursive_merge_sort
void non_recursive_merge_sort(const Iterator first, const Iterator last)
bottom-up merge sort which sorts elements in a non-decreasing order
Definition: non_recursive_merge_sort.cpp:86
Here is the call graph for this function:

## non_recursive_merge_sort() [2/3]

template<class Iterator >

void sorting::non_recursive_merge_sort | ( | const Iterator | first,
|  | const Iterator | last,
|  | const size_t | n
| ) |  |

bottom-up merge sort which sorts elements in a non-decreasing order

sorts elements non-recursively by breaking them into small segments, merging adjacent segments into larger sorted segments, then increasing the sizes of segments by factors of 2 and repeating the same process. best-case = worst-case = O(n log(n))

Parameters

  first | points to the first element
  last | points to 1-step past the last element
  n | the number of elements

{
// create a buffer large enough to store all elements
// dynamically allocated to comply with cpplint
char* buffer = new char[n * sizeof(*first)];
// buffer size can be optimized to largest power of 2 less than n
// elements divide the container into equally-sized segments whose
// length start at 1 and keeps increasing by factors of 2
for (size_t length(1); length < n; length <<= 1) {
// merge adjacent segments whose number is n / (length * 2)
Iterator left(first);
for (size_t counter(n / (length << 1)); counter; --counter) {
Iterator right(left + length), end(right + length);
merge(left, right, end, buffer);
left = end;
}
// if the number of remaining elements (n * 2 % length) is longer
// than a segment, merge the remaining elements
if ((n & ((length << 1) - 1)) > length)
merge(left, left + length, last, buffer);
}
delete[] buffer;
}
std::end
T end(T... args)
merge
void merge(int *arr, int l, int m, int r)
Definition: merge_sort.cpp:33
std::left
T left(T... args)
Here is the call graph for this function:

## non_recursive_merge_sort() [3/3]

template<class Iterator >

void sorting::non_recursive_merge_sort | ( | const Iterator | first,
|  | const size_t | n
| ) |  |

bottom-up merge sort which sorts elements in a non-decreasing order

Parameters

  first | points to the first element
  n | the number of elements

{
non_recursive_merge_sort(first, first + n, n);
}
Here is the call graph for this function:

## partition()

int sorting::partition | ( | int | arr[],
|  | int | low,
|  | int | high
| ) |  |

This function takes last element as pivot, places the pivot element at its correct position in sorted array, and places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot

{
int pivot = arr[high]; // taking the last element as pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j < high; j++) {
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot) {
i++; // increment index of smaller element
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}

## pigeonSort()

template<std::size_t N>

std::array<int, N> sorting::pigeonSort | ( | std::array< int, N > | arr | ) |

Pigeonhole sorting of array of size n The function will sort the array through Pigeonhole algorithm and print

Parameters

  arr | unsorted array of elements

Returns
  sorted array of elements

{
// Finding min and max*
auto min = std::min_element(std::begin(arr), std::end(arr));
auto max = std::max_element(std::begin(arr), std::end(arr));
// Range refers to the number of holes required
int range = *max - *min + 1;
int *hole = new int[range]();
// Copying all array values to pigeonhole
for (int i = 0; i < N; i++) {
hole[arr[i] - *min] = arr[i];
}
// Deleting elements from list and storing to original array
int count = 0;
for (int i = 0; i < range; i++) {
while (hole[i] != '\0') {
arr[count] = hole[i];
hole[i] = {};
count++;
}
}
delete[] hole;
return arr;
}
std::begin
T begin(T... args)
std::max_element
T max_element(T... args)
std::max
T max(T... args)
std::min_element
T min_element(T... args)
std::min
T min(T... args)
Here is the call graph for this function:

## quickSort()

void sorting::quickSort | ( | int | arr[],
|  | int | low,
|  | int | high
| ) |  |

The main function that implements QuickSort arr[] --> Array to be sorted, low --> Starting index, high --> Ending index

{
if (low < high) {
int p = partition(arr, low, high);
quickSort(arr, low, p - 1);
quickSort(arr, p + 1, high);
}
}
sorting::quickSort
void quickSort(int arr[], int low, int high)
Definition: quick_sort.cpp:63
sorting::partition
int partition(int arr[], int low, int high)
Definition: quick_sort.cpp:37
Here is the call graph for this function:

## quicksort() [1/2]

template<typename T >

void sorting::quicksort | ( | std::vector< T > * | arr,
|  | int32_t | low,
|  | int32_t | high
| ) |  |

3-way partition based quick sort. This function accepts array pointer and modified the input array.

Template Parameters

  T | type of data in the vector array

Parameters

  [in,out] | arr | vector array to sort
  [in] | low | lower limit of window to partition
  [in] | high | upper limit of window to partition

{
if (low >= high) { // 1 or 0 elements
return;
}
int32_t i = 0, j = 0;
// i and j are passed as reference
partition3(arr, low, high, &i, &j);
// Recur two halves
quicksort(arr, low, i);
quicksort(arr, j, high);
}
sorting::quicksort
std::vector< T > quicksort(std::vector< T > arr, int32_t low, int32_t high)
Definition: quick_sort_3.cpp:119

## quicksort() [2/2]

template<typename T >

std::vector<T> sorting::quicksort | ( | std::vector< T > | arr,
|  | int32_t | low,
|  | int32_t | high
| ) |  |

3-way partition based quick sort. This function accepts array by value and creates a copy of it. The array copy gets sorted and returned by the function.

Template Parameters

  T | type of data in the vector array

Parameters

  [in] | arr | vector array to sort
  [in] | low | lower limit of window to partition
  [in] | high | upper limit of window to partition

Returns
  sorted array vector

{
if (low >= high) { // 1 or 0 elements
return arr;
}
int32_t i = 0, j = 0;
// i and j are passed as reference
partition3(&arr, low, high, &i, &j);
// Recur two halves
quicksort(&arr, low, i);
quicksort(&arr, j, high);
return arr;
}
Here is the call graph for this function:

## randomized_bogosort()

template<typename T , size_t N>

std::array<T, N> sorting::randomized_bogosort | ( | std::array< T, N > | arr | ) |

Implement randomized Bogosort algorithm and sort the elements of a given array.

Template Parameters

  T | typename of the array
  N | length of array

Parameters

  arr | array to sort

Returns
  new array with elements sorted from a given array

{
// Untill array is not sorted
while (!std::is_sorted(arr.begin(), arr.end())) {
std::random_shuffle(arr.begin(), arr.end());// Shuffle the array
}
return arr;
}
std::is_sorted
T is_sorted(T... args)
std::random_shuffle
T random_shuffle(T... args)
Here is the call graph for this function:

## shell_sort() [1/3]

template<typename T >

void sorting::shell_sort | ( | std::vector< T > * | arr | ) |

function overload - when input array is of type std::vector, simply send the data content and the data length to the above function.

{
shell_sort(arr->data(), arr->size());
}
std::vector::data
T data(T... args)
sorting::shell_sort
void shell_sort(std::vector< T > *arr)
Definition: shell_sort2.cpp:75
Here is the call graph for this function:

## shell_sort() [2/3]

template<typename T >

void sorting::shell_sort | ( | T * | arr,
|  | size_t | LEN
| ) |  |

Optimized algorithm - takes half the time by utilizing Mar

{
const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
const unsigned int gap_len = 8;
size_t i, j, g;
for (g = 0; g < gap_len; g++) {
unsigned int gap = gaps[g];
for (i = gap; i < LEN; i++) {
T tmp = arr[i];
for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = tmp;
}
}
}

## shell_sort() [3/3]

template<typename T , size_t N>

void sorting::shell_sort | ( | T(&) | arr[N] | ) |

function overload - when input array is of a known length array type

{
shell_sort(arr, N);
}
Here is the call graph for this function:

## shuffle()

template<typename T , size_t N>

std::array<T, N> sorting::shuffle | ( | std::array< T, N > | arr | ) |

Function to shuffle the elements of an array. (for reference)

Template Parameters

  T | typename of the array
  N | length of array

Parameters

  arr | array to shuffle

Returns
  new array with elements shuffled from a given array

{
for (int i = 0; i < N; i++) {
// Swaps i'th index with random index (less than array size)
std::swap(arr[i], arr[std::rand() % N]);
}
return arr;
}
std::rand
T rand(T... args)
Here is the call graph for this function: