Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <stdlib.h>
- using namespace std;
- void wypisz(int *tab, int wiel);
- void losuj(int *tab, int wiel);
- void sortuj(int poczatek, int koniec);
- void sortuj2(int *tab, int poczatek, int koniec);
- //const int ilosc=10;
- //int dane[ilosc];
- //int bufor[ilosc];
- int main()
- {
- const int ilosc2=10;
- int dane2[ilosc2];
- losuj(dane2, ilosc2);
- wypisz(dane2, ilosc2);
- sortuj2(dane2, 0, ilosc2-1);
- wypisz(dane2, ilosc2);
- // losuj(dane, ilosc);
- // wypisz(dane, ilosc);
- //
- // sortuj(0, ilosc-1);
- // wypisz(dane, ilosc);
- return 0;
- }
- void losuj(int *tab, int wiel)
- {
- srand(time(NULL));
- for(int i=0; i<wiel; i++) tab[i]=rand()%10;
- }
- void wypisz(int *tab, int wiel)
- {
- for(int i=0; i<wiel; i++) cout << tab[i] << " ";
- cout << endl;
- }
- void sortuj(int poczatek, int koniec)
- {
- int srodek=(poczatek+koniec+1)/2; //i1, i2;
- if(srodek-poczatek>1) sortuj(poczatek, srodek-1);
- if(koniec-srodek>0) sortuj(srodek, koniec);
- int i1=poczatek, i2=srodek;
- for(int i=poczatek; i<=koniec; i++)
- bufor[i] = (i1==srodek || (i2<=koniec && dane[i1]>dane[i2])) ? dane[i2++] : dane[i1++];
- for(int i=poczatek; i<=koniec; i++)
- dane[i]=bufor[i];
- }
- void sortuj2(int *tab, int poczatek, int koniec)
- {
- static int* bufor = new int[koniec];
- int srodek=(poczatek+koniec+1)/2; //i1, i2;
- if(srodek-poczatek>1) sortuj2(tab, poczatek, srodek-1);
- if(koniec-srodek>0) sortuj2(tab, srodek, koniec);
- int i1=poczatek, i2=srodek;
- for(int i=poczatek; i<=koniec; i++)
- bufor[i] = (i1==srodek || (i2<=koniec && tab[i1]>tab[i2])) ? tab[i2++] : tab[i1++];
- for(int i=poczatek; i<=koniec; i++)
- tab[i]=bufor[i];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement