欢迎使用Markdown编辑器写博客
想要实现多种排序算法,首先,要写一个排序类,包括多种排序算法
const int defaultSize = 10000;
class sort
{
protected:
int *data; //存放数组
int maxSize; //最大项数
int last; //当前已存表项的最后位置
void reSize(int newSize); //改变data数组空间大小
public:
sort(int sz = defaultSize); //构造函数
~sort(){delete[]data;} //析构函数
int Size()const{return maxSize;} //计算最大项数
int Length()const{return last+1;} //计算表长度
void input(); //输入
void output(); // 输出
void InsertSort(int data[], int n); //插入排序
void BubbleSort(int data[], int n); //冒泡排序
void SelectionSort(int data[], int n); //选择排序
void MSort(int data[], int tmpArr[], int begin, int end); //将数组data中,[begin...end]的元素进行归并排序
void MergeSort(int data[], int n); //归并排序
void Merge(int data[], int tmpArr[], int lptr, int rptr, int rightEnd); // 将数组data中,[lptr...rptr-1][rptr...rightEnd]两部分的元素进行合并
};
接着是类函数的实现:
#include "StdAfx.h"
#include "sort.h"
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
#include <time.h>
using namespace std;
//**************************************************************************************************************************
//函数名:sort
//参数:sz
//返回值:无
//函数功能:构造函数
//备注:构造函数
//**************************************************************************************************************************
sort::sort(int sz)
{
int *data = new int[sz];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < sz;i++ )
data[i]=rand() ;
}
//**************************************************************************************************************************
//函数名:reSize
//参数: newSize
//返回值:无
//函数功能:改变data数组的最大空间
//备注:私有
//**************************************************************************************************************************
void sort::reSize(int newSize)
{
if(newSize<=0)
{
cerr<<"!"<<endl;
return;
}
if(newSize!=maxSize)
{
int *newarray = new int[newSize];
if(newarray==NULL)
{
cerr<<"!"<<endl;
exit(1);
}
int n = last+1;
int * srcptr = data;
int * destptr = newarray;
while(n--) * destptr++ = * srcptr++;
delete[]data;
data = newarray;
maxSize = newSize;
}
}
//**************************************************************************************************************************
//函数名:input
//参数:无
//返回值:无
//函数功能:输入操作
//备注:
//**************************************************************************************************************************
void sort::input()
{
cout<<"开始排序,请输入排序元素个数:";
while(1){
cin>>last;
if(last<=maxSize-1)break;
cout<<"表中无素个数有误,范围不能超过"<<maxSize-1<<":";
}
for(int i=0;i<=last;i++)
{
cin>>data[i];
cout<<i+1<<endl;
}
}
//**************************************************************************************************************************
//函数名:output
//参数:无
//返回值:无
//函数功能:输出操作
//备注:
//**************************************************************************************************************************
void sort::output()
{
cout<<"正在输出结果:"<<endl;
for(int i=0;i<=last;i++)
{
cout<<data[i]<<endl;
}
}
//**************************************************************************************************************************
//函数名:InsertSort
//参数:data[], n
//返回值:无
//函数功能:插入排序
//备注:
//**************************************************************************************************************************
void sort::InsertSort(int data[], int n)
{
int tmp;
int i, j;
for (i = 1; i < n; i++){
if ( data[i] > data[i - 1])
continue;
tmp = data[i]; // 保存待插入的元素
data[i] = data[i - 1];
for ( j = i - 1; j > 0 && data[j - 1] > tmp;j--)
data[j] = data[j - 1]; // 元素后移
data[j] = tmp; // 插入到正确位置
}
}
//**************************************************************************************************************************
//函数名:BubbleSort
//参数:data【】,n
//返回值:无
//函数功能:冒泡排序
//备注:
//**************************************************************************************************************************
void sort::BubbleSort(int data[], int n)
{
int lastSwapIndex = n - 1; // 用于记录最后一次交换的元素下标
int i, j;
for (i = lastSwapIndex; i > 0;i = lastSwapIndex)
{
lastSwapIndex = 0;
for (j = 0; j < i; j++)
if (data[j] > data[j + 1]){
swap( data[j],data[j + 1]);
lastSwapIndex = j;
}
}
}
//**************************************************************************************************************************
//函数名:SelectionSort
//参数:data[], n
//返回值:无
//函数功能:选择排序
//备注:
//**************************************************************************************************************************
void sort::SelectionSort(int data[], int n)
{
int i, j, min;
for (i = 0; i < n; i++){
min = i;
for (j = i + 1; j < n; j++){
if ( data[j] < data[min])
min = j;
}
swap(data[i],data[min]);
}
}
//**************************************************************************************************************************
//函数名:MSort
//参数:data[], tmpArr[], begin, end
//返回值:无
//函数功能:将数组data中,[begin...end]的元素进行归并排序
//备注:
//**************************************************************************************************************************
void sort::MSort(int data[], int tmpArr[], int begin, int end)
{
int middle;
if ( begin >= end)
return;
middle = (begin + end)/2; // 将data平分为[begin..middle]和[middle..end]
MSort( data, tmpArr, begin, middle); // 递归前半部分
MSort( data, tmpArr, middle + 1, end); // 递归后半部分
Merge( data, tmpArr, begin, middle + 1, end); // 将data[begin..middle],data[middle..end]进行归并
}
//**************************************************************************************************************************
//函数名::MergeSort
//参数:data,n
//返回值:无
//函数功能:归并排序
//备注:
//**************************************************************************************************************************
void sort::MergeSort(int data[], int n)
{
int* pArr = NULL;
pArr = new int[n];
MSort( data,pArr,0,n-1);
delete[] pArr;
}
//**************************************************************************************************************************
//函数名:Merge
//参数:data[],tmpArr[], lptr, rptr, rightEnd
//返回值:无
//函数功能: 将数组data中,[lptr...rptr-1][rptr...rightEnd]两部分的元素进行合并
//备注:
//**************************************************************************************************************************
void sort::Merge(int data[], int tmpArr[], int lptr, int rptr, int rightEnd)
{
int leftEnd = rptr - 1;
int ptr,i;
ptr = i = lptr;
while (lptr <= leftEnd && rptr <= rightEnd)
if (data[lptr] <= data[rptr])
tmpArr[ptr++] = data[lptr++];
else
tmpArr[ptr++] = data[rptr++];
while (lptr <= leftEnd)
tmpArr[ptr++] = data[lptr++];
while (rptr <= rightEnd)
tmpArr[ptr++] = data[rptr++];
for (;i <= rightEnd; i++)
data[i] = tmpArr[i];
}
为了比较各个排序算法的时间,共产生20组10000个的随机数,四种排序算法各排五组,使用GetTickCount();函数计算五组数的时间,并求出平均值。当时时间有点急,计算平均时间的时候使用的笨办法。代码如下:
// SortClass.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
#include <time.h>
#include "sort.h"
using namespace std;
int main()
{
int max;
cout<<"请输入产生随机数的个数:";
cin>>max;
int *data1 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data1[i]=rand() ;
int *data2 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data2[i]=rand() ;
int *data3 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data3[i]=rand() ;
int *data4 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data4[i]=rand() ;
int *data5 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data5[i]=rand() ;
int *data6 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data6[i]=rand() ;
int *data7 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data7[i]=rand() ;
int *data8 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data8[i]=rand() ;
int *data9 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data9[i]=rand() ;
int *data10 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data10[i]=rand() ;
int *data11 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data11[i]=rand() ;
int *data12 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data12[i]=rand() ;
int *data13 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data13[i]=rand() ;
int *data14 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data14[i]=rand() ;
int *data15 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data15[i]=rand() ;
int *data16 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data16[i]=rand() ;
int *data17 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data17[i]=rand() ;
int *data18 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data18[i]=rand() ;
int *data19 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data19[i]=rand() ;
int *data20 = new int[max];
srand( (unsigned)time( NULL ) );
for( int i = 0; i < max;i++ )
data20[i]=rand() ;
sort paixu;
DWORD t1[20],t2[20],T1,T2;
t1[0]=GetTickCount();
paixu.InsertSort(data1,max);
t2[0]=GetTickCount();
t1[1]=GetTickCount();
paixu.InsertSort(data2,max);
t2[1]=GetTickCount();
t1[2]=GetTickCount();
paixu.InsertSort(data3,max);
t2[2]=GetTickCount();
t1[3]=GetTickCount();
paixu.InsertSort(data4,max);
t2[3]=GetTickCount();
t1[4]=GetTickCount();
paixu.InsertSort(data5,max);
t2[4]=GetTickCount();
cout<<"插入排序所用时间为:"<<((t1[0]+t1[1]+t1[2]+t1[3]+t1[4])/5-(t2[0]+t2[1]+t2[2]+t2[3]+t2[4])/5)*1.0/1000<<"毫秒"<<endl;
t1[5]=GetTickCount();
paixu.BubbleSort(data6,max);
t2[5]=GetTickCount();
t1[6]=GetTickCount();
paixu.BubbleSort(data7,max);
t2[6]=GetTickCount();
t1[7]=GetTickCount();
paixu.BubbleSort(data8,max);
t2[7]=GetTickCount();
t1[8]=GetTickCount();
paixu.BubbleSort(data9,max);
t2[8]=GetTickCount();
t1[9]=GetTickCount();
paixu.BubbleSort(data10,max);
t2[9]=GetTickCount();
cout<<"冒泡排序所用时间为:"<<((t1[5]+t1[6]+t1[7]+t1[8]+t1[9])/5-(t2[5]+t2[6]+t2[7]+t2[8]+t2[9])/5)*1.0/1000<<"毫秒"<<endl;
t1[10]=GetTickCount();
paixu.SelectionSort(data11,max);
t2[10]=GetTickCount();
t1[11]=GetTickCount();
paixu.SelectionSort(data12,max);
t2[11]=GetTickCount();
t1[12]=GetTickCount();
paixu.SelectionSort(data13,max);
t2[12]=GetTickCount();
t1[13]=GetTickCount();
paixu.SelectionSort(data14,max);
t2[13]=GetTickCount();
t1[14]=GetTickCount();
paixu.SelectionSort(data15,max);
t2[14]=GetTickCount();
cout<<"选择排序所用时间为:"<<((t1[10]+t1[11]+t1[12]+t1[13]+t1[14])/5-(t2[10]+t2[11]+t2[12]+t2[13]+t2[14])/5)*1.0/1000<<"毫秒"<<endl;
t1[15]=GetTickCount();
paixu.MergeSort(data16,max);
t2[15]=GetTickCount();
t1[16]=GetTickCount();
paixu.MergeSort(data17,max);
t2[16]=GetTickCount();
t1[17]=GetTickCount();
paixu.MergeSort(data18,max);
t2[17]=GetTickCount();
t1[18]=GetTickCount();
paixu.MergeSort(data19,max);
t2[18]=GetTickCount();
t1[19]=GetTickCount();
paixu.MergeSort(data20,max);
t2[
19]=GetTickCount();
cout<<"归并排序所用时间为:"<<((t1[15]+t1[16]+t1[17]+t1[18]+t1[19])/5-(t2[15]+t2[16]+t2[17]+t2[18]+t2[19])/5)*1.0/1000<<"毫秒"<<endl;
system("pause");
return 0;
}
t2[
19]=GetTickCount();
cout<<"归并排序所用时间为:"<<((t1[15]+t1[16]+t1[17]+t1[18]+t1[19])/5-(t2[15]+t2[16]+t2[17]+t2[18]+t2[19])/5)*1.0/1000<<"毫秒"<<endl;
system("pause");
return 0;
}