【问题标题】:Merge sort algorithm compilation error [HELP]合并排序算法编译错误 [求助]
【发布时间】:2015-03-31 04:28:32
【问题描述】:
#include <iostream>
#include <cstdlib>

using namespace std;

void print(int a[], int sz) 
{ 
    for (int i = 0; i < sz; i++) cout << a[i] << " "; 
    cout << endl; 
} 

void merge(int a[], const int low, const int mid, const int high) 
{
    int *temp = new int[high-low+1];

    int left = low;
    int right = mid+1;
    int current = 0;
    // Merges the two arrays into temp[]
    while(left <= mid && right <= high)
    {
        if(a[left] <= a[right])
        {
            temp[current] = a[left];
            left++;
        }
        else { // if right element is smaller that the left
        {
            // if right element is smaller that the left
            temp[current] = a[right];
            right++;
        }
        current++;
    }

    // Fills the array

    // The temp array has already been filled
    // Use the right side of array to fill temp
    if(left > mid)
    {
        for(int i=right; i <= high;i++)
        {
            temp[current] = a[i];
            current++;
        }
    }

    // Use the left side of array to fill temp
    else
    {
        for(int i=left; i <= mid; i++)
        {
            temp[current] = a[i];
            current++;
        }
    }

    //Fill into original array
    for(int i=0; i<=high-low;i++)
    {
        a[i+low] = temp[i];
    }
    delete[] temp;
}

void merge_sort(int a[], const int low, const int high)
{ // <-- Error #68
    if(low >= high) return;
    int mid = (low+high)/2;
    merge_sort(a, low, mid); //left half
    merge_sort(a, mid+1, high); //right half
    merge(a, low, mid, high); //merge them
}

int main()
{ //<-- Error #77
    int a[] = {26, 5, 33, 6, 19, 69, 99};

    int arraySize = sizeof(a)/sizeof(int);

    print(a, arraySize);
    merge_sort(a, 0, (arraySize-1));
    print(a, arraySize);

    return 0;
} //<-- Error #87

// 这段代码应该在c++中实现归并排序算法。 但是,当我编译我的代码时,它会遇到一堆错误。

mergesort.cpp:在函数'void merge(int*, int, int, int)'中:
mergesort.cpp:68:错误:在“{”标记之前不允许函数定义
mergesort.cpp:77:错误:在“{”标记之前不允许函数定义
mergesort.cpp:87:错误:输入结束时应为“}”

我已经指出错误在代码中的位置 谁能帮帮我?

【问题讨论】:

  • 你有else { // if right ...,然后在下一行有另一个{,你可能只想做else // if right...
  • 哇.. 菜鸟的错误。谢谢兄弟,解决了!

标签: c++ sorting merge compilation


【解决方案1】:
#include <iostream>
#include <cstdlib>

using namespace std;

void print(int a[], int sz) 
{ 
    for (int i = 0; i < sz; i++) cout << a[i] << " "; 
    cout << endl; 
} 

void merge(int a[], const int low, const int mid, const int high) 
{
    int *temp = new int[high-low+1];

    int left = low;
    int right = mid+1;
    int current = 0;
    // Merges the two arrays into temp[]
    while(left <= mid && right <= high)
    {
        if(a[left] <= a[right])
        {
            temp[current] = a[left];
            left++;
        }
        else 
        { 

            // if right element is smaller that the left
            temp[current] = a[right];
            right++;
        }
        current++;
    }

    // Fills the array

    // The temp array has already been filled
    // Use the right side of array to fill temp
    if(left > mid)
    {
        for(int i=right; i <= high;i++)
        {
            temp[current] = a[i];
            current++;
        }
    }

    // Use the left side of array to fill temp
    else
    {
        for(int i=left; i <= mid; i++)
        {
            temp[current] = a[i];
            current++;
        }
    }

    //Fill into original array
    for(int i=0; i<=high-low;i++)
    {
        a[i+low] = temp[i];
    }
    delete[] temp;
}

void merge_sort(int a[], const int low, const int high)
{
    if(low >= high) return;
    int mid = (low+high)/2;
    merge_sort(a, low, mid); //left half
    merge_sort(a, mid+1, high); //right half
    merge(a, low, mid, high); //merge them
}

main()
{
    int a[] = {26, 5, 33, 6, 19, 69, 99};

    int arraySize = sizeof(a)/sizeof(int);

    print(a, arraySize);
    merge_sort(a, 0, (arraySize-1));
    print(a, arraySize);
}

【讨论】:

    【解决方案2】:

    您收到此错误是因为您在第 27 行写了一个额外的 {,这会弄乱所有 { } 匹配。

    删除表示你将通过编译。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-13
      • 2021-09-12
      • 2018-02-12
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 2013-08-04
      相关资源
      最近更新 更多