C ++:测试速度排序 - 得到0.00

时间:2017-05-06 02:56:45

标签: 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]);

    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);
}

但是我继续得到0.00,我复制了三个随机变量数组,这样我就不会使用排序数组了。看来阵列的转移是个问题吗?我怀疑我的电脑可以在那一瞬间排序所有东西。一分钱你的想法?为什么我得到0.00?

3 个答案:

答案 0 :(得分:0)

您的int arr []int *相同。它不包含数据大小信息。

您需要单独传递数组中的元素数。

编辑:你可能得到sizeof(指针)= 8 sizeof(element为int)= 4,得到长度为2.不是你想要的。

答案 1 :(得分:0)

您正在使用一种不必要的复杂方式来创建数组。

#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;
            }
        }
    }
}

如果要在编码时动态更改数组大小,请使用#define N.它的测试简单得多。

如果要设置打印的位数,可以使用cout的setprecision。看看它,它真的很漂亮:http://www.cplusplus.com/reference/iomanip/setprecision/它也有助于保持一致。

如果你想要更好的randoms,你可以使用srand(time(NULL));它基本上使用系统时间作为种子来生成随机数。它并不完美,但它是一个进步。

答案 2 :(得分:0)

这就像魅力一样

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

void bubbleSort(int arr[], int size)
{

    int i, j;
    for (i = (size - 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 size)
{

    int i = 0, j = 0;

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

void insertionSort(int arr2[], int size)
{

    for (int i = 1; i<size; 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;

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

    int choice, choose, temp;
    const int  size = 10000;
    int  arr[size], arr1[size], arr2[size];

    for (int i = 0; i < size; i++) {
        temp = rand();
        arr[i] = temp;
        arr1[i] = temp;
        arr2[i] = temp;
    }

    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: ";
        cin >> choice;

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


        //bubble sort
        case 2:
        {
            clock_t tStart = clock();
            selectionSort(arr1, size);
            printf("Time taken: %.2fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
        }
        break;
        case 3:
        {
            clock_t tStart = clock();
            insertionSort(arr2, size);
            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>: ");
        cin >> choose;
    } while (choose != 0);
}

我认为插入是最快的