A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/de/d7b/merge__insertion__sort_8cpp_source.html below:

TheAlgorithms/C++: sorting/merge_insertion_sort.cpp Source File

Go to the documentation of this file. 36template

<

typename

T,

size_t

N>

37static void InsertionSort

(std::array<T, N> *A,

size_t

start,

size_t

end) {

41 for

(i = start; i < end; i++) {

44 while

(j > start && temp < ptr[j - 1]) {

66template

<

typename

T,

size_t

N>

67static void merge

(std::array<T, N> *array,

size_t

min,

size_t

max,

size_t

mid) {

68 size_t

firstIndex = min;

69 size_t

secondIndex = mid + 1;

71 auto

ptr = array->data();

72

std::array<T, N + 1> tempArray{0};

75 for

(

size_t

index = min; index <= max; index++) {

77 if

(firstIndex <= mid &&

78

(secondIndex > max || ptr[firstIndex] <= ptr[secondIndex])) {

79

tempArray[index] = ptr[firstIndex];

82

tempArray[index] = ptr[secondIndex];

88

memcpy(ptr + min, tempArray.data() + min, (max - min) *

sizeof

(T));

106template

<

typename

T,

size_t

N>

107void mergeSort

(std::array<T, N> *array,

size_t

min,

size_t

max,

110 if

((max - min) <= threshold) {

114 size_t

mid = (max + min) >> 1;

121 merge

(array, min, max, mid);

133 constexpr size_t

size = 30;

134

std::array<int, size> array{0};

136 for

(

int

i = 0; i < size; i++) {

137

array[i] = std::rand() % 100 - 50;

138

std::cout << array[i] <<

" "

;

140

std::cout << std::endl;

146 for

(

int

i = 0; i < size; ++i) {

147

std::cout << array[i] <<

" "

;

149

std::cout << std::endl;

151

assert(std::is_sorted(std::begin(array), std::end(array)));

152

std::cout <<

"Test passed\n"

;

160

std::srand(std::time(

nullptr

));

static void InsertionSort(std::array< T, N > *A, size_t start, size_t end)

Insertion merge algorithm.

void mergeSort(std::array< T, N > *array, size_t min, size_t max, size_t threshold)

Final combined algorithm. Algorithm utilizes sorting::merge_insertion::InsertionSort if window length...

static void test()

Function to test code using random arrays.

int main()

Main function.

static void merge(std::array< T, N > *array, size_t min, size_t max, size_t mid)

Perform merge of data in a window.

Combined Intersion-Merge sorting algorithm.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4