Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 20th, 2010 | Syntax: None | Size: 0.92 KB | Hits: 76 | Expires: Never
Copy text to clipboard
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <time.h>
  4. #include <iomanip>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. void bubblesort(int a[], int n)
  9. {
  10.        
  11.         int i,j;
  12.         int tmp;
  13.         int change;
  14.        
  15.         for(i=0;i<n-1;i++)
  16.         {
  17.                 change=0;
  18.                 for(j=0;j<n-1-i;j++)
  19.                 if(a[j+1]<a[j])
  20.                 {
  21.                         tmp=a[j];
  22.                         a[j]=a[j+1];
  23.                         a[j+1]=tmp;
  24.                         change =1;
  25.                 }
  26.                 if(!change)break;
  27.         }
  28. }
  29. int _tmain(int argc, _TCHAR* argv[])
  30. {
  31.         ofstream pomiar;
  32.         pomiar.open ("pomiar babelkowe.txt");
  33.         clock_t tstart,tend;
  34.         int *tab;
  35.         int n=10000;
  36.         for(int l=0;l<10;l++)
  37.         {
  38.                 pomiar<<n<<endl;
  39.                 for(int k=0;k<20;k++)
  40.                 {
  41.                 tab=new int[n];
  42.                 for(int i=0;i<n;i++)
  43.                 {
  44.                         tab[i]=rand()%100000;
  45.                 }
  46.                 tstart=clock();
  47.                 bubblesort(tab,n);
  48.                 tend=clock();
  49.                 double t=(double)(tend-tstart)/CLOCKS_PER_SEC;
  50.                 pomiar<<fixed<<setprecision(4)<<t;
  51.                 pomiar<<endl;
  52.                 }
  53.                 n=n+10000;
  54.         }
  55.         pomiar.close();
  56.         delete []tab;
  57.         system("pause");
  58.         return 0;
  59. }