内排序算法比较

// Datastructure1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<ctime>
#include<fstream>

using namespace std;
#define MAXSIZE 4000   //可排序表的最大长度
#define SORTNUM 6     //测试6中排序方法
//#define  max 1000     
/*
typedef struct node {
    int data3;
    int next;
} node;
*/
typedef long DataType[MAXSIZE+2];
DataType data;
DataType data2;
DataType R1;
int  size;//可排序表的长度
//int head;
//int fr[10];
//int re[10];
long compCount;//统计比较次数
long shiftCount;//统计移动次数

 void BeforeSort()//对比较次数和移动次数清零
 {
	 compCount=0;
	 shiftCount=0;
 }

bool Less(int i,int j)//若表中第i个元素小于第j个元素,则返回True,否则返回False
 {
	 compCount++;
	 return data[i]<data[j];
 }

 void Swap(int i,int j)//交换表中第i个和第j个元素
 {
	 int a;
	 a=data[i];
	 data[i]=data[j];
	 data[j]=a;
	 shiftCount=shiftCount+3;
 }

 void Shift(DataType &R,DataType &R2,int i,int j)//将R2[j]赋给R[i]
 {
	 R[i]=R2[j];
     shiftCount++;
 }

 void CopyData(DataType list1,DataType list2)
 {
	 int i;
	 for(i=1;i<=size;i++) list2[i]=list1[i];
	 
 }

 void InverseOrder()//将可排序表置为逆序
 {
	 int i,j;
	 for(i=1,j=size;i<=size/2;i++,j--)
	 {
		 int a;
     	 a=data[i];
	     data[i]=data[j];
		 data[j]=a;
	 }
	 CopyData(data,data2);
 }

 void RandomizeList()//由系统随机一组数
 {
	 int i;
	 //srand(time(0));
	 for(i=1;i<=size;i++)
         data[i]=rand()%(size+1);
	 CopyData(data,data2);	
	 ofstream out_stream;
	 out_stream.open("input.txt",ios::app);
	 if(out_stream.fail())
	 {
		 cout<<"input file opening failed.\n";
		 exit(1);
	 }
	 for(i=1;i<=size;i++) out_stream<<data[i]<<" ";
	 out_stream<<"\n********************************************\n";
	 out_stream.close(); 
 }

void RecallList()//恢复最后一次用RandomizeList随机打乱的可排序表
 {
	 CopyData(data2,data);
 }

 void output()//输出函数
 {
	 ofstream out_stream;
	 cout<<"\t"<<compCount<<"\t\t"<<shiftCount<<"\n";
	 out_stream.open("output.txt",ios::app);
	 if(out_stream.fail())
	 {
		 cout<<"Output file opening failed.\n";
		 exit(1);
	 }
	 out_stream<<"\t"<<compCount<<"\t\t"<<shiftCount<<"\n";
	 out_stream.close();	 
 }

 void BubbleSort()//冒泡排序
 {
	 BeforeSort();
	 int swapped,i,m;
	 m=size-1;
	 do{
		 swapped=0;
		 for(i=1;i<=m;i++)
		 {
			 if(Less(i+1,i)) 
			 {
				 Swap(i+1,i);
				 swapped=1;
			 }
		 }
		 m--;
	 }while(swapped);
	 output();
 }

 void InsertSort() //插入排序
 {
	 BeforeSort();
	 int i,j;
	 for(i=2;i<=size;i++)
	 {
		 Shift(data,data,0,i);
		 j=i-1;
		 while(Less(0,j)) 
		 {
			 Shift(data,data,j+1,j);
			 j--;
		 }
		 Shift(data,data,j+1,0);
	 }
	 output();
 }

 void SelectSort()//选择排序
 {
	 BeforeSort();
	 int i,j,min;
	 for(i=1;i<=size-1;i++)
	 {
		 min=i;
		 for(j=i+1;j<=size;j++)
			 if(Less(j,min)) min=j;
		 if(i!=min) Swap(i,min);
	 }
	 output();
 }

 long Partition(long low,long high)
 {
	 long pivotkey;
	 Shift(data,data,0,low);
	 pivotkey=data[low];
	 while(low<high)
	 {
		 compCount++;
		 while(low<high&&data[high]>=pivotkey)  {compCount++;--high;}
		 Shift(data,data,low,high);
		 compCount++;
		 while(low<high&&data[low]<=pivotkey)  {compCount++;++low;}
		 Shift(data,data,high,low);
	 }
	 Shift(data,data,low,0);
	 return low;

 }

 void QSort(long low,long high)//QuickSort的辅助函数
 {
	 long pivotloc;
	 if(low<high)
	 {
		 pivotloc=Partition(low,high);
		 QSort(low,pivotloc-1);
		 QSort(pivotloc+1,high);
	 }
 }

 void QuickSort()//快速排序
 {
	 BeforeSort();
	 QSort(1,size);
	 output();
 }

 void ShellSort()//希尔排序
 {
	 BeforeSort();
	 int i,j,h;
	 i=4;
	 h=1;
	 while(i<=size) 
	 {
		 i=i*2;
		 h=2*h+1;
	 }
	 while (h!=0)
	 {
		 i=h;
		 while(i<=size)
		 {
			 j=i-h;
			 while(j>0&&Less(j+h,j))
			 {
				 Swap(j,j+h);
				 j=j-h;
			 }
			 i++;
		 }
		 h=(h-1)/2;
	 }
	 output();
 }

 void Sift(int left,int right)//堆排序的调堆函数
 {
	 int i,j,finished=0;
	 i=left;
	 j=2*i;
	 Shift(data,data,0,left);
	 Shift(data,data,MAXSIZE+1,left);
	 while(j<=right&&!finished)
	 {
		 if(j<right&&Less(j,j+1)) j=j+1;
		 if(!Less(0,j)) finished=1;
		 else 
		 {
			 Shift(data,data,i,j);
			 i=j;
			 j=2*i;
		 }
	 }
	 Shift(data,data,i,MAXSIZE+1);
 }

 void HeapSort()//堆排序
 {
	 int left,right;
	 BeforeSort();
	 for(left=size/2;left>=1;left--) Sift(left,size);
	 for(right=size;right>=2;right--)
	 {
		 Swap(1,right);
		 Sift(1,right-1);
	 }
	 output();	 
 }

void Initialization()//系统初始化
{
	system("cls");//清屏
	cout<<"  *菜单*  \n"
		<<"  a.由系统随机产生待排序表 \n"
		<<"  b.返回主菜单  \n"
		<<"  c.退出程序  \n"
		<<"*************************************************\n"
		<<"请输入要执行的步骤:";
}

 void Interpret(char cmd)//调用各个算法
 {
	 int j,m;
	 ofstream out_stream;
	 out_stream.open("output.txt",ios::app);
	 if(out_stream.fail())
	 {
		 cout<<"Output file opening failed.\n";
		 exit(1);
	  }
	 switch(cmd)
	 {
	 case 'a':
		out_stream<<"各个算法的比较次数和移动次数如下:\n";
		out_stream<<"\t比较次数\t\t移动次数\n";
		out_stream.close();
		cout<<"请输入待排序表的长度:";
		int sizeInput;
		cin>>sizeInput;
		while(sizeInput>4000){
			cout<<"输入长度过大超界,请重新输入.\n";
				cin>>sizeInput;
		}
		size=sizeInput;
		cout<<"由系统随机产生待排序表的各个算法的比较次数和移动次数如下:\n";
		RandomizeList();
		for(m=0;m<3;m++)
		{	
			cout<<"*************************************************\n";
			if(m==0){cout<<"无序:\t";
				out_stream.open("output.txt",ios::app);
				out_stream<<"(无序)\n********************************************\n";
				out_stream.close();
			}
			if(m==1){cout<<"正序:\t";
				out_stream.open("output.txt",ios::app);
				out_stream<<"(正序)\n********************************************\n";
				out_stream.close();
			}
			if(m==2){cout<<"逆序:\t";
				InverseOrder();
				out_stream.open("output.txt",ios::app);
				out_stream<<"(逆序)\n********************************************\n";
				out_stream.close();
			} 
			for(int i=1;i<=size;i++) cout<<data[i]<<" ";
		    cout<<"\n";
		    cout<<"\t比较次数\t移动次数\n";
			for(j=0;j<SORTNUM;j++)
			{
				if(m==0||m==2){RecallList();}
				//for(int i=1;i<=size;i++) cout<<data[i]<<" ";
				out_stream.open("output.txt",ios::app);
				if(j==0) {cout<<"起泡排序: ";out_stream<<"起泡排序: ";out_stream.close();BubbleSort();}
				if(j==1) {cout<<"直接插入排序: ";out_stream<<"直接插入排序: ";out_stream.close();InsertSort();}
				if(j==2) {cout<<"简单选择排序: ";out_stream<<"简单选择排序: ";out_stream.close();SelectSort();}
				if(j==3) {cout<<"快速排序: ";out_stream<<"快速排序: ";out_stream.close();QuickSort();}
				if(j==4) {cout<<"希尔排序: ";out_stream<<"希尔排序: ";out_stream.close();ShellSort();}
				if(j==5) {cout<<"堆排序 : ";out_stream<<"堆排序 : ";out_stream.close();HeapSort();}
			}
		}
		
		 break;
	 case 'b':
		 Initialization();
		 break;
	 case 'c':
		 exit(0);
	 default:
		 cout<<"输入错误!\n";
		 break;
	 }
 }

 void main()
 {
	 Initialization();
	 char cmd;
	 do{
		 cin>>cmd;
		 Interpret(cmd);
	 }while(cmd!='c');
 }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值