算法复习之快排(C++版)

本文深入探讨了快速排序算法的两种实现方式:递归与非递归版本。通过具体代码示例,详细解释了每种方法的工作原理,并展示了如何在 C++ 中实现这些算法。此外,还介绍了快速排序算法的基本思想及其在实际应用中的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<memory>
#include<vector>
#define MAX 100
using namespace std;
int src[MAX];
void init(){
    memset(src,0,MAX);
    for(int i=0;i<10;i++){
        src[i]  = i+1;
    }
}
void swap(int i,int j){
    int tmp = src[i];
    src[i] = src[j];
    src[j] = tmp;
}
/**
 * 递归的快排
 */ 
void quickSort(int start,int end){
    if(start>=end||end<0){
        return ;    
    }
    int l=start+1, r = end;
    while(l<r){
        while(l<r&&src[start] > src[l])l++;
        if(src[start] < src[l]){
            while(r>l&&src[start] < src[r])r--;
            if(src[start] > src[r]){
                swap(l,r);
                l++;r--;
            }
        }
    } 
    if(src[start] > src[l]){
        swap(start,l);
    }else{
        l=l-1;
        swap(start,l);
    }
    quickSort(start,l-1);
    quickSort(l+1,end);
}
/**
 * 非递归的快排
 **/ 
void quickSort2(int*src , int start,int n){

    vector< pair<int,int>* > s;
    s.push_back(new pair<int,int>(start,n));
    while(s.size()>0){
        cout<<s.size()<<endl;
        pair<int,int>* tmp = s.back();
        s.pop_back();
        int l = tmp->first,r = tmp ->second;
        start = l;
        l++;
        if(l>=r||r<0){
            continue;
        }
        while(l<r){
            while(l<r&&src[start] > src[l])l++;
            if(src[start] < src[l]){
                while(r>l&&src[start] < src[r])r--;
                if(src[start] > src[r]){
                    swap(l,r);
                    l++;r--;
                }
            }
        } 
        if(src[start] > src[l]){
            swap(start,l);
        }else{
            l=l-1;
            swap(start,l);
        }
        s.push_back(new pair<int,int>(tmp->first,l-1));
        s.push_back(new pair<int,int>(l+1,tmp->second));
        delete tmp;
    }
}
int main(){

    init();
    quickSort2(src,0,10);
    for(int i=0;i<11;i++){
        cout<<src[i]<<" "; 
    }
    cout<<endl;
    pair<int,int > test(1,2);
    cout<< test.first<<endl;
    cout<< test.second<<endl;
    return 0;
}
### C++ 数据结构与算法期末复习要点 #### 二叉树定义 在C++中,二叉树节点通常通过`struct`来定义。每个节点包含三个部分:存储数据的数据成员以及指向左子树和右子树的两个指针成员[^1]。 ```cpp typedef struct BiTNode { TElemType data; // 结点数据域 struct BiTNode *lchild, *rchild; // 左右孩子指针 } BiTNode, *BiTree; ``` 此段代码展示了如何声明一个简单的二叉链表形式表示的二叉树类型,在实际应用时可以根据需求调整其中的具体字段名称或增加其他辅助属性。 #### 数据逻辑结构分类 了解不同类型的逻辑结构对于掌握各种复杂度分析至关重要。常见的有: - **集合结构**:元素间无序且唯一; - **线性结构**:如数组、栈、队列等,具有顺序特性; - **树形结构**:层次化的组织方式,比如文件系统的目录结构; - **图状结构**:由顶点集和边构成的关系网; 这些基本概念有助于理解更高级别的抽象模型及其操作方法[^2]。 #### 排序算法实现——快速排序 作为一种高效的内部比较类排序方法,快排利用分治策略递归处理待排序序列直到整个列表有序为止。下面给出了一种基于数组本的实现方案[^3]: ```cpp #include <iostream> using namespace std; const int N = 100010; int q[N]; void quick_sort(int q[], int l, int r) { if (l >= r) return; int i = l - 1, j = r + 1, x = q[(l + r) >> 1]; while (i < j) { do i++; while (q[i] < x); do j--; while (q[j] > x); if (i < j) swap(q[i], q[j]); } quick_sort(q, l, j); quick_sort(q, j + 1, r); } ``` 这段程序实现了经典的荷兰国旗问题解决方案,并能有效地减少不必要的交换次数从而提高效率。 #### 单链表分割函数解析 针对单向链接列表的操作也是考试重点之一。这里提供了一个用于将原链表按中间位置断开成两部分的新函数实例[^4]: ```cpp void fun1(LinkNode*& L1, LinkNode*& L2) { int n = 0, i; LinkNode* p = L1; while (p != NULL) { n++; p = p->next; } p = L1; for (i = 1; i < n / 2; i++) p = p->next; L2 = p->next; p->next = NULL; } ``` 该过程首先遍历计算总长度n,接着再次迭代至一半处切断连接形成新的头指针L2。 #### 哈希表构建案例研究 当涉及到散列表设计题目时,则需考虑冲突解决机制的选择。例如给定一组键值并指定模数作为桶数量的情况下建立相应的映射关系[^5]: | 关键字 | H(key)=key%11 | |--------|---------------| | 19 | 8 | | 01 | 1 | | 23 | 1 | | ... | ... | 上述表格仅列举了几项样本输入对应的索引位置,具体实现还需根据实际情况选用合适的拉链法或者开放地址法定位溢出记录的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值