Sorting algorithms.
More...
|
| 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...
|
| |
| int | partition (int arr[], int low, int high) |
| |
| void | quickSort (int arr[], int low, int 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) |
| |
◆ insertionSort() [1/2]
Insertion Sort Function
- Template Parameters
-
- Parameters
-
| [in,out] | arr | pointer to array to be sorted |
78 size_t n = arr->size();
80 for (
size_t i = 1; i < n; i++) {
83 while (j >= 0 && temp < arr[0][j]) {
84 arr[0][j + 1] = arr[0][j];
◆ insertionSort() [2/2]
template<typename T >
| void sorting::insertionSort |
( |
T * |
arr, |
|
|
int |
n |
|
) |
| |
Insertion Sort Function.
- Template Parameters
-
- Parameters
-
| [in,out] | arr | Array to be sorted |
| n | Size of Array |
60 for (
int i = 1; i < n; i++) {
63 while (j >= 0 && temp < arr[j]) {
◆ 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 |
59 auto p(
reinterpret_cast<std::remove_reference_t<decltype(*l)
>*>(b)), c(p);
61 for (Iterator t(l); r != t; ++t) *p++ =
std::move(*t);
64 while (e != r && c != p) *l++ =
std::move(*r < *c ? *r++ : *c++);
◆ 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() [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 |
29 char* buffer =
new char[n *
sizeof(*first)];
33 for (
size_t length(1); length < n; length <<= 1) {
36 for (
size_t counter(n / (length << 1)); counter; --counter) {
37 Iterator
right(left + length),
end(right + length);
38 merge(left, right, end, buffer);
43 if ((n & ((length << 1) - 1)) > length)
44 merge(left, left + length, last, buffer);
◆ 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 |
◆ 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
38 int pivot = arr[high];
41 for (
int j = low; j < high; j++) {
44 if (arr[j] <= pivot) {
51 int temp = arr[i + 1];
52 arr[i + 1] = arr[high];
◆ 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
◆ shell_sort() [1/3]
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() [2/3]
template<typename T >
| void sorting::shell_sort |
( |
T * |
arr, |
|
|
size_t |
LEN |
|
) |
| |
Optimized algorithm - takes half the time by utilizing Mar
46 const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
47 const unsigned int gap_len = 8;
50 for (g = 0; g < gap_len; g++) {
51 unsigned int gap = gaps[g];
52 for (i = gap; i < LEN; i++) {
55 for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap) {
56 arr[j] = arr[j - gap];
◆ 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