C++ 合并排序C++;分类错误

C++ 合并排序C++;分类错误,c++,arrays,sorting,merge,C++,Arrays,Sorting,Merge,我正在写一个程序,将做5个不同的排序功能,并比较它们之间的时间。我将输出每个数组的第1000个元素,以查看其排序是否正确,除合并排序外的所有其他排序都生成了正确的数字。合并排序已关闭,但处于关闭状态,在获得正确答案的一两个元素内(第1000个元素输出25011而不是25034)。以下是我的合并排序代码: //Zach Heesaker. CS3. Project 4 #include <iostream> #include <fstream> #include <s

我正在写一个程序,将做5个不同的排序功能,并比较它们之间的时间。我将输出每个数组的第1000个元素,以查看其排序是否正确,除合并排序外的所有其他排序都生成了正确的数字。合并排序已关闭,但处于关闭状态,在获得正确答案的一两个元素内(第1000个元素输出25011而不是25034)。以下是我的合并排序代码:

//Zach Heesaker. CS3. Project 4
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include <ctime>
#include <cstdio>
#include <time.h>
#include <stdint.h>
#include<list>
#include<cmath>

using namespace std;

const int n = 10000;
int numswaps, numcompares;

void swap(int& a, int& b);
void print(int arr[], int n, ofstream& outf, double time);
void read(int arr[], int size);
void mergepass(int arr[], int y[], int& n, int& L);
void mergeSort(int arr[], int n);
void merge(int arr[], int y[], int L, int m, int n);

int main()
{
    int numsorts = 5;
    string whichsort;
    ofstream outf("output.ot");
    int arr[n + 1];
    clock_t start, end;
    double time;

    outf << "Sort Name: " << setw(5) << " " << "1000th Element: " << setw(1) << " " << "Number of Moves: " << setw(1) << " " << "Time taken: " << endl;
    read(arr, n);
    numcompares = 0;
    numswaps = 0;
    start = clock();
    mergeSort(arr, n);
    end = clock();
    time = double(end - start) / double(CLOCKS_PER_SEC);
}

void mergeSort(int arr[], int size)
{
    int L = 1;
    int y[n + 1];
    while (L < n)
    {
        mergepass(arr, y, size, L);
        L = 2 * L;
        mergepass(y, arr, size, L);
        L = 2 * L;
    }
}

void merge(int arr[], int y[], int L, int m, int n)
{
    int i, j, k, t;
    i = L;
    k = L;
    j = m + 1;
    while ((i <= m) && (j <= n))
    {
        numcompares++;
        if (arr[i] <= arr[j])
        {
            numswaps++;
            y[k++] = arr[i++];
        }
        else
        {
            numswaps++;
            y[k++] = arr[j++];
        }
    }
    if (i > m)
    {
        for (t = j; t <= n; t++)
        {
            numswaps++;
            y[k + t - j] = arr[t];
        }
    }
    else
    {
        for (t = i; t <= m; t++)
        {
            numswaps++;
            y[k + t - i] = arr[t];
        }
    }
}

void mergepass(int arr[], int y[], int& n, int& L)
{
    int i, t;
    i = 1;
    while (i <= n - 2 * L + 1)
    {
        merge(arr, y, i, i + L - 1, i + 2 * L - 1);
        i = i + 2 * L;
    }
    if ((i + L - 1) < n)
    {
        merge(arr, y, i, i + L - 1, n);
    }
    else
    {
        for (t = i; t <= n; t++)
        {
            numswaps++;
            y[t] = arr[t];
        }
    }
}

void swap(int& a, int& b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
    numswaps += 3;
}

void print(int arr[], int n, ofstream& outf, double time)
{
    outf << left  << setw(6) << " " << left  << arr[1000] << setw(12) << " " << left <<  numswaps << setw(10) << " " << left << "\t" << time << endl;
}

void read(int arr[], int size)
{
    ifstream ifs("input.txt");
    int i = 0;
    while (!ifs.eof())
    {
        ifs >> arr[i];
        i++;
    }
}

int merge(int arr[], int left, int right)
{
    int pivot = arr[right];
    int k = (left - 1);
    for (int j = left; j <= right - 1; j++)
    {
        numcompares++;
        if (arr[j] < pivot)
        {
            k++;
            swap(arr[k], arr[j]);
        }
    }
    swap(arr[k + 1], arr[right]);
    return (k + 1);
}
//Zach Heesaker。CS3。项目4
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
常数n=10000;
int numswaps,numcompares;
无效掉期(内部和a、内部和b);
无效打印(int arr[],int n,流和流出,双倍时间);
无效读取(整数arr[],整数大小);
无效合并通行证(内部arr[],内部y[],内部和n,内部和L);
无效合并排序(int arr[],int n);
无效合并(int arr[],int y[],int L,int m,int n);
int main()
{
int numsorts=5;
短弦;
流量输出(“output.ot”);
int arr[n+1];
时钟开始、结束;
双倍时间;

对于这个程序,正如您编写的,它甚至不会编译。@yzt我添加了完整的源代码以澄清问题,顶层代码只是mergesort函数。您的代码非常混乱,使用更好的变量名而不是单个字母,
n
既是全局变量又是函数参数,将变量传递到函数中而不是使用如果你修复了所有你的bug会消失或变得更加明显的问题,我不会感到惊讶