Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int czytaj()
- {
- int n;
- do
- {
- cout<<"Wrowadz rozmiar tablicy z zakresu od 100 do 1000"<<endl;
- cin>>n;
- if(n<100 || n>1000)
- {
- cout<<"Nieodpowiedni rozmiar tablicy"<<endl;
- }
- }
- while(n<100 || n>1000);
- return n;
- }
- void losuj(int t[] , int n)
- {
- srand(time(NULL));
- for(int i=0 ; i<n ; i++)
- {
- t[i]=rand()%21-10;
- }
- }
- void pisz(int t[] , int n)
- {
- cout<<"Pierwsza wylosowana tablica"<<endl;
- for(int i=0 ; i<n ; i++)
- {
- cout<<t[i]<<" ";
- }
- cout<<" "<<endl;
- }
- void sortowanie( int t[], int n )
- {
- int zamiana ;
- int porownanie ;
- for( int i = 0; i < n; i++ )
- {
- for( int a = 0; a < n - 1; a++ )
- {
- porownanie++;
- if( t[ a ] > t[ a + 1 ] )
- {
- swap( t[ a ], t[ a + 1 ] );
- zamiana++;
- }
- }
- }
- cout<<"Tablica po sortowaniu"<<endl;
- for(int i=0 ; i<n ; i++)
- {
- cout<<t[i]<<" ";
- }
- cout<<" "<<endl;
- cout<<"Ilosc wykonanych zamian: "<<zamiana<<endl;
- cout<<"Ilosc wykonanych porownan: "<<porownanie<<endl;
- }
- int main()
- {
- int n=czytaj();
- int t[n];
- losuj(t ,n);
- pisz(t ,n);
- sortowanie(t, n);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement