fork download
  1.  
  2. #include <stdio.h>
  3.  
  4. #define N 10
  5.  
  6. void quicksort(int a[], int low, int high);
  7. int split(int a[], int low, int high);
  8.  
  9. int main(void)
  10. {
  11. int a[N], i;
  12.  
  13. printf("Enter %d numbers to be sorted: ", N);
  14. for (i = 0; i < N; i++)
  15. scanf("%d", &a[i]);
  16.  
  17. quicksort(a, 0, N - 1);
  18.  
  19. printf("In sorted order: ");
  20. for (i = 0; i < N; i++)
  21. printf("%d ", a[i]);
  22. printf("\n");
  23.  
  24. return 0;
  25. }
  26.  
  27. void quicksort(int a[], int low, int high)
  28. {
  29. int middle;
  30.  
  31. if (low >= high) return;
  32. middle = split(a, low, high);
  33. quicksort(a, low, middle - 1);
  34. quicksort(a, middle + 1, high);
  35. }
  36.  
  37. int split(int a[], int low, int high)
  38. {
  39. int part_element = a[low];
  40.  
  41. for (;;) {
  42. while (low < high && part_element <= a[high])
  43. high--;
  44. if (low >= high) break;
  45. a[low++] = a[high];
  46.  
  47. while (low < high && a[low] <= part_element)
  48. low++;
  49. if (low >= high) break;
  50. a[high--] = a[low];
  51. }
  52.  
  53. a[high] = part_element;
  54. return high;
  55. }
Success #stdin #stdout 0s 2056KB
stdin
Standard input is empty
stdout
Enter 10 numbers to be sorted: In sorted order: -1218528284 -1218380653 -1081864236 -1081856142 -1081856142 0 47 134513549 134514466 134519380