排序排序排序

内部排序

1、插入排序

直接插入排序

#include <iostream>
using namespace std;


void InsertSort(SqList &L){  //对顺序表L作直接插入排序
	for(i=2;i<=L.length;++i)
	if(LT(L.r[i].key,L.r[i-1].key)){//需将L.r[i]插入有序子表
		L.r[0]=L.r[i];//复制为哨兵
		L.r[i]=L.r[i-1];
		for(j=i-2;LT(L.r[0].key,L.r[j].key);--j)
		L.r[j+1]=L.r[j];//记录后移
		L.r[j+1]=L.r[0];//插入到正确位置
	}
}

折半插入排序

void BInsertSort(SqList &L){//对顺序表L作折半插入排序
	
	for(i=2;i<=L.length;++i){
		L.r[0]=L.r[i];//将L。r[i]暂存到L.r[0]
		low=1;high=i-1;
		while(low<=high){//在r[low.high]中折半查找有序插入的位置
			m=(low+high)/2;//折半
			if(LT(L.r[i].key,L.r[i-1].key))//插入点在低半区
			high=m-1;
			else low=m+1;//插入点在高半区
		}
		for(j=i-1;j>=high+1;--j)//记录后移
		L.r[j+1]=L.r[j];
		L.r[high+1]=L.r[0];//插入
	}
	
}

表插入排序

#include <iostream>
using namespace std;


#define SIZE 100   //静态链表容量 
typedef struct{
	RcdType rc;//记录项 
	int next;//指针项 
}SLNode;//表结点类型 
typedef struct{
	SLNode r[SIZE];//0号单元为表头结点 
	int length;//链表当前长度 
}SLinkListType;//静态链表类型 

希尔排序

#include <iostream>
using namespace std;


void ShellSort(SqList &L,int dlta[],int t){//按增量序列dlta[0...t-1]对顺序表L作希尔排序 
	for(k=0;k<t;++k)
	ShellInsert(L,dlta[k]);//一趟增量为dlta[k]的插入排序 
}

void ShellInsert(SqList &L,int dk){//对顺序表L作一趟希尔插入排序
//前后记录位置的增量是dk,而不是1;r[0]只是暂存单元,不是哨兵,当j<=0时,插入位置已找到 
	for(i=dk+1;i<=L.length;++i)
	if(LT(L.r[i].key,L.r[i-dk].key)){//需将L.r[i]插入有序增量子表 
		L.r[0]=L.r[i];//暂存在L.r[0] 
		for(j=i-dk;j>0&&LT(L.r[0].key,L.r[j].key);j-=dk)
		L.r[j+dk]=L.r[j];//记录后移,查找插入位置 
		L.r[j+dk]=L.r[0];//插入 
	}
}

2、快速排序(是目前认为最好的一种内部排序方法)

#include <iostream>
using namespace std;


void QSort(SqList &L,int low,int high){//对顺序表L中子序列L.r[low..high]作快速排序 
	if(low<high){//长度大于1 
		pivotloc=Partition(L,low,high);//将L.r[low..high]一分为二 
		QSort(L,low,pivotloc-1);//对低子表递归排序,pivotloc时枢轴位置 
		QSort(L,pivotloc+1,high);//对高子表递归排序 
	}
} 

void QuickSort(SqList &L){//对顺序表L作快速排序 
	QSort(L,1,L.length);
}

3、选择排序

简单选择排序

#include <iostream>
using namespace std;


void SelectSort(SqList &L){//对顺序表L作简单选择排序 
	for(i=1;i<L.length;++i){//选择第i小的记录,并交换到位 
		j=SelectMinKey(L,i);//在L.r[i..L.length]中选择key最小的记录 
		if(i!=j)  L.r[i]<-->L.r[j];//与第i个记录交换 
	}
} 

树形选择排序(锦标赛排序)

堆排序

#include <iostream>
using namespace std;


void HeapSort(HeapType &H){//对顺序表H进行堆排序 
	for(i=H.length/2;i>0;--i)//把H.r[1..H.length]建成大顶堆 
	HeapAdjust(H,i,H.length);
	for(i=H.length;i>1;--i){
		H.r[1]<-->H.r[i];//将堆顶记录和当前未经排序子序列Hr[1..i]中 
		//最后一个记录相互交换 
		HeapAdjust(H,1,i-1);//将H.r[1..i-1]重新调整为大顶堆 
	}
} 

4、归并排序

#include <iostream>
using namespace std;


void MSort(RcdType SR[],RcdType &TR1[],int s,int t){//将SR[s..t]归并排序为TR1[s..t] 
	if(s==t)
	TR1[s]=SR[s];
	else{
		m=(s+t)/2;//将SR[s..m]平分为SR[s..m] 和SR[m+1..t] 
		MSort(SR,TR2,s,m);//递归的将SR[s..m]归并为有序的TR2[s..m] 
		MSort(SR,TR2,m+1,t);//递归的将SR[m+1..t]归并为有序的TR2[m+1..t] 
		Merge(TR2,TR1,s,m,t);//将TR2[s..m]和TR2[m+1..t]归并到TR1[s..t] 
		
	}
}

5、基数排序

链式基数排序

#define MAX_NUM_OF_KEY 8//关键字项数的最大值
#define RADIX 10//关键字基数,此时是十进制整数的基数
#define MAX_SPACE 10000
typedef struct{
KeysType keys[MAX_NUM_OF_KEY];//关键字
InFoType otheritems;//其他数据项
int next;
}SLCell;//静态链表的结点类型
typedef struct{
SLCell r[MAX_SPACE];//静态链表的可利用空间,r[0]为头结点
int keynum;//记录当前关键字个数
int recnum;//静态链表的当前长度
}SLList;//静态链表类型
typedef int ArrType[RADIX];指针数组类型

