Advertisement
Stancu

Grafica Buble Sort

Nov 12th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include "graphics.h"
  3. #include <bits/stdc++.h>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. // Initialize the size
  9. // with the total numere to sorted
  10. // and the dist to be maintained in graph
  11. vector<int> numere; //vector alocat dinamic, gol
  12. int size = 250; // numarul de elemente din vector
  13. int dist = 4; // distanta dintre lini
  14.  
  15.  
  16. //functie pentru a schimba linile graphic
  17. void swap(int i, int j, int x, int y)
  18. {
  19.     // Swapping the first line with the correct line
  20.     // by making it black again and then draw the pixel
  21.     // for white color.
  22.  
  23.     setcolor(GREEN);
  24.     line(i, size, i, size - x);
  25.     setcolor(BLACK);
  26.     line(i, size, i, size - x);
  27.     setcolor(WHITE);
  28.     line(i, size, i, size - y);
  29.  
  30.     // Swapping the first line with the correct line
  31.     // by making it black again and then draw the pixel
  32.     // for white color.
  33.     setcolor(GREEN);
  34.     line(j, size, j, size - y);
  35.     setcolor(BLACK);
  36.     line(j, size, j, size - y);
  37.     setcolor(WHITE);
  38.     line(j, size, j, size - x);
  39. }
  40.  
  41. // Bubble sort function
  42. void bubbleSort()
  43. {
  44.     int temp, i, j;
  45.  
  46.     for (i = 1; i < size; i++) {
  47.         for (j = 0; j < size - i; j++) {
  48.             if (numere[j] > numere[j + 1]) {
  49.                 temp = numere[j];
  50.                 numere[j] = numere[j + 1];
  51.                 numere[j + 1] = temp;
  52.  
  53.                 // As we swapped the last two numere
  54.                 // just swap the lines with the values.
  55.                 // This is function call
  56.                 // for swapping the lines
  57.                 swap(dist * j + 1,
  58.                     dist * (j + 1) + 1,
  59.                     numere[j + 1],
  60.                     numere[j]);
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66. // Driver program
  67. int main()
  68. {
  69.  
  70.     // auto detection of screen size
  71.     int gd = DETECT, gm;
  72.     int wid1;
  73.  
  74.     // Graph initialization
  75.     initgraph(&gd, &gm, NULL);
  76.  
  77.     delay(500);
  78.  
  79.     // setting up window size (dist*size) * (size)
  80.     wid1 = initwindow(dist * size + 1, size + 1);
  81.     setcurrentwindow(wid1);
  82.  
  83.     // Initializing the array
  84.     for (int i = 1; i <= size; i++)
  85.         numere.push_back(i);
  86.  
  87.  
  88.     shuffle(numere.begin(), numere.end(), default_random_engine());
  89.  
  90.     // Initial plot of numere in graph taking
  91.     // the vector position as x-axis and its
  92.     // corresponding value will be the height of line.
  93.     for (int i = 1; i <= dist * size; i += dist) {
  94.         line(i, size, i, (size - numere[i / dist]));
  95.     }
  96.  
  97.     // Delay the code
  98.     delay(200);
  99.  
  100.     // Call sort
  101.     bubbleSort();
  102.  
  103.     for (int i = 0; i < size; i++) {
  104.         cout << numere[i] << " ";
  105.     }
  106.     cout << endl;
  107.  
  108.     // Wait for sometime .
  109.     delay(5000);
  110.  
  111.     // Close the graph
  112.     closegraph();
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement