Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int partition (vector<int> &a, int l, int r)
- {
- int i= l-1, j=r;
- int v = a[r];
- for(;;)
- {
- while (a[++i] < v);
- while (v< a[--j]) if (j == l) break;
- if (i >= j) break;
- swap (a[i],a[j]);
- }
- swap(a[i],a[r]);
- return i;
- }
- void swap( vector<int> &A, vector<int> &B)
- {
- vector<int> t=A;
- A=B;
- B=t;
- }
- void compswap( vector<int> &A, vector<int> &B)
- {
- if(B >A) swap(A,B);
- }
- void quicksort(vector<int> &a, int l, int r)
- {
- if (r <= l) return;
- int i = partition(a,l,r);
- quicksort(a,l,i-1);
- quicksort(a,i+1,r);
- }
- int main() {
- int n=0, x=0;
- vector<int> a;
- cout<<"Quanti elementi inserire?"<<endl;
- cin>>n;
- for(int k=0; k<n; k++)
- {
- cout<<endl<<"Inserisci elemento: ";
- cin>>x;
- a.push_back(x);
- }
- for(int k=0; k<n; k++)
- {
- cout<<endl<<a[k];
- }
- quicksort(a,0,a.size()-1);
- cout<<endl;
- for(int k=0; k<n; k++)
- {
- cout<<endl<<a[k];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement