Skip to content

Commit

Permalink
Create Bubblesort.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Achlesha authored Oct 5, 2022
1 parent bc6612a commit 5011f11
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions c/Bubblesort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

#include <stdio.h>
#include<time.h>
#include<stdlib.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}


void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)

// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}


int main()
{
clock_t start,end;
int i, n, lower=-65536, upper=65536;

printf("Enter total no.of elements: ");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
a[i]=(rand()%(upper-lower+1))+lower;
}

start=clock();
bubbleSort(a, n);
end=clock();

double diff=end-start;
double time=(double)diff/(CLOCKS_PER_SEC);
printf("\n The time to execute this program is %lf",time);
return 0;
}

0 comments on commit 5011f11

Please sign in to comment.