/**
     * @brief Merges two sorted lists.
     * @param[in] i_left Pointer to the head of left list.
     * @param[in] i_right Pointer to the head of right list.
     */
    Node * p_merge(Node * i_left, Node * i_right)
    {
        // left list is empty
        if (i_left == nullptr)
        {
            return i_right;
        }

        // right list is empty
        if (i_right == nullptr)
        {
            return i_left;
        }

        if (i_left->data < i_right->data)
        {
            // place at the top of list
            i_left->next = p_merge(i_left->next, i_right);
            // change previous of next pointer
            i_left->next->prev = i_left;
            i_left->prev = nullptr;
            return i_left;
        }
        else
        {
            // place at the top of list
            i_right->next = p_merge(i_left, i_right->next);
            // change previous of next pointer
            i_right->next->prev = i_right;
            i_right->prev = nullptr;
            return i_right;
        }
    }
示例#2
0
static void
p_sort(uint* base, uint* l, uint* h, uint* p_)
{
  if((h - l) <= MAXSZ) p_sort_(base, l, h);
  else
  {
    uint* m  = l  + (h - l) / 2;
    uint* m_ = p_ + (m - l);

#pragma omp task
    p_sort(base, l, m, p_);
    p_sort(base, m, h, m_);
#pragma omp taskwait
 
    p_merge(base, l, m, m, h, p_);
  }
}
    /**
     * @brief Sorts list using mergesort algorithms.
     * @param[in] i_head Pointer to the head of list.
     */
    Node * p_mergesort(Node * i_head)
    {
        // list is empty or has only one node
        if (i_head == nullptr || i_head->next == nullptr)
        {
            return i_head;
        }
        // split list into two halfs
        Node * half = p_split(i_head);

        // recursively sort two halfs
        i_head = p_mergesort(i_head);
        half = p_mergesort(half);

        // merge sorted lists
        return p_merge(i_head, half);
    }