Refactor sorting logic in PmergeMe class; remove placeholder comments and adjust insertion logic

This commit is contained in:
whaffman 2025-09-02 17:34:08 +02:00
parent e3d2a47a5e
commit 25e95e3ff6

View File

@ -86,8 +86,6 @@ std::ostream &PmergeMe::operator<<(std::ostream &os) const
void PmergeMe::sort()
{
// Implement the sorting algorithm using _data_vector and _data_deque
// This is a placeholder for the actual sorting logic
sortVector(0);
std::sort(_data_deque.begin(), _data_deque.end());
}
@ -103,7 +101,6 @@ void PmergeMe::sortVector(int level = 0)
std::vector<int> left = std::vector<int>(_data_vector.end() - odd, _data_vector.end());
_data_vector.erase(_data_vector.end() - odd, _data_vector.end());
for (size_t i = 0; i < _data_vector.size(); i += group_size)
@ -138,7 +135,7 @@ void PmergeMe::sortVector(int level = 0)
pend.insert(pend.end(), left.begin(), left.end());
int i = 0;
int sorted_in_main = 2;
int sorted_in_main = 3; // TODO ??
while (!pend.empty())
{
int jacob_index = Jacobstahl().get(i + 3) - Jacobstahl().get(i + 2);
@ -149,7 +146,7 @@ void PmergeMe::sortVector(int level = 0)
insertVector(main, pend, j, sorted_in_main, group_size);
std::cout << "insertVector(main, pend, " << j << ", " << sorted_in_main << ", " << group_size << ")" << std::endl;
}
sorted_in_main += start_index;
sorted_in_main += start_index + 1; // TODO ??
i++;
}
_data_vector = main;
@ -190,21 +187,15 @@ void PmergeMe::insertVector(std::vector<int> &main, std::vector<int> &pend, int
{
if (pend.empty() || start_index < 0 || start_index >= static_cast<int>(pend.size()))
return;
// // Find the group in pend starting at start_index
// if (start_index + group_size > static_cast<int>(pend.size()))
// group_size = pend.size() - start_index;
// if (group_size <= 0)
// return;
std::cout << main << " | " << pend << " | start_index: " << start_index << " | right: " << right << " | group_size: " << group_size << std::endl;
// Use the last element in the group for binary search
int value_to_insert = pend[(start_index + 1) * group_size - 1];
std::cout << "Value to insert: " << value_to_insert << std::endl;
// Binary search in main for insertion position
// Binary search comparing only the last values of each group in main
auto insert_pos = main.begin() + right;
auto insert_pos = main.begin() + right * group_size;
int left = 0;
// right = (main.size() + group_size - 1) / group_size - 1; // number of groups - 1
while (left <= right)
{
@ -217,14 +208,13 @@ void PmergeMe::insertVector(std::vector<int> &main, std::vector<int> &pend, int
else
{
right = mid - 1;
insert_pos = main.begin() + last_idx;
insert_pos = main.begin() + (mid * group_size); // Insert before the start of the found group //TODO ??
}
}
// Insert the whole group before insert_pos
main.insert(insert_pos, pend.begin() + start_index * group_size, pend.begin() + (start_index + 1) * group_size);
std::cout << "After insertion: " << main << std::endl;
// Erase the group from pend
pend.erase(pend.begin() + start_index * group_size, pend.begin() + (start_index + 1) * group_size);
}