C++ C++;:分拣测试速度-获得0.00';s

C++ C++;:分拣测试速度-获得0.00';s,c++,arrays,sorting,C++,Arrays,Sorting,我想测试三种算法的速度这是我的全部代码: #include<iostream> #include <stdio.h> #include <math.h> #include <vector> #include <algorithm> #include <time.h> void bubbleSort(int arr[]) { int length = sizeof(arr)/sizeof(arr[0]);

我想测试三种算法的速度这是我的全部代码:

#include<iostream>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
#include <time.h>

void bubbleSort(int arr[])
{
    int length = sizeof(arr)/sizeof(arr[0]);

    int i, j;
    for(i=(length-1); i >= 0; i--)
    {

        for(j =1; j<=i; j++)
        {
            if (arr[j-1]>arr[j])
            {
                int temp = arr[j-1];
                arr[j-1]=arr[j];
                arr[j]=temp;
            }
        }
    }
}

void selectionSort(int arr1[])
{
    int length = sizeof(arr1)/sizeof(arr1[0]);

    int i;
    int j;
    for(i= 0; i<length-1; i++)
    {
        int min = j;
        for(j = i+1;j<length;j++)
        {
            if (arr1[j]<arr1[min])
            {
                min = j;
            }
        }
        int temp =arr1[i];
        arr1[i]=arr1[min];
        arr1[min]= temp;
    }
}

void insertionSort(int arr2[])
{
    int length = sizeof(arr2)/sizeof(arr2[0]);

    for(int i=1;i<length; i++)
    {
        int index = arr2[i];
        int j =i;
        while(j>0&&arr2[j-1]>index)
        {
            arr2[j]= arr2[j-1];
            j--;
        }
        arr2[j]= index;
    }
}

using namespace std;

main()
{
    char t = 'f';
    char *t1;
    char **t2;

    int choice;

    vector<int> array;
    for (int i = 1; i <= 1000; i++) {
    printf("%d ", rand());
    array.push_back(rand());
    }
    int *arr = new int[array.size()];
    std::copy(array.begin(), array.end(), arr);

    int *arr1 = new int[array.size()];
    std::copy(array.begin(), array.end(), arr1);

    int *arr2 = new int[array.size()];
    std::copy(array.begin(), array.end(), arr2);

    int choose;

    do{
    std::cout << "\n\nWelcome to the algortihm tester!\n";
    std::cout << "What algorithm would you like to test?";
    std::cout << "\nChoose: \n1.Bubble Sort\n2.Selection Sort\n3.Insertion Sort\n";
    std::cout << "\nNumber of choice: ";
    scanf("%d", &choice);

    switch(choice)
    {
    case 1:
        {
        clock_t tStart = clock();
        bubbleSort(arr);
        printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
        }
        break;


        //bubble sort
    case 2:
        {
        clock_t tStart = clock();
        selectionSort(arr1);
        printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
        }
        break;
    case 3:
        {
        clock_t tStart = clock();
        insertionSort(arr2);
        printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
        }
        break;
    }

printf("\nDo you want to test the other algorithms?:<1-Yes/0-No>: ");
scanf("%d", &choose);
}while(choose != 0);
}
#包括
#包括
#包括
#包括
#包括
#包括
void bubbleSort(int arr[])
{
int length=sizeof(arr)/sizeof(arr[0]);
int i,j;
对于(i=(长度-1);i>=0;i--)
{
对于(j=1;jarr[j])
{
内部温度=arr[j-1];
arr[j-1]=arr[j];
arr[j]=温度;
}
}
}
}
无效选择排序(int arr1[]
{
int length=sizeof(arr1)/sizeof(arr1[0]);
int i;
int j;

对于(i=0;i您的
int arr[]
int*
相同。它不携带数组大小信息

您需要分别传递数组中的元素数


编辑:您可能得到了sizeof(指针)=8 sizeof(元素为int)=4,得到的长度为2。这根本不是您想要的。

您使用了一种不必要的复杂方法来创建数组

#include <iostream>
#include <ctime>
#include <iomanip> //setprecision
#define N 10000 

using namespace std;

void bubbleSort(int p[]);

int main() {

    int i, *p = new int[N]; //create int array named p with N size

    for (i = 0; i < N; i++) {
        p[i] = rand();
    }

    clock_t tStart = clock();
    bubbleSort(p);
    cout << "Time taken: " << setprecision(2) << (double)(clock() - tStart) / CLOCKS_PER_SEC << "s" <<endl;

    return 0;
}

void bubbleSort(int p[]) {
    int i, j, temp;
    for (i = 1; i<N; i++) {
        for (j = N - 1; j >= i; j--) {
            if (p[j - 1] > p[j]) {
                temp = p[j - 1];
                p[j - 1] = p[j];
                p[j] = temp;
            }
        }
    }
}
#包括
#包括
#包括//设置精度
#定义N 10000
使用名称空间std;
void bubbleSort(int p[]);
int main(){
int i,*p=new int[N];//创建名为p且大小为N的int数组
对于(i=0;i
#包括
#包括
#包括
#包括
#包括
#包括
void bubbleSort(整数arr[],整数大小)
{
int i,j;
对于(i=(大小-1);i>=0;i--)
{
对于(j=1;j arr[j])
{
内部温度=arr[j-1];
arr[j-1]=arr[j];
arr[j]=温度;
}
}
}
}
无效选择排序(整数arr1[],整数大小)
{
int i=0,j=0;

对于(i=0;iIt似乎您已经养成了非常糟糕的代码格式习惯。您应该立即修复这些习惯;正确缩进代码将更容易发现问题。谢谢Ken:)是的,我将尝试修复Itahh,就像我认为问题是在传递数组时一样。那么如何使用sizeof()获得长度呢很抱歉我是这样的。我是C++的新手。SRSLY。你说的“通过数组中的元素数”是什么意思?非常感谢你的一些东西,比如<代码> BuffelRoT(int *ARR,int NUMLATEMS)。
。如果使用原始数组,则可以按现有方式计算,但传递数组会自动将其转换为指向数组的指针。