Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
945 B
1
Indexable
Never
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void bubble_sort(int a[], int n)
{
    int i,j,temp;
    long long int compare=0,swap=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-i-1;j++)
        {
            compare++;
            if(a[j]>a[j+1])
            {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
                swap++;
            }
        }
    }
    printf("Swap Number=%lld \nComparison number= %lld\n",swap,compare);
}

int main()
{
    int len, i;
    clock_t start_time, end_time;
    double bubble_time;

    printf("Enter Array Length: ");
    scanf("%d", &len);
    int arr[len];
    srand(time(0));
    for(i=0;i<len;i++)
    {
        arr[i]=rand()%100+1;
    }

    start_time = clock();
    bubble_sort(arr,len);
    end_time = clock();

    bubble_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;

    printf("Time for bubble sort: %lf\n",bubble_time);

}
Leave a Comment