Advertisement
1fractal

Untitled

Oct 1st, 2020
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <limits.h>
  4. #include <stdlib.h>
  5. #include <chrono>
  6. #include <fcntl.h>
  7. #include <vector>
  8. #include <unistd.h>
  9. #include <cstring>
  10.  
  11. /**
  12.  * @fn void BubbleSort(int* arr, unsigned n)
  13.  * @briefTrie le tableau passé en argument selon méthode du tri à bulles
  14.  * @param arr Le tableau a trier
  15.  * @param n La taille du tableau
  16.  * @pre Le tableau doit être de taille strictement positive soit <em> n > 0</em>
  17.  */
  18. void BubleSort(int* arr, unsigned n){
  19.     for (int i = n-1; i >= 0; i--) {
  20.         for (int j = 0; j < i; j++) {
  21.             if(arr[i] > arr[j+1]){
  22.                 int tmp = arr[i];
  23.                 arr[i] = arr[j+1];
  24.                 arr[j+1] = tmp;
  25.             }
  26.         }
  27.     }
  28. }
  29.  
  30. void insertSort(int* arr, unsigned n){
  31.     for (int i = 1; i < n ; ++i) {
  32.         int ainserer = arr[i];
  33.         int j = i;
  34.         while ( (j > 0) && (arr[j-1] > ainserer)){
  35.             arr[j] = arr[j-1];
  36.             j--;
  37.         }
  38.         arr[j] = ainserer;
  39.     }
  40. }
  41.  
  42. int main(int argc, char* argv[]) {
  43.     char buff[254];
  44.     time_t t;
  45.     std::vector<std::pair<int,int>> containerBubble;
  46.     std::vector<std::pair<int,int>> containerInsert;
  47.  
  48.     srand((unsigned) time(&t));
  49.     //fill array with random number
  50.  
  51.     int fdBubble;
  52.     int fdInsert;
  53.     fdBubble = open("bubble.time",O_WRONLY | O_CREAT | O_TRUNC,0666);
  54.     fdInsert = open("insert.time",O_WRONLY | O_CREAT | O_TRUNC,0666);
  55.     if(fdBubble == -1){
  56.         perror("Error on open");
  57.         exit(EXIT_FAILURE);
  58.     }
  59.     if(fdInsert == -1){
  60.         perror("Error on open");
  61.         exit(EXIT_FAILURE);
  62.     }
  63.  
  64.     //bubble sort
  65.     int nb = 10;
  66.     while(nb <= 81920){
  67.         int arr[nb];
  68.         for (unsigned i = 0; i < nb; ++i) {
  69.             arr[i] = rand() % INT_MAX;
  70.         }
  71.         auto start = std::chrono::high_resolution_clock::now();
  72.         BubleSort(arr,nb);
  73.         auto end = std::chrono::high_resolution_clock::now();
  74.         auto durationSec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  75.  
  76.         containerBubble.emplace_back(nb,durationSec.count());
  77.         nb *=2;
  78.     }
  79.     std::cout << "Bubble\n";
  80.     std::cout << "N\t\t"<<"Duration"<<"\n";
  81.  
  82.     for (auto itt  = containerBubble.begin(); itt < containerBubble.end() ; itt++) {
  83.         sprintf(buff,"%5d\t\t%5d\n",itt->first,itt->second);
  84.         printf("%s",buff);
  85.         if(write(fdBubble,buff,strlen(buff)) == -1){
  86.             perror("Error: write");
  87.         }
  88.     }
  89.  
  90.     //insert
  91.     nb = 10;
  92.     while(nb <= 81920){
  93.         int arr[nb];
  94.         for (unsigned i = 0; i < nb; ++i) {
  95.             arr[i] = rand() % INT_MAX;
  96.         }
  97.         auto start = std::chrono::high_resolution_clock::now();
  98.         insertSort(arr,nb);
  99.         auto end = std::chrono::high_resolution_clock::now();
  100.         auto durationSec = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  101.  
  102.         containerInsert.emplace_back(nb,durationSec.count());
  103.         nb *=2;
  104.     }
  105.     std::cout << "Insert\n";
  106.     std::cout << "N\t\t"<<"Duration"<<"\n";
  107.  
  108.     for (auto itt  = containerInsert.begin(); itt < containerInsert.end() ; itt++) {
  109.         sprintf(buff,"%5d\t\t%5d\n",itt->first,itt->second);
  110.         printf("%s",buff);
  111.         if(write(fdInsert,buff,strlen(buff)) == -1){
  112.             perror("Error: write");
  113.         }
  114.     }
  115.  
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement