Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<string>
- #include<vector>
- using namespace std;
- template <class T>
- void ShellSort(vector<T> &A)
- {
- int n = A.size();
- int h = 1;
- while(h <= n)
- {
- h *= 3;
- h++;
- }
- while(h > 0)
- {
- for(int i = h;i < n;i++)
- {
- int j = i;
- T bufor = A[j];
- while((j >= h) && (A[j - h] > bufor))
- {
- A[j] = A[j - h];
- j -= h;
- }
- A[j] = bufor;
- }
- h /= 3;
- }
- }
- template <class T>
- void heapify(vector<T> &A,int l,int r)
- {
- int i,j;
- T x;
- bool flag;
- x = A[l];
- i = l;
- j = 2 * i + 1;
- flag = false;
- while((j <= r) && (!flag))
- {
- if((j < r) && (A[j] < A[j + 1]))
- j++;
- if(x < A[j])
- {
- A[i] = A[j];
- i = j;
- j = 2 * i + 1;
- }
- else
- flag = true;
- }
- A[i] = x;
- }
- template <class T>
- void heapSort(vector<T> &A)
- {
- int n = A.size();
- int i;
- T x;
- for(i = n/2 - 1;i >= 0;i--)
- heapify(A,i,n - 1);
- for(i = n - 1;i >= 1;i--)
- {
- x = A[0];
- A[0] = A[i];
- A[i] = x;
- heapify(A,0,i - 1);
- }
- }
- template <class T>
- int HoarePartition(vector<T> &A,int p,int r)
- {
- int i,j;
- T x,temp;
- x = A[p];
- i = p - 1;
- j = r + 1;
- do
- {
- do
- {
- j--;
- }
- while(A[j] > x);
- do
- {
- i++;
- }
- while(A[i] < x);
- if(i < j)
- {
- temp = A[i];
- A[i] = A[j];
- A[j] = temp;
- }
- }
- while(i < j);
- return j;
- }
- template <class T>
- void quickSort(vector<T> &A)
- {
- int n = A.size();
- int p,q,r;
- int top = -1;
- vector<int> s;
- if(n > 1)
- {
- top += 2;
- s.push_back(0);
- s.push_back(n - 1);
- while(!s.empty())
- {
- r = s[top];
- p = s[top - 1];
- top -= 2;
- s.pop_back();
- s.pop_back();
- while(p < r)
- {
- q = HoarePartition(A,p,r);
- if(q - p + 1 < r - q)
- {
- top += 2;
- s.push_back(q+1);
- s.push_back(r);
- r = q;
- }
- else
- {
- top += 2;
- s.push_back(p);
- s.push_back(q);
- p = q + 1;
- }
- }
- }
- }
- }
- int main(int argc, char** argv)
- {
- fstream fin;
- fstream fout;
- string temp;
- vector<string> lines;
- int k,n;
- fin.open(argv[1],ios::in);
- while(!fin.eof())
- {
- getline(fin,temp);
- lines.push_back(temp);
- }
- fin.close();
- quickSort(lines);
- fout.open(argv[2],ios::out);
- n=lines.size();
- for(k = 0;k < n;k++)
- fout<<lines[k]<<endl;
- fout.close();
- while(!lines.empty())
- lines.pop_back();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment