AlgorithmD.cxx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // CycleSort implementation adapted from:
  2. // https://www.geeksforgeeks.org/cycle-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. #include <iostream>
  12. using namespace std;
  13. // Function sort the array using Cycle sort
  14. void cycleSort (int arr[], int n)
  15. {
  16. // count number of memory writes
  17. int writes = 0;
  18. // traverse array elements and put it to on
  19. // the right place
  20. for (int cycle_start=0; cycle_start<=n-2; cycle_start++)
  21. {
  22. // initialize item as starting point
  23. int item = arr[cycle_start];
  24. // Find position where we put the item. We basically
  25. // count all smaller elements on right side of item.
  26. int pos = cycle_start;
  27. for (int i = cycle_start+1; i<n; i++)
  28. if (arr[i] < item)
  29. pos++;
  30. // If item is already in correct position
  31. if (pos == cycle_start)
  32. continue;
  33. // ignore all duplicate elements
  34. while (item == arr[pos])
  35. pos += 1;
  36. // put the item to it's right position
  37. if (pos != cycle_start)
  38. {
  39. swap(item, arr[pos]);
  40. writes++;
  41. }
  42. // Rotate rest of the cycle
  43. while (pos != cycle_start)
  44. {
  45. pos = cycle_start;
  46. // Find position where we put the element
  47. for (int i = cycle_start+1; i<n; i++)
  48. if (arr[i] < item)
  49. pos += 1;
  50. // ignore all duplicate elements
  51. while (item == arr[pos])
  52. pos += 1;
  53. // put the item to it's right position
  54. if (item != arr[pos])
  55. {
  56. swap(item, arr[pos]);
  57. writes++;
  58. }
  59. }
  60. }
  61. // Number of memory writes or swaps
  62. // cout << writes << endl ;
  63. }
  64. // Driver program to test above function
  65. int main(int argc, char** argv)
  66. {
  67. if (argc != 2) {
  68. printf("Usage: %s inputFile\n", argv[0]);
  69. return EXIT_FAILURE;
  70. }
  71. // Get samples
  72. std::string inputFilename(argv[1]);
  73. std::ifstream inputFile;
  74. inputFile.open(inputFilename.c_str());
  75. nlohmann::json samples;
  76. if (inputFile.is_open()) {
  77. inputFile >> samples;
  78. } else {
  79. printf("Unable to open file %s\n", inputFilename.c_str());
  80. return EXIT_FAILURE;
  81. }
  82. int n = samples["metadata"]["arraySize"];
  83. int* sampleData = (int*) malloc(sizeof(int) * n);
  84. nlohmann::json result;
  85. result["metadata"] = samples["metadata"];
  86. for (auto itr = samples.begin(); itr != samples.end(); ++itr) {
  87. if (itr.key() != "metadata") {
  88. auto sample = itr.value();
  89. // InsertionSort
  90. std::copy(sample.begin(), sample.end(), sampleData);
  91. cycleSort(sampleData, n);
  92. for (int i = 0; i < n; i++) {
  93. result[itr.key()].push_back(sampleData[i]);
  94. }
  95. }
  96. }
  97. std::cout << result << '\n';
  98. }