6、拓扑排序

拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列。且该序列必须满足下面两个条件:

1.每个顶点出现且只出现一次。
2.若存在一条从顶点 A 到顶点 B 的路径,那么在序列中顶点 A 出现在顶点 B 的前面。
 

#include"topological_sort.h"

Graph_DG::Graph_DG(int vexnum, int edge) {
    this->vexnum = vexnum;
    this->edge = edge;
    this->arc = new Vnode[this->vexnum];
    this->indegree = new int[this->vexnum];
    for (int i = 0; i < this->vexnum; i++) {
        this->indegree[i] = 0;
        this->arc[i].firstarc = NULL;
        this->arc[i].data = "v" + to_string(i + 1);
    }
}
//释放内存空间
Graph_DG::~Graph_DG() {
    ArcNode * p, *q;
    for (int i = 0; i < this->vexnum; i++) {
        if (this->arc[i].firstarc) {
            p = this->arc[i].firstarc;
            while (p) {
                q = p->next;
                delete p;
                p = q;
            }
        }
    }
    delete [] this->arc;
    delete [] this->indegree;
}
//判断我们每次输入的的边的信息是否合法
//顶点从1开始编号
bool Graph_DG::check_edge_value(int start, int end) {
    if (start<1 || end<1 || start>vexnum || end>vexnum) {
        return false;
    }
    return true;
}
void Graph_DG::createGraph() {
    int count = 0;
    int start, end;
    cout << "输入每条起点和终点的顶点编号(从1开始编号)" << endl;
    while (count != this->edge) {
        cin >> start;
        cin >> end;
        //检查边是否合法
        while (!this->check_edge_value(start, end)) {
            cout << "输入的顶点不合法,请重新输入" << endl;
            cin >> start;
            cin >> end;
        }
        //声明一个新的表结点
        ArcNode * temp = new ArcNode;
        temp->adjvex = end - 1;
        temp->next = NULL;
        //如果当前顶点的还没有边依附时,
        if (this->arc[start - 1].firstarc == NULL) {
            this->arc[start - 1].firstarc = temp;
        }
        else {
            ArcNode * now = this->arc[start - 1].firstarc;
            while(now->next) {
                now = now->next;
            }//找到该链表的最后一个结点
            now->next = temp;
        }
        ++count;
    }
}
void Graph_DG::print() {
    int count = 0;
    cout << "图的邻接矩阵为:" << endl;
    //遍历链表,输出链表的内容
    while (count != this->vexnum) {
        //输出链表的结点
        cout << this->arc[count].data<<" ";
        ArcNode * temp = this->arc[count].firstarc;
        while (temp) {
            cout<<"<"<< this->arc[count].data<<","<< this->arc[temp->adjvex].data<<"> ";
            temp = temp->next;
        }
        cout << "^" << endl;
        ++count;
    }
}

bool Graph_DG::topological_sort() {
    cout << "图的拓扑序列为:" << endl;
    //栈s用于保存栈为空的顶点下标
    stack<int> s;
    int i;
    ArcNode * temp;
    //计算每个顶点的入度,保存在indgree数组中
    for (i = 0; i != this->vexnum; i++) {
        temp = this->arc[i].firstarc;
        while (temp) {
            ++this->indegree[temp->adjvex];
            temp = temp->next;
        }

    }

    //把入度为0的顶点入栈
    for (i = 0; i != this->vexnum; i++) {
        if (!indegree[i]) {
            s.push(i); 
        }
    }
    //count用于计算输出的顶点个数
    int count=0;
    while (!s.empty()) {//如果栈为空,则结束循环
        i = s.top();
        s.pop();//保存栈顶元素,并且栈顶元素出栈
        cout << this->arc[i].data<<" ";//输出拓扑序列
        temp = this->arc[i].firstarc;
        while (temp) {
            if (!(--this->indegree[temp->adjvex])) {//如果入度减少到为0,则入栈
                s.push(temp->adjvex);
            }
            temp = temp->next;
        }
        ++count;
    }
    if (count == this->vexnum) {
        cout << endl;
        return true;
    } 
    cout << "此图有环,无拓扑序列" << endl;
    return false;//说明这个图有环
}
bool Graph_DG::topological_sort_by_dfs() {
    stack<string> result;
    int i;
    bool * visit = new bool[this->vexnum];
    //初始化我们的visit数组
    memset(visit, 0, this->vexnum);
    cout << "基于DFS的拓扑排序为:" << endl;
    //开始执行DFS算法
    for (i = 0; i < this->vexnum; i++) {
        if (!visit[i]) {
            dfs(i, visit, result);
        }
    }
    //输出拓扑序列,因为我们每次都是找到了出度为0的顶点加入栈中,
    //所以输出时其实就要逆序输出,这样就是每次都是输出入度为0的顶点
    for (i = 0; i < this->vexnum; i++) {
        cout << result.top() << " ";
        result.pop();
    }
    cout << endl;
    return true;
}
void Graph_DG::dfs(int n, bool * & visit, stack<string> & result) {

        visit[n] = true;
        ArcNode * temp = this->arc[n].firstarc;
        while (temp) {
            if (!visit[temp->adjvex]) {
                dfs(temp->adjvex, visit,result);
            }
            temp = temp->next;
        }
        //由于加入顶点到集合中的时机是在dfs方法即将退出之时,
        //而dfs方法本身是个递归方法,
        //仅仅要当前顶点还存在边指向其他不论什么顶点,
        //它就会递归调用dfs方法,而不会退出。
        //因此,退出dfs方法,意味着当前顶点没有指向其他顶点的边了
        //,即当前顶点是一条路径上的最后一个顶点。
        //换句话说其实就是此时该顶点出度为0了
        result.push(this->arc[n].data);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风铃奈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值