sorting.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2019 Mael Keryell
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include "sorting.h"
  18. unsigned chose_pivot(int first, int last)
  19. {
  20. return ( rand() % (last - first + 1) + first );
  21. }
  22. int partitionning(double *arr, int first, int last, int pivot)
  23. {
  24. double tmp;
  25. int i,j;
  26. tmp = arr[last];
  27. arr[last] = arr[pivot];
  28. arr[pivot] = tmp;
  29. j = first;
  30. for (i = first; i < last; i++)
  31. {
  32. if (arr[i] <= arr[last]){
  33. tmp = arr[i];
  34. arr[i] = arr[j];
  35. arr[j] = tmp;
  36. j++;
  37. }
  38. }
  39. tmp = arr[j];
  40. arr[j] = arr[last];
  41. arr[last] = tmp;
  42. return j;
  43. }
  44. void quicksort(double *arr, int first, int last)
  45. {
  46. if (first < last){
  47. int pivot = chose_pivot(first, last);
  48. int j;
  49. j = partitionning(arr, first, last, pivot);
  50. quicksort(arr, first, j - 1);
  51. quicksort(arr, j + 1, last);
  52. }
  53. }