简体   繁体   English

C ++中的mergesort

[英]mergesort in C++

#include "stdafx.h"
#include <iostream>

using namespace std;
using namespace System;

void mergesort(int a[], int p, int r);
void merge(int a[], int p, int q, int r);
int main(array<System::String ^> ^args)
{
    int result[2] = { 1, 0 };
    int *p;
    p = result;
    mergesort(p, 0,1);
    while (1);
    return 0;
}

void mergesort(int a[],int p, int r)
{
    if (p < r)
    {
        int q = (p + r) / 2;
        mergesort(a, p, q);
        cout << endl;
        mergesort(a, q + 1, r);
        cout << endl;
        merge(a,p,q,r);
        for (int i = 0; i < 2; i++)
        {
            cout << a[i] << " ";
        }
        cout << endl;
    }
}

void merge(int a[], int p, int q, int r)
{
    int left[100] = {};
    int right[100] = {};
    for (int i = 0; i < (q - p + 1); i++)
    {
        left[i] = *(a + p + i);
        left[i + 1] = 999;
    }
    for (int i = 0; i < (r - q); i++)
    {
        right[i] = *(a + q + 1 + i);
        right[i + 1] = 999;
    }
    for (int k = p; k < r+1; k++)
    {
        int i = 0;
        int j = 0;
        if (left[i] <= right[j])
        {
            a[k] = left[i++];
        }
        else
        {
            a[k] = right[j++];      **// it always goes into this route? why does not left[i]<=right[j] work?**
        }
    }
}

Here is my code about a basic merge sort. 这是我有关基本合并排序的代码。 I cannot enter the first IF statement in the merge function. 我无法在合并功能中输入第一个IF语句。 Why does if (left[i] <= right[j]) not work? 为什么if (left[i] <= right[j])不起作用? I have tried may time whatever the left[i] is. 我已经尝试过,可能left[i]时间left[i]都是这样。 The program just goes to the else statement. 该程序只是转到else语句。

for (int k = p; k < r+1; k++)
{
    int i = 0;
    int j = 0;
    if (left[i] <= right[j])
    {
        a[k] = left[i++];
    }
    else
    {
        a[k] = right[j++];      **// it always goes into this route? why does not left[i]<=right[j] work?**
    }
}

This does not do what you think it does. 这并没有按照您的想法做。 It will always compare left[0] with right[0] , because you declare i and j inside the for loop, so they will be reset to 0 on each iteration. 它将始终将left[0]right[0] ,因为您在for循环中声明了ij ,因此它们在每次迭代时都将重置为0。

To make them retain their values, declare them outside the loop: 为了使它们保留其值,请在循环外部声明它们:

int i = 0;
int j = 0;
for (int k = p; k < r+1; k++)
{
    if (left[i] <= right[j])
    {
        a[k] = left[i++];
    }
    else
    {
        a[k] = right[j++];      **// it always goes into this route? why does not left[i]<=right[j] work?**
    }
}

If your intent is to use variables i and j in the loop only then try to restrict the scope of i and j to the loop by doing following. 如果您的意图是仅在循环中使用变量i和j,请尝试执行以下操作以将i和j的范围限制为循环。

for (int k = p, i = 0, j = 0; k < r+1; k++)
{
    if (left[i] <= right[j])
    {
        a[k] = left[i++];
    }
    else
    {
        a[k] = right[j++];      
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM