数据结构上机作业 排序简单排序 归并排序 堆排序 快速排序 插入排序.doc
文本预览下载声明
/*数据结构排序算法 简单排序 快速排序 堆排序 插入排序 归并排序 此程序在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
显示全部