STANAANDREY

Untitled

Jan 7th, 2024
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <assert.h>
  5. #include <stdbool.h>
  6.  
  7. typedef enum {
  8. FIRST, MID, LAST
  9. } PivTp;
  10.  
  11. void insertionSort(int arr[], int l, int r) {
  12. int i, key, j;
  13. for (i = l + 1; i <= r; i++) {
  14. key = arr[i];
  15. j = i - 1;
  16.  
  17. while (j >= l && arr[j] > key) {
  18. arr[j + 1] = arr[j];
  19. j = j - 1;
  20. }
  21. arr[j + 1] = key;
  22. }
  23. }
  24.  
  25. void quickSort(int v[], int l, int r, int lim, int pivTp)
  26. {
  27. if (l < r) {
  28. int m = (l + r) / 2;
  29. int piv;
  30. if (pivTp == FIRST) {
  31. piv = l;
  32. } else if (pivTp == LAST) {
  33. piv = r;
  34. } else {
  35. piv = (l + r) / 2;
  36. }
  37. int aux = v[l];
  38. v[l] = v[m];
  39. v[m] = aux;
  40. int i = l, j = r, d = 0;
  41. while (i < j) {
  42. if (v[i] > v[j]) {
  43. aux = v[i];
  44. v[i] = v[j];
  45. v[j] = aux;
  46. d = 1 - d;
  47. }
  48. i += d;
  49. j -= 1 - d;
  50. }
  51. if (i - 1 - l >= lim) {
  52. quickSort(v, l, i - 1, lim, rand() % 3);
  53. } else {
  54. insertionSort(v, l, i - 1);
  55. }
  56. if (r - i - 1 >= lim) {
  57. quickSort(v, i + 1, r, lim, rand() % 3);
  58. } else {
  59. insertionSort(v, i + 1, r);
  60. }
  61. }
  62. }
  63.  
  64. #define NMAX 10000000
  65. int main(void)
  66. {
  67. srand(time(NULL));
  68. static int arr[NMAX];
  69. for (int i = 0; i < NMAX; i++) {
  70. arr[i] = rand() % 100000;
  71. }
  72. quickSort(arr, 0, NMAX, 100, rand() % 3);
  73. for (int i = 1; i < NMAX; i++) {
  74. assert(arr[i - 1] <= arr[i]);
  75. }
  76. return 0;
  77. }
Add Comment
Please, Sign In to add comment