- 10
- 0
- 约6.66千字
- 约 7页
- 2018-03-14 发布于湖北
- 举报
数据结构上机作业 排序(简单排序 归并排序 堆排序 快速排序 插入排序)
/*数据结构排序算法 简单排序 快速排序 堆排序 插入排序 归并排序 此程序在DEV下测试成功*/
#include cstdlib
#include iostream
#define Maxlength 51
struct Compute //用于统计比较和交换的次数
{
int compareNum;
int swapNum;
};
typedef int LIST[Maxlength];
using namespace std;
typedef struct
{
LIST myheap;
int count;
}Heap;
Compute whole,whole1,Com;;
void CreateHeap(Heap temp)
{
temp.count=0;
}
bool HeapEmpty(Heap temp)
{
return (!temp.count);
}
bool HeapFull(Heap temp)
{
return (temp.count==Maxlength-1);
}
int GenerateNum() //随机生成1~99之间的一个数并返回
{
return rand()%99+1;
}
void Swap(int x,int y) //传址参数 交换两个整数
{
int temp;
temp=x;
x=y;
y=temp;
}
void Init(LIST temp)
{
temp[0]=-1; //引入temp[0],使其小于任何一个元素
for(int i=0;iMaxlength;i++)
temp[i]=GenerateNum();
}
void Show(LIST temp)
{
for(int i=1;iMaxlength;i++)
couttemp[i]\t;
coutendl;
}
void Result(Compute a)
{
coutpareNum\ta.swapNum\t;
}
//直接插入排序
void InsertionSort(LIST temp)
{
//Compute Com;
CpareNum=0;
Com.swapNum=0;
//cout直接插入排序:endl;
int j;
for(int i=1;iMaxlength;i++)
{
j=i;
while(temp[j]temp[j-1])
{
CpareNum++;
Com.swapNum++;
int a;
a=temp[j];
temp[j]=temp[j-1];
temp[j-1]=a;
j--;
}
CpareNum++;
}
//Show(temp);
Result(Com);
}
//简单选择排序
void SelectionSort(LIST temp)
{
//Compute Com;
CpareNum=0;
Com.swapNum=0;
//cout简单选择排序:endl;
int lowkey;
int lowindex;
for(int i=1;iMaxlength;i++)
{
lowindex=i;
lowkey=temp[i];
for(int j=i;jMaxlength;j++)
{
CpareNum++;
if(temp[j]lowkey)
{
lowkey=temp[j];
lowindex=j;
}
}
Com.swapNum++;
int a=temp[lowindex];
temp[lowindex]=temp[i];
temp[i]=a;
}
//Show(temp);
Result(Com);
}
//气泡排序
void BubbleSort(LIST te
您可能关注的文档
最近下载
- 2025年MK 袋鼠数学竞赛Level-C (5-6年级) 真题+解析.pdf
- 2025至2030年中国余压发电行业市场深度分析及发展前景预测报告.docx
- 安徽医科大学220级护理专业《细胞生物学》期末考试.docx VIP
- 五年级语文下册第二单元教学课件统编版新课标.pptx VIP
- 2023年天津外国语大学附属外国语学校小升初测试题 .pdf VIP
- 鼻中隔偏曲多学科决策模式中国专家共识(2025版).docx VIP
- 恒瑞医药股份有限公司财务风险及防范-会计-毕业论文.pdf VIP
- 《Python快速编程入门(第3版)》PPT完整全套教学课件.pptx VIP
- 园林绿化工程监理实施细则(公园与广场).docx VIP
- 最新部编版一年级语文下册全册学历案(2课时版).docx
原创力文档

文档评论(0)