Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <graphics.h>
- #include <stdio.h>
- using namespace std;
- void BubleSort(int v[], int n);
- int main()
- {
- int n;
- char text[3];
- cout<<"Cate numere se citesc?";
- cin >> n;
- int L=n*140, l=400;
- int v[n];
- for (int i=0; i<n; i++)
- {
- cout<<"v["<<i<<"]=";
- cin>>v[i];
- }
- initwindow(L, l);
- int x=100;
- for (int i=0; i<n; i++, x+=100) // afisare numere
- {
- setcolor(WHITE);
- circle (x, 300, 40);
- sprintf(text, "%d", v[i]);
- settextstyle(3, 0, 2);
- int latime=textwidth(text);
- int inaltime=textheight(text);
- outtextxy(x-latime/2, 300-inaltime/2, text);
- }
- BubleSort(v, n);
- getch();
- return 0;
- }
- void BubleSort (int v[], int n)
- {
- int ok, x=10, latime, inaltime;
- do{
- ok=1;
- x=100;
- for (int i=0; i<n-1; i++, x+=100)
- {
- cout<<"Se compara "<<v[i]<<" cu "<<v[i+1]<<endl;
- setcolor(RED);
- circle(x, 300, 40);
- circle(x+100, 300, 40);
- delay(1000);
- setcolor(WHITE);
- circle(x, 300, 40);
- circle(x+100, 300, 40);
- delay (500);
- char text[3];
- if (v[i]>v[i+1])
- {
- sprintf(text, "%d", v[i+1]);
- settextstyle(3, 0, 2);
- latime=textwidth(text);
- inaltime=textheight(text);
- setcolor(BLACK);
- outtextxy(x-20, 300-inaltime/2, "sters");
- outtextxy(x+80, 300-inaltime/2, "sters");
- setcolor(WHITE);
- outtextxy(x-latime/2, 300-inaltime/2, text);
- sprintf(text, "%d", v[i]);
- settextstyle(3, 0, 2);
- latime=textwidth(text);
- inaltime=textheight(text);
- outtextxy(x+100-latime/2, 300-inaltime/2, text);
- delay(1000);
- cout<<"Se interschimba "<<v[i]<<" cu "<<v[i+1]<<endl;
- swap(v[i], v[i+1]);
- ok=0;
- }
- }
- }while (!ok);
- setcolor(RED);
- settextstyle(3, 0, 3);
- outtextxy(50, 50, "Sirul este sortat!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement