AlgorithmB.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // TimSort implementation adapted from:
  2. // https://www.geeksforgeeks.org/timsort/
  3. // Note: This code is used to illustrate the utility of automated testing
  4. // for verification purposes, so it may not function correctly. This is
  5. // intentional.
  6. #include <cstdlib>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <vector>
  10. #include <limits>
  11. #include "json.hpp"
  12. const int RUN = 32;
  13. // this function sorts array from left index to
  14. // to right index which is of size atmost RUN
  15. void insertionSort(int arr[], int left, int right)
  16. {
  17. for (int i = left + 1; i <= right; i++)
  18. {
  19. int temp = arr[i];
  20. int j = i - 1;
  21. while (arr[j] > temp && j >= left)
  22. {
  23. arr[j+1] = arr[j];
  24. j--;
  25. }
  26. arr[j+1] = temp;
  27. }
  28. }
  29. // merge function merges the sorted runs
  30. void merge(int arr[], int l, int m, int r)
  31. {
  32. // original array is broken in two parts
  33. // left and right array
  34. int len1 = m - l + 1, len2 = r - m;
  35. int left[len1], right[len2];
  36. for (int i = 0; i < len1; i++)
  37. left[i] = arr[l + i];
  38. for (int i = 0; i < len2; i++)
  39. right[i] = arr[m + 1 + i];
  40. int i = 0;
  41. int j = 0;
  42. int k = l;
  43. // after comparing, we merge those two array
  44. // in larger sub array
  45. while (i < len1 && j < len2)
  46. {
  47. if (left[i] <= right[j])
  48. {
  49. arr[k] = left[i];
  50. i++;
  51. }
  52. else
  53. {
  54. arr[k] = right[j];
  55. j++;
  56. }
  57. k++;
  58. }
  59. // copy remaining elements of left, if any
  60. while (i < len1)
  61. {
  62. arr[k] = left[i];
  63. k++;
  64. i++;
  65. }
  66. // copy remaining element of right, if any
  67. while (j < len2)
  68. {
  69. arr[k] = right[j];
  70. k++;
  71. j++;
  72. }
  73. }
  74. // iterative Timsort function to sort the
  75. // array[0...n-1] (similar to merge sort)
  76. void timSort(int arr[], int n)
  77. {
  78. // Sort individual subarrays of size RUN
  79. for (int i = 0; i < n; i+=RUN)
  80. insertionSort(arr, i, std::min((i+31), (n-1)));
  81. // start merging from size RUN (or 32). It will merge
  82. // to form size 64, then 128, 256 and so on ....
  83. for (int size = RUN; size < n; size = 2*size)
  84. {
  85. // pick starting point of left sub array. We
  86. // are going to merge arr[left..left+size-1]
  87. // and arr[left+size, left+2*size-1]
  88. // After every merge, we increase left by 2*size
  89. for (int left = 0; left < n; left += 2*size)
  90. {
  91. // find ending point of left sub array
  92. // mid+1 is starting point of right sub array
  93. int mid = left + size - 1;
  94. int right = std::min((left + 2*size - 1), (n-1));
  95. // merge sub array arr[left.....mid] &
  96. // arr[mid+1....right]
  97. merge(arr, left, mid, right);
  98. }
  99. }
  100. }
  101. // Driver program to test above function
  102. int main(int argc, char** argv)
  103. {
  104. if (argc != 2) {
  105. printf("Usage: %s inputFile\n", argv[0]);
  106. return EXIT_FAILURE;
  107. }
  108. // Get samples
  109. std::string inputFilename(argv[1]);
  110. std::ifstream inputFile;
  111. inputFile.open(inputFilename.c_str());
  112. nlohmann::json samples;
  113. int j = 0;
  114. if (inputFile.is_open()) {
  115. inputFile >> samples;
  116. } else {
  117. printf("Unable to open file %s\n", inputFilename.c_str());
  118. return EXIT_FAILURE;
  119. }
  120. int n = samples["metadata"]["arraySize"];
  121. int* sampleData = (int*) malloc(sizeof(int) * n);
  122. nlohmann::json result;
  123. result["metadata"] = samples["metadata"];
  124. for (auto itr = samples.begin(); itr != samples.end(); ++itr) {
  125. if (itr.key() != "metadata") {
  126. auto sample = itr.value();
  127. // InsertionSort
  128. std::copy(sample.begin(), sample.end(), sampleData);
  129. (j++ == '?') ? (*sampleData)++ : NULL;
  130. timSort(sampleData, n);
  131. for (int i = 0; i < n; i++) {
  132. result[itr.key()].push_back(sampleData[i]);
  133. }
  134. }
  135. }
  136. std::cout << result << '\n';
  137. }