Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-冒泡排序和插入排序的相同比较次数_C_Sorting - Fatal编程技术网

C-冒泡排序和插入排序的相同比较次数

C-冒泡排序和插入排序的相同比较次数,c,sorting,C,Sorting,最近,我正在做一项学校任务,要求我们编写一个程序来计算冒泡排序和插入排序的比较次数 但是,当我执行程序时,它返回2个相同的值,用于冒泡排序和插入排序 例如: Sorted! Number of comparison for bubble sort: 559150 Number of comparison for insertion sort: 559150 这是我的节目: #include<stdio.h> #include<time.h> #define SIZ

最近,我正在做一项学校任务,要求我们编写一个程序来计算冒泡排序和插入排序的比较次数

但是,当我执行程序时,它返回2个相同的值,用于冒泡排序和插入排序

例如:

Sorted!
Number of comparison for bubble sort:    559150
Number of comparison for insertion sort: 559150
这是我的节目:

#include<stdio.h>
#include<time.h>
#define SIZE 1500

void swap(int *a,int *b)                 //swap function
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

int bubblesort(int x[])                  //Bubble sort 
{
    int i,j;
    int count = 0;
    for(i=0;i<SIZE-1;i++)
    {
        for(j=0;j<SIZE-i-1;j++)
        {
            if(x[j]>x[j+1])
            {
                swap(&x[j],&x[j+1]);
                count = count + 1;      //count comparison
            }
        }
    }
    return count;
}

int insertionsort(int x[])             //Insertion sort
{
    int i,j,temp;
    int count = 0;
    for(i=1;i<SIZE;i++)
    {
        temp = x[i];
        j = i-1;
        while((j>=0)&&(x[j]>temp))
        {
            count = count + 1;          //count comparison
            x[j+1] = x[j];
            j--;
        }
        x[j+1] = temp;
    }
    return count;
}

int main()
{
    int i;
    int b_array[SIZE];
    int i_array[SIZE];
    int b_count = 0;
    int i_count = 0;

    srand(time(NULL));

    for(i=0;i<SIZE;i++)             //Generate random number and assign it the the array
    {
        b_array[i] = rand()%SIZE;
        i_array[i] = b_array[i];
    }

    b_count = bubblesort(b_array);
    i_count = insertionsort(i_array);

    printf("Sorted!\n");
    printf("Number of comparison for bubble sort:    %d\n",b_count);
    printf("Number of comparison for insertion sort: %d",i_count);

    return 0;
}
#包括
#包括
#定义尺寸1500
无效交换(int*a,int*b)//交换函数
{
内部温度;
温度=*a;
*a=*b;
*b=温度;
}
int-bubblesort(int x[])//冒泡排序
{
int i,j;
整数计数=0;
对于(i=0;itemp))
{
count=count+1;//计数比较
x[j+1]=x[j];
j--;
}
x[j+1]=温度;
}
返回计数;
}
int main()
{
int i;
int b_数组[大小];
int i_数组[大小];
int b_计数=0;
int i_计数=0;
srand(时间(空));

对于(i=0;i我一点也不觉得奇怪。它们都有相同的时间复杂度,即O(n²)。它们也有O(n²)比较。此外,如果你分析冒泡排序和插入排序(没有二进制搜索的变体,你正在使用的),你会发现它们非常相似

但当我查看代码时,您不计算比较,而是计算交换

    for(j=0;j<SIZE-i-1;j++) {
        // Move it outside the comparison
        count = count + 1;      //count comparison                                                         
        if(x[j]>x[j+1]) {
            swap(&x[j],&x[j+1]);
        }
    }
(j=0;jx[j+1]){ 互换(&x[j]、&x[j+1]); } }
我一点也不觉得奇怪。它们都有相同的时间复杂度,即O(n²)。它们也有O(n²)比较。此外,如果你分析冒泡排序和插入排序(没有二进制搜索的变体,即你正在使用的变体),你会发现它们非常相似

但当我查看代码时,您不计算比较,而是计算交换

    for(j=0;j<SIZE-i-1;j++) {
        // Move it outside the comparison
        count = count + 1;      //count comparison                                                         
        if(x[j]>x[j+1]) {
            swap(&x[j],&x[j+1]);
        }
    }
(j=0;jx[j+1]){ 互换(&x[j]、&x[j+1]); } }
比较次数-程序达到
if
语句的次数。 在冒泡排序的情况下-
如果(x[j]>x[j+1])
。 在while循环中插入排序时-
(x[j]>temp)
。因此,您计算的是交换的数量,而不是比较

int bubblesort(int x[])
{
    int i,j;
    int count = 0;
    for(i=0;i<SIZE-1;i++)
    {
        for(j=0;j<SIZE-i-1;j++)
        {
            count++;      //comparison. Increment "count" each time program reach "if"
            if(x[j]>x[j+1])
            {
                swap(&x[j],&x[j+1]);
            }
        }
    }
    return count;
}
intbubblesort(intx[]
{
int i,j;
整数计数=0;

对于(i=0;i比较次数-程序达到
if
语句的次数。 在冒泡排序的情况下-
如果(x[j]>x[j+1])
。 在while循环中插入排序时-
(x[j]>temp)
。因此,您计算的是交换的数量,而不是比较

int bubblesort(int x[])
{
    int i,j;
    int count = 0;
    for(i=0;i<SIZE-1;i++)
    {
        for(j=0;j<SIZE-i-1;j++)
        {
            count++;      //comparison. Increment "count" each time program reach "if"
            if(x[j]>x[j+1])
            {
                swap(&x[j],&x[j+1]);
            }
        }
    }
    return count;
}
intbubblesort(intx[]
{
int i,j;
整数计数=0;

对于(i=0;i为什么这是个问题?为什么这是个问题?实际上,在渐近情况下具有相同的时间复杂度与对某些特定输入进行相同数量的比较并不相关。我知道两种排序的时间复杂度是相同的。但我发现每次运行程序时都有完全相同数量的比较是很奇怪的实际上,在渐近情况下具有相同的时间复杂度与对某些特定输入进行相同数量的比较并不相关。我知道两种排序的时间复杂度是相同的。但我发现每次运行程序时都有完全相同数量的比较是很奇怪的