This PPT gives fundamentals of working of bubble sort.
Click following link to DOWNLOAD ppt.
Following is the program for Bubble Sort Algorithm for GCC (Ubuntu)
File Name: BUBBLE.c
//**********************************************************
// Bubble Sorting Algorithm Implementation & Analysis.
//**********************************************************
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<unistd.h>
#define SIZE 25000
void sorting(void);
int rand_range(int, int);
long int x[SIZE];
int main()
{
int i;
//Best-Case(Ascending Order)
for(i=0;i<SIZE;i++)
x[i]=i;
printf(“\n**** Best Case ****\n”);
sorting();
//Average-Case(Random Order)
for(i=0;i<SIZE;i++)
x[i]=rand_range(1, SIZE);
printf(“\n\n**** Average Case ****\n”);
sorting();
//Worst-Case(Descending Order)
for(i=SIZE;i>=0;i–)
x[i]=((SIZE+1500000)-i);
printf(“\n\n**** Worst Case ****\n”);
sorting();
return 0;
}
int rand_range(int min_n, int max_n)
{
return rand() % (max_n – min_n + 1) + min_n;
}
void sorting()
{
int hold,i,j,pass;
int switched=1;
long int count=0;
clock_t start, end;
start = clock();
for(pass=0;pass<SIZE-1 && switched==1;pass++)
{
//outer loop controls the number of passes
switched=0; //initially no interchange
for(j=0;j<SIZE-pass-1;j++)
{
//inner loop for each individual pass
if(x[j]>x[j+1])
{
switched=1;
hold=x[j];
x[j]=x[j+1];
x[j+1]=hold;
count++;
sleep(0.1);
}
}
}
end = clock();
printf(“The time was: %f Sec\nNo.of Comparisons:%ld\n”, (end – start) / 1000000.0,count); // CLOCKS_PER_SEC
}
*************************************************************************************************************
Following are the commands to run this program:
gcc -o bubble BUBBLE.c
./bubble