簡體   English   中英

如何計算鏈接列表的合並排序代碼中的交換(反轉)和比較次數?

[英]How do I count the number of swaps (inversions) and comparisons in my merge sort code for my Linked List?

這是我的合並排序的代碼:這是合並 function

node* Merge(node* h1, node* h2, int &comp, int &swaps){ node *t1 = new node; node *t2 = new node; node *temp = new node;


// Return if the first list is empty.
if(h1 == NULL)
    return h2;

// Return if the Second list is empty.
if(h2 == NULL)
    return h1;

t1 = h1;

// A loop to traverse the second list, to merge the nodes to h1 in sorted way.
while (h2 != NULL)
{
    // Taking head node of second list as t2.
    t2 = h2;

    // Shifting second list head to the next.
    h2 = h2->next;
    t2->next = NULL;

    // If the data value is lesser than the head of first list add that node at the beginning.
comp++;
    if(h1->data > t2->data)
    {
        t2->next = h1;
        h1 = t2;
        t1 = h1;
        continue;
    }

    // Traverse the first list.
    flag:
    if(t1->next == NULL)
    {
        t1->next = t2;
        t1 = t1->next;
    }
    // Traverse first list until t2->data more than node's data.
    else if((t1->next)->data <= t2->data)
    {
        t1 = t1->next;
        goto flag;
    }
    else
    {
        // Insert the node as t2->data is lesser than the next node.
        temp = t1->next;
        t1->next = t2;
        t2->next = temp;
  
    }
}

// Return the head of new sorted list.
return h1;

}

這是合並排序 function,它調用合並 function。 我不知道我是否應該將比較和交換計數器放在合並或合並排序中,而且我不知道在 function 中將它們放在哪里。

void MergeSort(node **head, int &comp, int &swaps)

{

node *first = new node;
node *second = new node;
node *temp = new node;
first = *head;
temp = *head;




// Return if list have less than two nodes.
if(first == NULL || first->next == NULL)
{
    return;
}
else
{
    // Break the list into two half as first and second as head of list.
    while(first->next != NULL)
    {
        first = first->next;
        if(first->next != NULL)
        {
            temp = temp->next;
            first = first->next;
        }
    }
    second = temp->next;
    temp->next = NULL;
    first = *head;
}

// Implementing divide and conquer approach.
MergeSort(&first, comp, swaps);
MergeSort(&second, comp, swaps);

// Merge the two part of the list into a sorted one.      
*head = Merge(first, second, comp, swaps);

}

我不確定在哪里放置我的交換和比較計數器來計算合並排序進行的交換和比較的數量?

對於比較,您可以在每次進行比較時增加一個全局計數器。

反轉與交換不同,它是數組中數組 [i] > 數組 [j] 和 i < j 的出現次數。 例如:

int InversionCount(int array[], int n)
{
    int count = 0;
    for (int i = 0; i < n - 1; i++)
        for (int j = i + 1; j < n; j++)
            if (array[i] > array[j])
                count++;
    return count;
}

一個典型的合並步驟合並兩個已經排序的子數組,array[low to mid-1],array[mid to end-1]。 令 i = 左數組的索引(低到中 1)和 j = 右數組的索引(中到尾 1)。 如果在合並過程中遇到 array[i] > array[j] 的情況,那么 array[i 到 mid-1] 都將大於 array[j]。 看看您是否可以使用它來確定和求和合並排序的合並步驟部分中的反轉次數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM