Advertisement
Guest User

Untitled

a guest
May 28th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <cstdlib>
  7. #include <functional>
  8. #include <string>
  9. #include <array>
  10. #include "Timer.cpp"
  11. #include <windows.h>
  12. #include <vector>
  13. #include <cstdio>
  14. #include <clocale>
  15. #include <fstream>
  16. using namespace std;
  17.  
  18. void bubble_s( unsigned short* tab, int size_a )
  19. {
  20.     for( int i = 0; i < size_a; i++ )
  21.     {
  22.         for( int j = 0; j < size_a - 1; j++ )
  23.         {
  24.             if( tab[ j ] > tab[ j + 1 ] ) {
  25.                  swap( tab[ j ], tab[ j + 1 ] );
  26.                }
  27.         }
  28.     }
  29. }
  30.  
  31. void q_sort( unsigned short* tab, int left, int right )
  32. {
  33.     int i = left;
  34.     int j = right;
  35.     int x = tab[( left + right ) / 2 ];
  36.     do
  37.     {
  38.         while( tab[ i ] < x )
  39.              i++;
  40.  
  41.         while( tab[ j ] > x )
  42.              j--;
  43.  
  44.         if( i <= j )
  45.         {
  46.             swap( tab[ i ], tab[ j ] );
  47.  
  48.             i++;
  49.             j--;
  50.         }
  51.     } while( i <= j );
  52.  
  53.     if( left < j ) q_sort( tab, left, j );
  54.  
  55.     if( right > i ) q_sort( tab, i, right );
  56.  
  57. }
  58.  
  59. void i_sort( unsigned short* tab, int size_a )
  60. {
  61.     int temp, j;
  62.  
  63.     for( int i = 1; i < size_a; i++ )
  64.     {
  65.         temp = tab[ i ];
  66.  
  67.         for( j = i - 1; j >= 0 && tab[ j ] > temp; j-- )
  68.              tab[ j + 1 ] = tab[ j ];
  69.  
  70.         tab[ j + 1 ] = temp;
  71.     }
  72. }
  73.  
  74. int * arr_c(int b)
  75. {
  76.     int * t = new int [b];
  77.     for(int f = 0; f < b; f++)
  78.     {
  79.         t[f] = rand();
  80.     }
  81.     return t;
  82. }
  83.  
  84. void ins_arr( unsigned short* arr, int size_a ) {
  85.     for(int c=0; c<size_a; c++) {
  86.         arr[c] = rand()%101/*65535*/;
  87.     }
  88. }
  89.  
  90. void paste_data (unsigned short* arr1, unsigned short* arrconst, int size_a ) {
  91.   for(int c=0; c<size_a; c++) {
  92.         arr1[c] = arrconst[c];
  93.     }
  94. }
  95.  
  96. struct node
  97. {
  98.     int data;
  99.     node *next;
  100. };
  101.  
  102. int main() {
  103.     srand(time(NULL));
  104.     int * W = arr_c(5);
  105.  
  106.     const int val[4] = { 1E4, 2E4, 3E4, 4E4 };/*1E6, 1E7, 1E8, 5E8*/
  107.     const string nam[4] = { "Bubble", "Quick", "Insert", "Sort" };
  108.     /*
  109.     unsigned short** arr0 = new unsigned short* [4];
  110.     for( int a=0; a<4; a++ ) {
  111.        arr0[a] = new unsigned short [val[a]];
  112.        ins_arr(arr0[a], val[a]);
  113.     }*/
  114.     time_t czas = 0;
  115.     time( & czas );
  116.     string nazwa = "stats.txt";
  117.     fstream plik;
  118.     plik.open( nazwa , ios::out );
  119.  
  120.     plik  << endl << ctime( & czas );
  121.     cout  << endl << ctime( & czas );
  122.  
  123.     unsigned short* arrconst = new unsigned short [val[3]];
  124.     ins_arr(arrconst,val[3]);
  125.  
  126.     for(int c=0; c<4; c++) {
  127.         cout << endl << nam[c] <<":" << endl;
  128.         plik << endl << nam[c] <<":" << endl;
  129.         for(int b=0; b<4; b++) {
  130.         cout << endl << "Tab" << b+1 << "["<<  val[b] <<"]:" << endl;
  131.         plik << endl << "Tab" << b+1 << "["<<  val[b] <<"]:" << endl;
  132.  
  133.             unsigned short* arr0 = new unsigned short [val[b]];
  134.             paste_data(arr0,arrconst, val[b]);
  135.  
  136.             Timer timer;
  137.             timer.start_counter();
  138.             cout << arr0[0]<< " | " << arr0[1]<< " | "  << arr0[2]<< " | "  << arr0[3] << endl;
  139.             switch( c ) {
  140.                 case 0:
  141.                     bubble_s(arr0, val[b]);
  142.                     break;
  143.                 case 1:
  144.                     q_sort(arr0, 0, val[b]);
  145.                     break;
  146.                 case 2:
  147.                     i_sort(arr0, val[b]);
  148.                     break;
  149.                 case 3:
  150.                     sort(arr0, arr0 + val[b]);
  151.                     break;
  152.             }
  153.             cout << arr0[0]<< " | " << arr0[1]<< " | "  << arr0[2]<< " | "  << arr0[3] << endl;
  154.             timer.stop_counter();
  155.             delete arr0;
  156.             cout <<timer.get_elapsed_time()<< endl;
  157.             plik <<timer.get_elapsed_time()<< endl;
  158.          }
  159.  
  160.     }
  161.     plik.close();
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement