三向字符串快速排序(c++ 版)

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;
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值