Advertisement
lukusm

functions.cpp

Dec 2nd, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include "headers.h"
  2. #include "functions.h"
  3. using namespace std;
  4.  
  5. void randomNumGen(int size, int range, vector<double>& numTable) {
  6.     for(int i=1; i<=size; i++) {
  7.         double num = (rand()%(range*100)+1)/100;
  8.         numTable.push_back(num);
  9.     }
  10. }
  11.  
  12. void BubbleSort(vector<double> &Numbers){
  13.     double n=Numbers.size();
  14.     for (int i =0; i < n-2; i++)
  15.         for (int j = n-2; j > i; --j)
  16.             if (Numbers[j] < Numbers[j-1])
  17.                 swap (Numbers[j], Numbers[j-1]);
  18. }
  19.  
  20. void SelectionSort(vector<double> &Smallest){
  21.     int n=Smallest.size();
  22.     int smallestElement;
  23.     for (int j=1;j<n-2;j++){
  24.         smallestElement=j;
  25.         for (int i=j+1;i<n-1;i++){
  26.            if(Smallest[i]<Smallest[smallestElement])
  27.                  smallestElement=i;
  28.         }
  29.         swap(Smallest[j],Smallest[smallestElement]);
  30.     }
  31. }
  32.  
  33. void InsertionSort(vector<double> &data){
  34.     int z=data.size(),i;
  35.     double key;
  36.     for (int j=1;j<z;j++){
  37.         key=data[j];
  38.         //cout<<j<<"j"<<z<<"z"<<i<<"i"<<key<<"key"<<data[j]<<"data[j]"<<endl;
  39.         i=j-1;
  40.         while (i > 0 && data[i]>key){
  41.             data[i+1]=data[i];
  42.           i=i-1;
  43.         }
  44.         data[i+1]=key;
  45.     }  
  46. }
  47.  
  48. /*void writeResults(string file, vector<double> gTimes1, vector<double> sTimes1,
  49. vector<double> gTimes2, vector<double> sTimes2, vector<double> gTimes3, vector<double> sTimes3) {*/
  50. void writeResults(string file, vector<double> sTimes1, vector<double> sTimes2, vector<double> sTimes3) {
  51.     ofstream file;
  52.     file.open(file.c_str());
  53.     //file<<"|\t\tROZMIAR\t\t|\t\tCZAS TWORZENIA\t\t|\t\tCZAS BUBBLE_s\t\t|\t\tCZAS SELECT_s\t\t|\t\tCZAS INSERT_s\t\t|"<<endl;
  54.     file<<"|\t\tROZMIAR\t\t|\t\tCZAS BUBBLE_s\t\t|\t\tCZAS SELECT_s\t\t|\t\tCZAS INSERT_s\t\t|"<<endl;
  55.    
  56.     for(int i=0, j=100; i<gTimes1.size(); i++, j*=10) {
  57. //      file<<"|\t\t"<<size<<"\t\t|\t\t"<<generateTimes[i]<<"\t\t|\t\t"<<sortTimes[i]<<"\t\t|"<<endl;
  58.         file<<"|\t\t"<<j<<"\t\t|\t\t"<<gTimes1[i]<<"\t\t|\t\t"<<gTimes2[i]<<"\t\t|\t\t"<<gTimes3[i]"\t\t|"<<endl;
  59.     }
  60.     file.close();
  61. }
  62.  
  63. double timesDiff(clock_t clock1,clock_t clock2) {
  64.     double diffticks = clock1 - clock2;
  65.     double diffms    = diffticks / ( CLOCKS_PER_SEC / 1000 );
  66.     return diffms;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement