Advertisement
Solingen

fast_sort.cpp

Dec 11th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.87 KB | None | 0 0
  1. #include <fstream>
  2. #include <ctime>
  3. #include "string.h"
  4. #include "array.h"
  5.  
  6. using namespace std;
  7.  
  8. template <typename T>
  9. struct item
  10. {
  11.     int index;
  12.     T data;
  13.  
  14.     bool operator<(const item& other) const
  15.     {
  16.         #ifdef STABLE
  17.         return data < other.data || (data == other.data && index < other.index);
  18.         #else
  19.         return data < other.data;
  20.         #endif
  21.  
  22.     }
  23.  
  24.     bool operator>(const item& other) const
  25.     {
  26.         #ifdef STABLE
  27.         return data > other.data || (data == other.data && index > other.index);
  28.         #else
  29.         return data < other.data;
  30.         #endif
  31.  
  32.     }
  33. };
  34.  
  35. /*istream& operator>>(istream& in, item& item)
  36. {   in >> item.data;
  37.     return in;
  38. }
  39. ostream& operator<<(ostream& out, item& item)
  40. { out << "[" << item.index << "] " << item.data;
  41.     return out;
  42. }
  43. */
  44. template <typename T>
  45. istream& operator>>(istream& in, item<T>& item)
  46. {
  47.     in >> item.data;
  48.     return in;
  49. }
  50.  
  51. template <typename T>
  52. ostream& operator<<(ostream& out, const item<T>& item)
  53. {
  54.     out << "[" << item.index << "] " << item.data;
  55.     return out;
  56. }
  57.  
  58. template <typename T>
  59. void swap(T& a, T& b)
  60. {
  61.     T temp = a;
  62.     a = b;
  63.     b = temp;
  64. }
  65.  
  66.  
  67. template <typename T>
  68. int partition(Array<T>& arr, int low, int high, bool (*cmp)(T a, T b))
  69. {
  70.     T pivot = arr[low];
  71.     int i = low - 1;
  72.     int j = high + 1;
  73.  
  74.     while (true)
  75.     {
  76.        
  77.         do
  78.         {
  79.             i++;
  80.         } while ((cmp(arr[i], pivot)));
  81.  
  82.         do
  83.         {
  84.             j--;
  85.         } while ((cmp(pivot, arr[j])));
  86.  
  87.  
  88.         if (i >= j)
  89.         {
  90.             return j;
  91.         }
  92.  
  93.         ::swap(arr[i], arr[j]);
  94.     }
  95. }
  96.  
  97. template <typename T>
  98. void quicksort(Array<T>& arr, int low, int high, bool (*cmp)(T a, T b))
  99. {
  100.     if (low < high)
  101.     {
  102.         int pi = partition(arr, low, high, cmp);
  103.         quicksort(arr, low, pi, cmp);
  104.         quicksort(arr, pi + 1, high, cmp);
  105.     }
  106. }
  107.  
  108. template <typename T>
  109. bool Asc(T a, T b)
  110. {
  111.     return a < b;
  112. }
  113.  
  114. template <typename T>
  115. bool Desc(T a, T b)
  116. {
  117.     return a > b;
  118. }
  119.  
  120. template <typename T>
  121. void sort(Array<T>& arr, bool asc)
  122. {
  123.     quicksort(arr, 0, arr.size() - 1, asc ? Asc<T> : Desc<T> );
  124. }
  125.  
  126. template <typename T>
  127. Array<T> load_array(const String& filename)
  128. {
  129.     Array<T> arr;
  130.     ifstream in(filename.str);
  131.     if (!in)
  132.     {
  133.         cerr << "Error: File not found - " << filename.str << endl;
  134.         exit(1);
  135.     }
  136.     T value;
  137.     while (in >> value.data)
  138.     {
  139.         if (in.eof())
  140.         {
  141.             break;
  142.         }
  143.        value.index = arr.size();
  144.         arr.append(value);
  145.     }
  146.     return arr;
  147. }
  148.  
  149. template <typename T>
  150. Array<item<T>> load_array_pos(const String& posfilename)
  151. {
  152.     Array<item<T>> arr;
  153.     ifstream in(posfilename.str);
  154.     if (!in)
  155.     {
  156.         cerr << "Error: File not found - " << posfilename.str << endl;
  157.         exit(1);
  158.     }
  159.  
  160.     T value;
  161.     int index = 0;
  162.     while (in >> value)
  163.     {
  164.         if (in.eof())
  165.         {
  166.             break;
  167.         }
  168.         item<T> newItem;
  169.         newItem.data = value;
  170.         newItem.index = index++;
  171.         arr.append(newItem);
  172.     }
  173.  
  174.     return arr;
  175. }
  176.  
  177.  
  178. template <typename T>
  179. void print_array(const Array<T>& arr)
  180. {
  181.     for (int i = 0; i < arr.size(); i++)
  182.     {
  183.         if (i > 0) cout << " ";
  184.         cout << arr[i];
  185.     }
  186.     cout << endl;
  187. }
  188.  
  189. template <typename T>
  190. bool is_sorted(const Array<T>& arr, bool asc)
  191. {
  192.     for (int i = 1; i < arr.size(); i++)
  193.     {
  194.         if ((asc && arr[i] < arr[i - 1]) || (!asc && arr[i] > arr[i - 1]))
  195.         {
  196.             return false;
  197.         }
  198.         if (arr[i].data == arr[i - 1].data && arr[i].index < arr[i - 1].index)
  199.         {
  200.             cout << "По индексу:" << arr[i].data << "  Лежит:" << arr[i-1] << "  Должно:" << arr[i] << endl;
  201.             return false;
  202.         }
  203.     }
  204.     return true;
  205. }
  206.  
  207. int main(int argc, char* argv[])
  208. {
  209.     if (argc < 2)
  210.     {
  211.         cerr << "Usage: fastsort file [asc|desc] [-o] [-v]" << endl;
  212.         return 1;
  213.     }
  214.  
  215.     String posfilename = argv[1];
  216.     bool asc = true;
  217.     bool verbose = false;
  218.     String outputFile;
  219.  
  220.     for (int i = 2; i < argc; i++)
  221.     {
  222.         if (String(argv[i]) == "desc")
  223.         {
  224.             asc = false;
  225.         } else if (String(argv[i]) == "asc")
  226.         {
  227.             asc = true;
  228.         } else if (String(argv[i]) == "-o" && i + 1 < argc)
  229.         {
  230.             outputFile = argv[++i];
  231.         } else if (String(argv[i]) == "-v")
  232.         {
  233.             verbose = true;
  234.         } else
  235.         {
  236.             cerr << "Error: Unknown option - " << argv[i] << endl;
  237.             return 1;
  238.         }
  239.     }
  240.  
  241.     clock_t startTime = clock();
  242.     Array<item<int>> arr = load_array_pos<int>(posfilename);
  243.     clock_t loadTime = clock() - startTime;
  244.  
  245.     startTime = clock();
  246.     sort(arr, asc);
  247.     clock_t sortTime = clock() - startTime;
  248.  
  249.     if (outputFile.length() > 0)
  250.     {
  251.         ofstream out(outputFile.str);
  252.         if (!out)
  253.         {
  254.             cerr << "Error: Failed to open output file - " << outputFile.str << endl;
  255.             return 1;
  256.         }
  257.         for (int i = 0; i < arr.size(); i++)
  258.         {
  259.             out << arr[i].data << " ";
  260.         }
  261.         out << endl;
  262.     } else
  263.     {
  264.         print_array(arr);
  265.     }
  266.  
  267.     if (verbose)
  268.     {
  269.         cout << "Load time: " << static_cast<double>(loadTime) / CLOCKS_PER_SEC << " sec" << endl;
  270.         cout << "Sort time: " << static_cast<double>(sortTime) / CLOCKS_PER_SEC << " sec" << endl;
  271.         cout << "Memory used: " << arr.size() * sizeof(item<int>) / 1024.0 << " KB" << endl;
  272.         //cout << "Memory used: " << arr.size() * sizeof(item) / 1024.0 << " KB" << endl;
  273.     }
  274.  
  275.     if (!is_sorted(arr, asc))
  276.     {
  277.         print_array(arr);
  278.         return 1;
  279.     }
  280.  
  281.     return 0;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement