1
回答
我的归并排序法 有什么问题吗 ,我不知道,无法运行成功
【腾讯云】学生服务器套餐10元/月 >>>   
#include <iostream>

using namespace std;

void merge_(int array_[], int p, int q, int r);
void merge_sort(int array_[], int p, int r);

int main()
{
   cout << "this is a merge sort:" << endl;
   int a[10] = {5, 2, 1, 7, 3, 4, 9, 8, 0, 6};
   for(int i = 1; i <= 10; i ++)
   {
        cout << a[i] << " ";
   }

   cout << endl << endl;
   merge_sort(a, 1, 10);

    return 0;


}



void merge_(int array_[], int p, int q, int r)
{
    int n1, n2;
    n1 = q - p + 1;
    n2 = r - q;


    int left_array[n1];
    int right_array[n2];

    for (int i = 1; i <= n1; i ++)
    {

        left_array[i] = array_[p + i -1];
    }
    for (int j = 1; j <= n2; j ++)
    {

        right_array[j] = array_[q + j];
    }

    int i= 1;
    int j = 1;

    for (int k = p; k <= r; k ++)
    {

        if (left_array[i] <= right_array[j])
        {

            array_[k] = left_array[i];
            i ++;
        }else{
            array_[k]  = right_array[j];
            j ++;
        }
    }


    for(int i = 1; i <= 10; i++)
       {
           cout << array_[i] << " .";
       }

    cout << endl << endl;
}


void merge_sort(int array_[], int p, int r)
{

    if (p < r)
    {

        int q;
        q  = (p + r)/2;
        merge_sort(array_, p, q);
        merge_sort(array_, q + 1, r);
        merge_(array_, p, q, r);
    }

}



<无标签>
举报
陈诒亮
发帖于4年前 1回/67阅
顶部