Program for bubble sort in C++.

#include<iostream>
using namespace std;
#include<stdio.h>
#include<time.h>
#include<stdlib.h>

void bubblesort(long int*);

void main()
{
long int i=0,c;
long int a[30000];
FILE *fp;
fp=fopen("input3.txt","r");
while(fscanf(fp,"%ld",&c)!=EOF)
{
a[i]=c;
//    cout<<a[i];
i++;
}
fclose(fp);

bubblesort(a);

fp=fopen("output3.txt","w");
for(i=0;i<30000;i++)
{
fprintf(fp,"%ld ",a[i]);
}
fclose(fp);
}

void bubblesort(long int*h)
{
clock_t start, finish;
double  duration;

long int temp;
start = clock();

for(int i=30000;i>1;i--)
{
for(int j=0;j<i-1;j++)
{
if(h[j]>h[j+1])
{
temp=h[j];
h[j]=h[j+1];
h[j+1]=temp;
}
}
}
finish = clock();
duration = (float)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.3f seconds\n", duration );

}
Advertisements

6 thoughts on “Program for bubble sort in C++.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s