吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1581|回复: 3
收起左侧

[C&C++ 转载] 快速排序——新手笔记

  [复制链接]
rain-xuan 发表于 2021-4-27 22:28
本帖最后由 rain-xuan 于 2021-4-28 15:05 编辑

快速排序算法初探

看图说话

说话

  1. 思想

    • 快速排序以我的理解就是先随意选择一个初始值,为了方便,一般是选择最左边的值,也就是图中的target.然后通过分割实现target的左边值都小于等于target,右边值都大于等于target.
  2. 具体实现步骤

    2.1 分割的实现

    • 确定了target之后,我们可以创建两个指针,left指针初始指向target+1的位置,right指针指向数组尾部
    • i<=j为前提。当以i为下标的数组元素大于target,并且以j为下标的数组元素小于target时,交换i,j指向的数组元素值
    • 如果上面后一句条件不成立,i++,j--
    • 如果i>=j时,交换target与j指向的数组元素。然后返回小标j
  3. 递归

    • 有了分割的思想,思考发现,可以用递归不断重复,直到数组有序。

代码

#include <iostream>
using namespace std;
int partition(int a[],int left,int right)
{
    int pivot = a[left],i = left+1,j=right;
    while(true)
    {
        while(i<=j&&a[i]<=pivot)
        {
            i++;
        }
        while(i<=j&&a[j]>=pivot)
        {
            j--;
        }
        if(i>=j)
        {
            break;
        }
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
    a[left] = a[j];
    a[j] = pivot;
    return j;
}

void QuickSort(int a[],int left,int right)
{
    if(left < right)
    {
        int target = partition(a,left,right);
        QuickSort(a,left,target-1);
        QuickSort(a,target+1,right);
    }
}

int main()
{
    int a[]={2,7,1,0,3,9};
    int left = 0;
    int right = sizeof(a)/sizeof(a[0]);
    for(int i=0;i<right;i++)
    {
        cout << a[i] << "\t";
    }
    cout << endl;
    QuickSort(a,left,right-1);
    for(int i=0;i<right;i++)
    {
        cout << a[i] << "\t";
    }
    cout << endl;
    return 0;
}

运行结果

运行截图


补充字符串排序

#include <iostream>
#include <string>
using namespace std;
int partition(string a[],int left,int right)
{
    string pivot = a[left];
    int i = left + 1,j = right;
    while(true)
    {
        while(i<=j&&a[i]<=pivot)
        {
            i++;
        }
        while(i<=j&&a[j]>=pivot)
        {
            j--;
        }
        if(i>=j)
        {
            break;
        }
        string temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
    a[left] = a[j];
    a[j] = pivot;
    return j;
}
void QuickSort(string a[],int left,int right)
{
    if(left < right)
    {
        int center = partition(a,left,right);
        QuickSort(a,left,center-1);
        QuickSort(a,center+1,right);
    }
}
int main()
{
    string str[] = {"3.txt","6.txt","a.md","1.pptx","7.docx"};
    int right = sizeof(str)/sizeof(str[0]);
    int left = 0;
    for(int i=0;i<right;i++)
    {
        cout << str[i] << "\t";
    }
    cout << endl;
    QuickSort(str,left,right-1);
    for(int i=0;i<right;i++)
    {
        cout << str[i] << "\t";
    }
    cout << endl;
    return 0;
}

运行结果

运行截图

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

drophair 发表于 2021-4-27 22:47
代码格式化一下吧,看的有点头晕
sjt5396 发表于 2021-4-28 06:34
 楼主| rain-xuan 发表于 2021-4-28 13:42
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-30 11:56

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表