feat : added library sort (#710)

* added library_sort.cpp

* added library_sort.cpp

* format

* format

* modified the code

* feat : added library sort
This commit is contained in:
Perukii 2020-03-07 16:20:08 +09:00 committed by GitHub
parent e1fce9e2bf
commit 6655038105
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 157 additions and 0 deletions

67
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,67 @@
{
"files.associations": {
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"cfenv": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cuchar": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"fstream": "cpp",
"functional": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"numeric": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"scoped_allocator": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"thread": "cpp",
"type_traits": "cpp",
"tuple": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"valarray": "cpp",
"algorithm": "cpp"
}
}

90
sorting/library_sort.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <algorithm>
#include <iostream>
void librarySort(int *index, int n) {
int lib_size, index_pos,
*gaps, // gaps
*library[2]; // libraries
bool target_lib, *numbered;
for (int i = 0; i < 2; i++)
library[i] = new int[n];
gaps = new int[n + 1];
numbered = new bool[n + 1];
lib_size = 1;
index_pos = 1;
target_lib = 0;
library[target_lib][0] = index[0];
while (index_pos < n) {
// binary search
int insert = std::distance(
library[target_lib],
std::lower_bound(library[target_lib],
library[target_lib] + lib_size, index[index_pos]));
// if there is no gap to insert a new index ...
if (numbered[insert] == true) {
int prov_size = 0, next_target_lib = !target_lib;
// update library and clear gaps
for (int i = 0; i <= n; i++) {
if (numbered[i] == true) {
library[next_target_lib][prov_size] = gaps[i];
prov_size++;
numbered[i] = false;
}
if (i <= lib_size) {
library[next_target_lib][prov_size] =
library[target_lib][i];
prov_size++;
}
}
target_lib = next_target_lib;
lib_size = prov_size - 1;
} else {
numbered[insert] = true;
gaps[insert] = index[index_pos];
index_pos++;
}
}
int index_pos_for_output = 0;
for (int i = 0; index_pos_for_output < n; i++) {
if (numbered[i] == true) {
// std::cout << gaps[i] << std::endl;
index[index_pos_for_output] = gaps[i];
index_pos_for_output++;
}
if (i < lib_size) {
// std::cout << library[target_lib][i] << std::endl;
index[index_pos_for_output] = library[target_lib][i];
index_pos_for_output++;
}
}
}
int main() {
// ---example--
int index_ex[] = {-6, 5, 9, 1, 9, 1, 0, 1, -8, 4, -12};
int n_ex = sizeof(index_ex) / sizeof(index_ex[0]);
librarySort(index_ex, n_ex);
std::cout << "sorted array :" << std::endl;
for (int i = 0; i < n_ex; i++)
std::cout << index_ex[i] << " ";
std::cout << std::endl;
/* --output--
sorted array :
-12 -8 -6 0 1 1 1 4 5 9 9
*/
}