简体   繁体   中英

C - Same number of number of comparisons for bubble sort and insertion sort

Recently I am doing a school task that ask us to write a program to count the number of comparisons for bubble sort and insertion sort.

However, when I execute the program, it returns 2 same value for bubble sort and insertion sort.

Example:

Sorted!
Number of comparison for bubble sort:    559150
Number of comparison for insertion sort: 559150

Here is my program:

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

I want to know where the problem is? and how can I solve it? Thanks a lot.

Number of comparisons - how many times program reach if statement. In case of bubble sort - if(x[j]>x[j+1]) . In case of insertion sort - (x[j]>temp) in while loop. So you are counting number of swaps not comparisons.

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

I don't see anything strange with this at all. They both have the same time complexity, which is O(n²). They also have O(n²) comparisons. Besides. If you analyze bubble sort and insertion sort (the variant without binary search, which is the one you're using) you'll find that they are VERY similar.

But when I look in your code, you do not count comparisons. You count swaps.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM