4void librarySort(
int *index,
int n) {
5 int lib_size, index_pos,
9 bool target_lib, *numbered;
11 for (
int i = 0; i < 2; i++) library[i] =
new int[n];
13 gaps =
new int[n + 1];
14 numbered =
new bool[n + 1];
19 library[target_lib][0] = index[0];
21 while (index_pos < n) {
23 int insert = std::distance(
25 std::lower_bound(library[target_lib],
26 library[target_lib] + lib_size, index[index_pos]));
30 if (numbered[
insert] ==
true) {
31 int prov_size = 0, next_target_lib = !target_lib;
35 for (
int i = 0; i <= n; i++) {
36 if (numbered[i] ==
true) {
37 library[next_target_lib][prov_size] = gaps[i];
43 library[next_target_lib][prov_size] =
44 library[target_lib][i];
49 target_lib = next_target_lib;
50 lib_size = prov_size - 1;
53 gaps[
insert] = index[index_pos];
58 int index_pos_for_output = 0;
59 for (
int i = 0; index_pos_for_output < n; i++) {
60 if (numbered[i] ==
true) {
62 index[index_pos_for_output] = gaps[i];
63 index_pos_for_output++;
68 index[index_pos_for_output] = library[target_lib][i];
69 index_pos_for_output++;
74 for (
int i = 0; i < 2; ++i) {
81 int index_ex[] = {-6, 5, 9, 1, 9, 1, 0, 1, -8, 4, -12};
82 int n_ex =
sizeof(index_ex) /
sizeof(index_ex[0]);
84 librarySort(index_ex, n_ex);
85 std::cout <<
"sorted array :" << std::endl;
86 for (
int i = 0; i < n_ex; i++) std::cout << index_ex[i] <<
" ";
87 std::cout << std::endl;
node * insert(node *root, int item)
inserts a new element into AVL tree