AlgorithmC.cxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // PancakeSort implementation adapted from:
  2. // https://www.geeksforgeeks.org/pancake-sorting/
  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 "json.hpp"
  11. /* Reverses arr[0..i] */
  12. void flip(int arr[], int i)
  13. {
  14. int temp, start = 0;
  15. while (start < i)
  16. {
  17. temp = arr[start];
  18. arr[start] = arr[i];
  19. arr[i] = temp;
  20. start++;
  21. i--;
  22. }
  23. }
  24. // Returns index of the
  25. // maximum element in
  26. // arr[0..n-1]
  27. int findMax(int arr[], int n)
  28. {
  29. int mi, i;
  30. for (mi = 0, i = 0; i < n; ++i)
  31. if (arr[i] > arr[mi])
  32. mi = i;
  33. return mi;
  34. }
  35. // The main function that
  36. // sorts given array using
  37. // flip operations
  38. void pancakeSort(int *arr, int n)
  39. {
  40. // Start from the complete
  41. // array and one by one
  42. // reduce current size
  43. // by one
  44. for (int curr_size = n; curr_size > 1; --curr_size)
  45. {
  46. // Find index of the
  47. // maximum element in
  48. // arr[0..curr_size-1]
  49. int mi = findMax(arr, curr_size);
  50. // Move the maximum
  51. // element to end of
  52. // current array if
  53. // it's not already
  54. // at the end
  55. if (mi != curr_size-1)
  56. {
  57. // To move at the end,
  58. // first move maximum
  59. // number to beginning
  60. flip(arr, mi);
  61. // Now move the maximum
  62. // number to end by
  63. // reversing current array
  64. flip(arr, curr_size-1);
  65. }
  66. }
  67. }
  68. // Driver program to test above function
  69. int main(int argc, char** argv)
  70. {
  71. if (argc != 2) {
  72. printf("Usage: %s inputFile\n", argv[0]);
  73. return EXIT_FAILURE;
  74. }
  75. // Get samples
  76. std::string inputFilename(argv[1]);
  77. std::ifstream inputFile;
  78. inputFile.open(inputFilename.c_str());
  79. nlohmann::json samples;
  80. if (inputFile.is_open()) {
  81. inputFile >> samples;
  82. } else {
  83. printf("Unable to open file %s\n", inputFilename.c_str());
  84. return EXIT_FAILURE;
  85. }
  86. int n = samples["metadata"]["arraySize"];
  87. int* sampleData = (int*) malloc(sizeof(int) * n);
  88. nlohmann::json result;
  89. result["metadata"] = samples["metadata"];
  90. for (auto itr = samples.begin(); itr != samples.end(); ++itr) {
  91. if (itr.key() != "metadata") {
  92. auto sample = itr.value();
  93. // InsertionSort
  94. std::copy(sample.begin(), sample.end(), sampleData);
  95. pancakeSort(sampleData, n);
  96. for (int i = 0; i < n; i++) {
  97. result[itr.key()].push_back(sampleData[i]);
  98. }
  99. }
  100. }
  101. std::cout << result << '\n';
  102. }