Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define N 100005
- using namespace std;
- int n, a[N];
- int Pivot(int st, int dr)
- {
- ///randomizare pivot
- int mij=(st+dr)/2,aux;
- aux=a[st],a[st]=a[mij], a[mij]=aux;
- ///asezare pivota a[st] pe pozitia lui
- int i=st, j=dr,d=0;/// pt d=0 scade j pt d=1 creste i
- while(i<j)
- {
- if(a[i]>a[j])
- {
- aux=a[i]; a[i]=a[j]; a[j]=aux;
- d=1-d;
- }
- i+=d;
- j-=1-d;
- }
- return i;
- }
- void QS(int st, int dr)
- {
- if(st<dr)
- {
- int mij=Pivot(st,dr);
- QS(st,mij-1);
- QS(mij+1,dr);
- }
- }
- void Citire()
- {
- cin>>n;
- for(int i=1;i<=n;i++)cin>>a[i];
- }
- void Afisare()
- {
- for(int i=1;i<=n;i++)
- cout<<a[i]<<' ';
- }
- int main()
- {
- Citire();
- QS(1,n);
- Afisare();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment