Advertisement
villers

tri rapide

Apr 16th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. /*
  2. ** sort.cpp for Elendil in /home/viller_m/Desktop/
  3. **
  4. ** Made by mickael villers
  5. ** Login   <[email protected]>
  6. **
  7. ** Started on  Thu Apr 16 22:04:07 2015 mickael villers
  8. ** Last update Thu Apr 16 22:28:59 2015 mickael villers
  9. */
  10.  
  11. #include <iostream>
  12. using namespace std;
  13.  
  14. const int N = 10;
  15.  
  16. void    echanger(int &a, int &b)
  17. {
  18.   int   tmp;
  19.  
  20.   tmp = a;
  21.   a = b;
  22.   b = tmp;
  23. }
  24.  
  25. int partitionner(int *tableau, int debut, int fin, int pivot)
  26. {
  27.   int   i;
  28.   int   j;
  29.  
  30.   i = debut - 1;
  31.   j = fin + 1;
  32.   while (1) {
  33.     do{
  34.       j--;
  35.     } while (tableau[j] > pivot);
  36.     do{
  37.       i++;
  38.     } while (tableau[i] < pivot);
  39.     if (i < j) {
  40.       echanger(tableau[i], tableau[j]);
  41.     }
  42.     else {
  43.       return j;
  44.     }
  45.   }
  46. }
  47.  
  48. void    triRapide(int *tableau, int debut, int fin)
  49. {
  50.   int   pivot;
  51.  
  52.   if (debut < fin) {
  53.     pivot = partitionner(tableau, debut, fin, tableau[debut]);
  54.     triRapide(tableau, debut, pivot);
  55.     triRapide(tableau, pivot+1, fin);
  56.   }
  57. }
  58.  
  59. int main(int argc, char **argv)
  60. {
  61.   int   tableau[N] = {16, 2, 77, 40, 1, 12, 23, 33, 76, 7};
  62.  
  63.   triRapide(tableau, 0, N - 1);
  64.   for (int i = 0; i < N; i++)
  65.   {
  66.     cout << tableau[i];
  67.     if (i < N - 1) {
  68.       cout << ", ";
  69.     }
  70.   }
  71.   cout << endl;
  72.  
  73.   return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement