Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "graphics.h"
- #include <bits/stdc++.h>
- using namespace std;
- // Initialize the size
- // with the total numere to sorted
- // and the dist to be maintained in graph
- vector<int> numere; //vector alocat dinamic, gol
- int size = 250; // numarul de elemente din vector
- int dist = 4; // distanta dintre lini
- //functie pentru a schimba linile graphic
- void swap(int i, int j, int x, int y)
- {
- // Swapping the first line with the correct line
- // by making it black again and then draw the pixel
- // for white color.
- setcolor(GREEN);
- line(i, size, i, size - x);
- setcolor(BLACK);
- line(i, size, i, size - x);
- setcolor(WHITE);
- line(i, size, i, size - y);
- // Swapping the first line with the correct line
- // by making it black again and then draw the pixel
- // for white color.
- setcolor(GREEN);
- line(j, size, j, size - y);
- setcolor(BLACK);
- line(j, size, j, size - y);
- setcolor(WHITE);
- line(j, size, j, size - x);
- }
- // Bubble sort function
- void bubbleSort()
- {
- int temp, i, j;
- for (i = 1; i < size; i++) {
- for (j = 0; j < size - i; j++) {
- if (numere[j] > numere[j + 1]) {
- temp = numere[j];
- numere[j] = numere[j + 1];
- numere[j + 1] = temp;
- // As we swapped the last two numere
- // just swap the lines with the values.
- // This is function call
- // for swapping the lines
- swap(dist * j + 1,
- dist * (j + 1) + 1,
- numere[j + 1],
- numere[j]);
- }
- }
- }
- }
- // Driver program
- int main()
- {
- // auto detection of screen size
- int gd = DETECT, gm;
- int wid1;
- // Graph initialization
- initgraph(&gd, &gm, NULL);
- delay(500);
- // setting up window size (dist*size) * (size)
- wid1 = initwindow(dist * size + 1, size + 1);
- setcurrentwindow(wid1);
- // Initializing the array
- for (int i = 1; i <= size; i++)
- numere.push_back(i);
- shuffle(numere.begin(), numere.end(), default_random_engine());
- // Initial plot of numere in graph taking
- // the vector position as x-axis and its
- // corresponding value will be the height of line.
- for (int i = 1; i <= dist * size; i += dist) {
- line(i, size, i, (size - numere[i / dist]));
- }
- // Delay the code
- delay(200);
- // Call sort
- bubbleSort();
- for (int i = 0; i < size; i++) {
- cout << numere[i] << " ";
- }
- cout << endl;
- // Wait for sometime .
- delay(5000);
- // Close the graph
- closegraph();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement