Qucik3String.h
#pragma once
#include <string>
#include <memory>
class Quick3String
{
private:
std::string *row;
int size;
private:
int toIndex(const std::string& s, const int& pos)
{
if (pos < s.length())
return s[pos];
else
return -1;
}
public:
Quick3String(std::string s[],const int& n)
:row(s),size(n)
{
}
void sort()
{
sort(0, size - 1, 0);
}
void sort(const int& lo, const int& hi, const int& d)
{
if (hi <= lo) return;
int el = lo, eh = hi;
int i = lo + 1;
std::string dummy = row[lo];
int f = toIndex(dummy, d);
while (i <= hi)
{
int s = toIndex(row[i], d);
if (f > s)
std::swap(row[i++], row[el++]);
else if (f < s)
std::swap(row[i], row[eh--]);
else
i++;
}
sort(lo, el - 1, d);
if (f >= 0)
sort(el, eh, d + 1);
sort(eh + 1, hi, d);
}
};
main.cpp
#include <iostream>
#include <string>
#include "Quick3String.h"
using namespace std;
int main()
{
string s[13] =
{
"she",
"sells",
"seashells",
"by",
"the",
"shells",
"she",
"sells",
"she",
"sells",
"are",
"surely",
"seashells"
};
for (int i = 0; i < 13; ++i)
cout << s[i] << endl;
cout << "-----------" << endl;
Quick3String q3s(s, 13);
q3s.sort();
for (int i = 0; i < 13; ++i)
cout << s[i] << endl;
system("pause");
return 0;
}