Guest User

Untitled

a guest
Apr 7th, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<string>
  4. #include<vector>
  5.  
  6. using namespace std;
  7.  
  8. template <class T>
  9. void ShellSort(vector<T> &A)
  10. {
  11.     int n = A.size();
  12.     int h = 1;
  13.     while(h <= n)
  14.     {
  15.       h *= 3;
  16.       h++;      
  17.     }
  18.     while(h > 0)
  19.     {
  20.        for(int i = h;i < n;i++)
  21.        {
  22.           int j = i;
  23.           T bufor = A[j];
  24.           while((j >= h) && (A[j - h] > bufor))
  25.           {
  26.              A[j] = A[j - h];
  27.              j -= h;      
  28.           }
  29.           A[j] = bufor;
  30.        }
  31.        h /= 3;    
  32.     }
  33. }
  34.  
  35. template <class T>
  36. void heapify(vector<T> &A,int l,int r)
  37. {
  38.      int i,j;
  39.      T x;
  40.      bool flag;
  41.      x = A[l];
  42.      i = l;
  43.      j = 2 * i + 1;
  44.      flag = false;
  45.      while((j <= r) && (!flag))
  46.      {
  47.         if((j < r) && (A[j] < A[j + 1]))
  48.             j++;
  49.         if(x < A[j])
  50.         {
  51.              A[i] = A[j];
  52.              i = j;
  53.              j = 2 * i + 1;
  54.         }
  55.         else
  56.            flag = true;        
  57.      }
  58.      A[i] = x;
  59. }
  60.  
  61. template <class T>
  62. void heapSort(vector<T> &A)
  63. {
  64.      int n = A.size();
  65.      int i;
  66.      T x;
  67.      for(i = n/2 - 1;i >= 0;i--)
  68.          heapify(A,i,n - 1);
  69.      for(i = n - 1;i >= 1;i--)
  70.      {
  71.          x = A[0];
  72.          A[0] = A[i];
  73.          A[i] = x;
  74.          heapify(A,0,i - 1);  
  75.      }  
  76. }
  77.  
  78. template <class T>
  79. int HoarePartition(vector<T> &A,int p,int r)
  80. {
  81.     int i,j;
  82.     T x,temp;
  83.     x = A[p];
  84.     i = p - 1;
  85.     j = r + 1;
  86.     do
  87.     {
  88.         do
  89.         {
  90.             j--;
  91.         }
  92.         while(A[j] > x);
  93.         do
  94.         {
  95.             i++;      
  96.         }
  97.         while(A[i] < x);
  98.         if(i < j)
  99.         {
  100.              temp = A[i];
  101.              A[i] = A[j];
  102.              A[j] = temp;
  103.         }
  104.     }
  105.     while(i < j);
  106.     return j;
  107. }
  108.  
  109. template <class T>
  110. void quickSort(vector<T> &A)
  111. {
  112.      int n = A.size();
  113.      int p,q,r;
  114.      int top = -1;
  115.      vector<int> s;
  116.      if(n > 1)
  117.      {
  118.           top += 2;
  119.           s.push_back(0);
  120.           s.push_back(n - 1);
  121.           while(!s.empty())
  122.           {
  123.              r = s[top];
  124.              p = s[top - 1];
  125.              top -= 2;
  126.              s.pop_back();
  127.              s.pop_back();
  128.              while(p < r)
  129.              {
  130.                 q = HoarePartition(A,p,r);
  131.                 if(q - p + 1 < r - q)
  132.                 {
  133.                      top += 2;
  134.                      s.push_back(q+1);
  135.                      s.push_back(r);
  136.                      r = q;
  137.                 }
  138.                 else
  139.                 {
  140.                     top += 2;
  141.                     s.push_back(p);
  142.                     s.push_back(q);
  143.                     p = q + 1;
  144.                 }    
  145.              }
  146.           }              
  147.      }
  148. }
  149.  
  150. int main(int argc, char** argv)
  151. {
  152.     fstream fin;
  153.     fstream fout;
  154.     string temp;
  155.     vector<string> lines;
  156.     int k,n;
  157.     fin.open(argv[1],ios::in);
  158.     while(!fin.eof())
  159.     {
  160.        getline(fin,temp);
  161.        lines.push_back(temp);              
  162.     }
  163.     fin.close();
  164.     quickSort(lines);
  165.     fout.open(argv[2],ios::out);
  166.     n=lines.size();
  167.     for(k = 0;k < n;k++)
  168.          fout<<lines[k]<<endl;
  169.     fout.close();
  170.     while(!lines.empty())
  171.          lines.pop_back();
  172.    
  173.    return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment