factored out internal_array_separate(..) to check if it has the errors or not
This commit is contained in:
parent
680936f50a
commit
fbea1e607c
@ -72,6 +72,41 @@ inline uint32_t internal_mid(uint32_t low, uint32_t high) {
|
||||
/** Sort using space-partitioning - does recursive re-pivoting */
|
||||
inline void spsort(uint32_t *t, int n, int m = 32);
|
||||
|
||||
struct lr {
|
||||
int left;
|
||||
int right;
|
||||
};
|
||||
|
||||
inline struct lr internal_array_separate(uint32_t *t, int n, uint32_t low, uint32_t mid, uint32_t high) {
|
||||
// Init left-right "write head" indices
|
||||
int left = 0;
|
||||
int right = n - 1;
|
||||
uint32_t current = t[left];
|
||||
bool current_is_left = true;
|
||||
|
||||
// Separate to the ends of the array (in-place)
|
||||
for(int i = 0; i < n; ++i) {
|
||||
if(current < mid) {
|
||||
const auto tmp = t[left];
|
||||
t[left++] = current;
|
||||
current = current_is_left ? t[left] : tmp;
|
||||
current_is_left = true;
|
||||
|
||||
} else {
|
||||
const auto tmp = t[right];
|
||||
t[right--] = current;
|
||||
current = tmp;
|
||||
current = !current_is_left ? t[right] : tmp;
|
||||
current_is_left = false;
|
||||
}
|
||||
}
|
||||
|
||||
struct lr eler;
|
||||
eler.left = left;
|
||||
eler.right = right;
|
||||
|
||||
return eler;
|
||||
}
|
||||
|
||||
// TODO: [spsort] In-place variant that looks like a quicksort-kind-of-thing:
|
||||
// - I realized that in-placing make this similar to quicksort actually
|
||||
@ -98,28 +133,9 @@ inline void internal_spsort(uint32_t *t, int n, int m, uint32_t low, uint32_t mi
|
||||
binsertion_sort(t, n);
|
||||
}
|
||||
|
||||
// Init left-right "write head" indices
|
||||
int left = 0;
|
||||
int right = n - 1;
|
||||
uint32_t current = t[left];
|
||||
bool current_is_left = true;
|
||||
|
||||
// Separate to the ends of the array (in-place)
|
||||
for(int i = 0; i < n; ++i) {
|
||||
if(current < mid) {
|
||||
const auto tmp = t[left];
|
||||
t[left++] = current;
|
||||
current = current_is_left ? t[left] : tmp;
|
||||
current_is_left = true;
|
||||
|
||||
} else {
|
||||
const auto tmp = t[right];
|
||||
t[right--] = current;
|
||||
current = tmp;
|
||||
current = !current_is_left ? t[right] : tmp;
|
||||
current_is_left = false;
|
||||
}
|
||||
}
|
||||
struct lr eler = internal_array_separate(t, n, low, mid, high);
|
||||
auto left = eler.left;
|
||||
auto right = eler.right;
|
||||
|
||||
auto lefts = left;
|
||||
auto rights = right - left;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user