Advertisement
darkjessy94

quicksort senza vector c++ - ivan

Oct 13th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int partition (vector<int> &a, int l, int r)
  8. {
  9.     int i= l-1,  j=r;
  10.     int v = a[r];
  11.     for(;;)
  12.     {
  13.         while (a[++i] < v);
  14.         while (v< a[--j]) if (j == l) break;
  15.         if (i >= j) break;
  16.         swap (a[i],a[j]);
  17.     }
  18.     swap(a[i],a[r]);
  19.     return i;
  20. }
  21.  
  22. void swap( vector<int> &A, vector<int> &B)
  23. {
  24.     vector<int> t=A;
  25.     A=B;
  26.      B=t;
  27. }
  28.  
  29. void compswap( vector<int> &A, vector<int> &B)
  30. {
  31.     if(B >A) swap(A,B);
  32. }
  33.  
  34. void quicksort(vector<int> &a, int l, int r)
  35. {
  36.   if (r <= l) return;
  37.   int i = partition(a,l,r);
  38.   quicksort(a,l,i-1);
  39.   quicksort(a,i+1,r);
  40. }
  41.  
  42. int main() {
  43.     int n=0, x=0;
  44. vector<int> a;
  45. cout<<"Quanti elementi inserire?"<<endl;
  46. cin>>n;
  47. for(int k=0; k<n; k++)
  48. {
  49.     cout<<endl<<"Inserisci elemento: ";
  50.     cin>>x;
  51.     a.push_back(x);
  52. }
  53. for(int k=0; k<n; k++)
  54. {
  55.     cout<<endl<<a[k];
  56. }
  57.  quicksort(a,0,a.size()-1);
  58. cout<<endl;
  59.   for(int k=0; k<n; k++)
  60. {
  61.     cout<<endl<<a[k];
  62. }
  63.  return 0;
  64.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement