fork download
  1. #include <stdio.h>
  2.  
  3. void swap (double *a, double *b) {
  4. double temp = *a;
  5. *a = *b;
  6. *b = temp;
  7. }
  8.  
  9. void qsort_inplace_inner (double list[], long start, long end) {
  10. if (end - start <= 1) { return; }
  11. long pivotPos = start, partitionStart = start + 1, partitionEnd = end;
  12. while (partitionEnd - partitionStart > 0) {
  13. if (list[partitionStart] < list[pivotPos]) {
  14. swap(&list[pivotPos], &list[partitionStart]);
  15. pivotPos = partitionStart;
  16. partitionStart++;
  17. }
  18. else {
  19. swap(&list[partitionStart], &list[partitionEnd-1]);
  20. partitionEnd--;
  21. }
  22. }
  23. qsort_inplace_inner(list, start, pivotPos);
  24. qsort_inplace_inner(list, pivotPos + 1, end);
  25. }
  26.  
  27. void qsort_inplace (double list[], long len) {
  28. qsort_inplace_inner(list, 0, len);
  29. }
  30.  
  31. int main(void) {
  32. int i;
  33. double test[] = {85.06,
  34. 76.68,
  35. 35.32,
  36. 45.15,
  37. 9.85,
  38. 2.31,
  39. 37.93,
  40. 74.72,
  41. 93.11,
  42. 90.97,
  43. 30.62,
  44. 64.23,
  45. 61.06,
  46. 40.58,
  47. 40.56,
  48. 41.50,
  49. 88.69,
  50. 62.26,
  51. 50.41,
  52. 7.04};
  53. printf("Before: ");
  54. for (i = 0; i < 20; i++) {
  55. printf("%.2f ", test[i]);
  56. }
  57. printf("\n");
  58. qsort_inplace(test, 20);
  59. printf(" After: ");
  60. for (i = 0; i < 20; i++) {
  61. printf("%.2f ", test[i]);
  62. }
  63. printf("\n");
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0s 2052KB
stdin
Standard input is empty
stdout
Before: 85.06 76.68 35.32 45.15 9.85 2.31 37.93 74.72 93.11 90.97 30.62 64.23 61.06 40.58 40.56 41.50 88.69 62.26 50.41 7.04 
 After: 2.31 7.04 9.85 30.62 35.32 37.93 40.56 40.58 41.50 45.15 50.41 61.06 62.26 64.23 74.72 76.68 85.06 88.69 90.97 93.11