sorting.c 1.3 KB

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