Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void mergee(int n, int maintab[], int pocz, int srodek, int kon)
- {
- int tabpom[n];
- for(int i=pocz; i<=kon; i++)
- {
- tabpom[i] = maintab[i];
- }
- int pocz1 = pocz, srodek1 = srodek;
- while(pocz <= srodek1 && srodek+1 <= kon)
- {
- if(tabpom[pocz] > tabpom[srodek+1] )
- {
- maintab[pocz1] = tabpom[srodek+1];
- srodek++; pocz1++;
- }
- else
- {
- maintab[pocz1] = tabpom[pocz];
- pocz++; pocz1++;
- }
- }
- while (pocz<=srodek1)
- {
- maintab[pocz1] = tabpom[pocz]; // w przypadku gdy zbior drugi sie skonczy musimy reszte liczb
- // pozostalych ze zbioru 1 wprowadzic do naszej mainowej tablicy
- pocz1++; pocz++; // jesli wszystko sie zgadza to w ogole nie wejdzie do whila
- }
- }
- void mergesort(int n, int tab[], int pocz, int kon)
- {
- int srodek;
- if(pocz<kon)
- {
- srodek = (pocz+kon)/2;
- mergesort(n, tab, pocz, srodek);
- mergesort(n, tab, srodek+1, kon);
- mergee(n, tab, pocz, srodek, kon);
- }
- }
- int main()
- {
- int n; cin>>n;
- int tab[n];
- for(int i=0; i<n; i++) {cin >> tab[i];}
- int poczatek = 0, koniec = n-1;
- mergesort(n, tab, poczatek, koniec);
- for(int i=0; i<n; i++)
- {
- cout<<tab[i]<<" ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement