data.hpp : Fixed wrong signatures for implace sorting and replaced custom swap in favour of std::swap
This commit is contained in:
110
data.hpp
110
data.hpp
@@ -106,8 +106,8 @@ namespace asp {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr T& max(const Array<T>& a) noexcept {
|
constexpr T max(const Array<T>& a) noexcept {
|
||||||
T& max_el = a[0];
|
T max_el = a[0];
|
||||||
for (size_t i = 1; i < a.length; ++i)
|
for (size_t i = 1; i < a.length; ++i)
|
||||||
if (a[i] > max_el)
|
if (a[i] > max_el)
|
||||||
max_el = a[i];
|
max_el = a[i];
|
||||||
@@ -138,26 +138,19 @@ namespace asp {
|
|||||||
|
|
||||||
inline Array<size_t> range(const size_t& n) noexcept {
|
inline Array<size_t> range(const size_t& n) noexcept {
|
||||||
Array<size_t> a(n);
|
Array<size_t> a(n);
|
||||||
return map(a, [](const size_t& i, const size_t&) -> const size_t& {
|
return map(a, [](const size_t i, const size_t) -> size_t {
|
||||||
return i;
|
return i;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr void swap(T* const a, T* const b) noexcept {
|
constexpr void bubble_sort(Array<T>& a) noexcept {
|
||||||
const T temp = *a;
|
|
||||||
*a = *b;
|
|
||||||
*b = temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
constexpr void bubble_sort(const Array<T>& a) noexcept {
|
|
||||||
size_t j = 0;
|
size_t j = 0;
|
||||||
for (size_t i = 0; i < a.length; ++i) {
|
for (size_t i = 0; i < a.length; ++i) {
|
||||||
bool swapped = false;
|
bool swapped = false;
|
||||||
for (j = i + 1; j < a.length; ++j)
|
for (j = i + 1; j < a.length; ++j)
|
||||||
if (a[i] > a[j]) {
|
if (a[i] > a[j]) {
|
||||||
swap(&a[i], &a[j]);
|
std::swap(a[i], a[j]);
|
||||||
swapped = true;
|
swapped = true;
|
||||||
}
|
}
|
||||||
if (!swapped)
|
if (!swapped)
|
||||||
@@ -166,15 +159,15 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Array<size_t> bubble_sort_arg(const Array<T>& a) noexcept {
|
inline Array<size_t> bubble_sort_arg(Array<T>& a) noexcept {
|
||||||
Array<size_t> indices = range(a.length);
|
Array<size_t> indices = range(a.length);
|
||||||
size_t j;
|
size_t j;
|
||||||
for (size_t i = 0; i < a.length; ++i) {
|
for (size_t i = 0; i < a.length; ++i) {
|
||||||
bool swapped = false;
|
bool swapped = false;
|
||||||
for (j = i + 1; j < a.length; ++j)
|
for (j = i + 1; j < a.length; ++j)
|
||||||
if (a[i] > a[j]) {
|
if (a[i] > a[j]) {
|
||||||
swap(&indices[i], &indices[j]);
|
std::swap(indices[i], indices[j]);
|
||||||
swap(&a[i], &a[j]);
|
std::swap(a[i], a[j]);
|
||||||
swapped = true;
|
swapped = true;
|
||||||
}
|
}
|
||||||
if (!swapped)
|
if (!swapped)
|
||||||
@@ -184,17 +177,17 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr size_t qs_partition(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
constexpr size_t qs_partition(Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
||||||
size_t i = l - 1;
|
size_t i = l - 1;
|
||||||
for (size_t j = l; j <= h; ++j)
|
for (size_t j = l; j <= h; ++j)
|
||||||
if (a[j] < a[h])
|
if (a[j] < a[h])
|
||||||
swap(&a[++i], &a[j]);
|
std::swap(a[++i], a[j]);
|
||||||
swap(&a[++i], &a[h]);
|
std::swap(a[++i], a[h]);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr void quicksort(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
constexpr void quicksort(Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
||||||
if (l >= h)
|
if (l >= h)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -205,12 +198,12 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr void quicksort(const Array<T>& a) noexcept {
|
constexpr void quicksort(Array<T>& a) noexcept {
|
||||||
quicksort(a, 0, a.length - 1);
|
quicksort(a, 0, a.length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr void quicksort_iter(const Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
constexpr void quicksort_iter(Array<T>& a, const size_t& l, const size_t& h) noexcept {
|
||||||
// Create an auxiliary stack
|
// Create an auxiliary stack
|
||||||
|
|
||||||
const size_t total = h - l + 1;
|
const size_t total = h - l + 1;
|
||||||
@@ -253,25 +246,25 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr void quicksort_iter(const Array<T>& a) noexcept {
|
constexpr void quicksort_iter(Array<T>& a) noexcept {
|
||||||
quicksort_iter(a, 0, a.length - 1);
|
quicksort_iter(a, 0, a.length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr size_t qs_arg_partition(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
|
constexpr size_t qs_arg_partition(Array<T>& a, Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
|
||||||
size_t i = l - 1;
|
size_t i = l - 1;
|
||||||
for (size_t j = l; j <= h; ++j)
|
for (size_t j = l; j <= h; ++j)
|
||||||
if (a[j] < a[h]){
|
if (a[j] < a[h]){
|
||||||
swap(&a[++i], &a[j]);
|
std::swap(a[++i], a[j]);
|
||||||
swap(&indices[i], &indices[j]);
|
std::swap(indices[i], indices[j]);
|
||||||
}
|
}
|
||||||
swap(&indices[++i], &indices[h]);
|
std::swap(indices[++i], indices[h]);
|
||||||
swap(&a[i], &a[h]);
|
std::swap(a[i], a[h]);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr void quicksort_arg(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
|
constexpr void quicksort_arg(Array<T>& a, Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
|
||||||
if (l >= h)
|
if (l >= h)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -282,19 +275,19 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Array<size_t> quicksort_arg(const Array<T>& other, const size_t& l, const size_t& h) noexcept {
|
inline Array<size_t> quicksort_arg(Array<T>& other, const size_t& l, const size_t& h) noexcept {
|
||||||
Array<size_t> indices = range(other.length);
|
Array<size_t> indices = range(other.length);
|
||||||
quicksort_arg(other, indices, l, h);
|
quicksort_arg(other, indices, l, h);
|
||||||
return indices;
|
return indices;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Array<size_t> quicksort_arg(const Array<T>& a) noexcept {
|
inline Array<size_t> quicksort_arg(Array<T>& a) noexcept {
|
||||||
return quicksort_arg(a, 0, a.length - 1);
|
return quicksort_arg(a, 0, a.length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr void quicksort_arg_iter(const Array<T>& a, const Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
|
inline void quicksort_arg_iter(Array<T>& a, Array<size_t>& indices, const size_t& l, const size_t& h) noexcept {
|
||||||
// Create an auxiliary stack
|
// Create an auxiliary stack
|
||||||
const size_t total = h - l + 1;
|
const size_t total = h - l + 1;
|
||||||
// push initial values of l and h to stack
|
// push initial values of l and h to stack
|
||||||
@@ -334,7 +327,7 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Array<size_t> quicksort_arg_iter(const Array<T>& a) noexcept {
|
inline Array<size_t> quicksort_arg_iter(Array<T>& a) noexcept {
|
||||||
Array<size_t> indices = range(a.length);
|
Array<size_t> indices = range(a.length);
|
||||||
quicksort_arg_iter(a, indices, 0, a.length - 1);
|
quicksort_arg_iter(a, indices, 0, a.length - 1);
|
||||||
return indices;
|
return indices;
|
||||||
@@ -363,7 +356,7 @@ namespace asp {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr void merge(const Array<T>& a, const size_t& l, const size_t& m, const size_t& r) noexcept {
|
constexpr void merge(Array<T>& a, const size_t& l, const size_t& m, const size_t& r) noexcept {
|
||||||
|
|
||||||
Array<T> left_arr(m - l + 1);
|
Array<T> left_arr(m - l + 1);
|
||||||
memcpy(&left_arr.data[0], &a[l], left_arr.length * sizeof(T));
|
memcpy(&left_arr.data[0], &a[l], left_arr.length * sizeof(T));
|
||||||
@@ -384,7 +377,7 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr void mergesort(const Array<T>& a, const size_t& l, const size_t& r) noexcept {
|
constexpr void mergesort(Array<T>& a, const size_t& l, const size_t& r) noexcept {
|
||||||
if (l >= r)
|
if (l >= r)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -395,15 +388,15 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr void mergesort(const Array<T>& a) noexcept {
|
constexpr void mergesort(Array<T>& a) noexcept {
|
||||||
mergesort(a, 0, a.length - 1);
|
mergesort(a, 0, a.length - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Array<size_t> mergesort_arg(const Array<T>& a, const size_t& l, const size_t& r) noexcept {
|
inline Array<size_t> mergesort_arg(Array<T>& a) noexcept {
|
||||||
|
|
||||||
Array<ArgVal<T>> temp_vals(a.length);
|
Array<ArgVal<T>> temp_vals(a.length);
|
||||||
map(temp_vals, [&a](const size_t& i, const ArgVal<T>&) -> const ArgVal<T> {
|
map(temp_vals, [&a](const size_t& i, const ArgVal<T>&) -> ArgVal<T> {
|
||||||
return ArgVal<T>(i, a[i]);
|
return ArgVal<T>(i, a[i]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -421,7 +414,7 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr void insertion_sort(const Array<T>& a) noexcept {
|
constexpr void insertion_sort(Array<T>& a) noexcept {
|
||||||
size_t j = 0;
|
size_t j = 0;
|
||||||
for(size_t i = 1; i < a.length; ++i) {
|
for(size_t i = 1; i < a.length; ++i) {
|
||||||
T key = a[i];
|
T key = a[i];
|
||||||
@@ -432,41 +425,18 @@ namespace asp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Array<size_t> insertion_argsort(const Array<T>& a) noexcept {
|
inline Array<size_t> insertion_argsort(Array<T>& a) noexcept {
|
||||||
Array<size_t> indices = range(a.length);
|
Array<ArgVal<T>> temp_vals(a.length);
|
||||||
size_t j = 0;
|
map(temp_vals, [&a](const size_t& i, const ArgVal<T>&) -> ArgVal<T> {
|
||||||
for(size_t i = 1; i < a.length; ++i){
|
return ArgVal<T>(i, a[i]);
|
||||||
for(j = i; j > 0 && a[indices[j - 1]] > a[indices[i]]; --j)
|
|
||||||
indices[j] = indices[j - 1];
|
|
||||||
indices[j] = i;
|
|
||||||
}
|
|
||||||
return indices;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
constexpr void counting_sort(const Array<T>& a) noexcept {
|
|
||||||
const size_t N = a.length;
|
|
||||||
const T M = max(a);
|
|
||||||
|
|
||||||
const T exp = 10;
|
|
||||||
const T d = 128;
|
|
||||||
|
|
||||||
Array<T> countArray(M);
|
|
||||||
memset(&countArray[0], 0, M * sizeof(T));
|
|
||||||
|
|
||||||
foreach(a, [&countArray](const size_t, const T val) -> void {
|
|
||||||
++countArray[val];
|
|
||||||
});
|
});
|
||||||
|
|
||||||
for (T i = 1; i <= M; ++i)
|
insertion_sort(temp_vals);
|
||||||
countArray[i] += countArray[i - 1];
|
|
||||||
|
|
||||||
Array<T> output(N);
|
Array<size_t> indices(a.length);
|
||||||
for (size_t i = N - 1; i > 0; --i){
|
return map(indices, [&temp_vals](const size_t& i, const size_t&) -> size_t {
|
||||||
output[countArray[a[i]] - 1] = a[i];
|
return temp_vals[i].indice;
|
||||||
--countArray[a[i]];
|
});
|
||||||
}
|
|
||||||
memmove(&a[0], &output[0], a.length * sizeof(T));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
Reference in New Issue
Block a user