分析链接:http://blog.csdn.net/hguisu/article/details/7776068
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <windows.h>
#define cin fin
#define cout fout
using namespace std;
ifstream fin("data.in");
ofstream fout("data.out");
unsigned int compare, move; //元素比较次数compare,元素移动次数move
SYSTEMTIME start, stop;
//产生随机数
void Randomize(int, int[]);
//冒泡排序
template<class ElemType>
void BubbleSort(ElemType[], int);
//直接插入排序
template<class ElemType>
void InsertSort(ElemType[], int);
//简单选择排序
template<class ElemType>
void SelectionSort(ElemType[], int);
//快速排序
template<class ElemType>
void QuickSort(ElemType[], int);
//希尔排序
template<class ElemType>
void ShellSort(ElemType[], int[], int, int);
//堆排序
template<class ElemType>
void HeapSort(ElemType[], int);
//归并排序
template<class ElemType>
void MergeSort(ElemType[], int);
//元素交换
template<class ElemType>
void Swap(ElemType&, ElemType&);
//打印数组内容
template<class ElemType>
void Print( ElemType[], int );
//打印结果:元素移动、比较次数,算法耗用时间(second)
void PrintResult( char* sort);
//数组拷贝
template<class ElemType>
void CopyArray( ElemType[], ElemType[], int);
//产生随机数
void Randomize(int n, int data[])
{
srand((unsigned)time(NULL)); //使用系统定时计数器的值做为随机种子
for (int i = 0; i < n; i++)
data[i] = rand();
}
template<class ElemType>
void Swap( ElemType& a, ElemType& b)
{
ElemType c = a;
a = b;
b = c;
}
//数组拷贝
template<class ElemType>
void CopyArray( ElemType s[],ElemType d[],int n)
{
for (int i = 0; i < n; i++)
{
d[i] = s[i];
}
}
template<class ElemType>
void Print(ElemType data[],int n)
{
for (int i = 0; i < n; i++)
{
cout << setw(8) << data[i];
if ( i % 15 == 9) //每行输出10个元素
cout << endl;
}
cout << endl;
}
void PrintResult( char* sort)
{
cout << sort << ":" << endl;
cout << "元素比较次数为:" << compare << ",元素移动次数为:" << move << endl;
cout << "耗时: "
<< (((stop.wHour - start.wHour)*24*60000 + stop.wMinute - start.wMinute)*60000
+ (stop.wSecond - start.wSecond)*1000 + stop.wMilliseconds - start.wMilliseconds)/1000.0
<< "秒" << endl;
cout << "================================================================"<<endl;
}
//冒泡排序
template<class ElemType>
void BubbleSort(ElemType data[],int n)
{
compare = move = 0;
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;
move += 3;
}
compare++;
}
}
}
//直接插入排序
template<class ElemType>
void InsertSort(ElemType data[], int n)
{
compare = move = 0;
ElemType tmp;
int i, j;
for (i = 1; i < n; i++){
if ( data[i] > data[i - 1]){
compare++;
continue;
}
compare++;
tmp = data[i];
data[i] = data[i - 1];
for ( j = i - 1; j > 0 && data[j - 1] > tmp;j--){
data[j] = data[j - 1];
compare++;
move++;
}
data[j] = tmp;
compare++;
move += 2;
}
}
//简单选择排序
template<class ElemType>
void SelectionSort(ElemType data[], int n)
{
compare = move = 0;
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;
compare++;
}
Swap(data[i],data[min]);
move += 3;
}
}
//快速排序
template<class ElemType>
int Partition(ElemType data[] , int low , int high)
{
ElemType pivot = data[low];
while (low < high){
while (low < high && data[high] >= pivot){
high--;
compare++;
}
compare++;
data[low] = data[high];
while (low < high && pivot >= data[low]){
low++;
compare++;
}
compare++;
data[high] = data[low];
move += 2;
}
data[low] = pivot;
move += 2;
return low;
}
template<class ElemType>
void QuickSort(ElemType data[], int begin, int end)
{
if (begin >= end)
return;
int pivot = Partition(data , begin , end);
QuickSort(data , begin , pivot - 1);
QuickSort(data , pivot + 1, end);
}
template<class ElemType>
void QuickSort(ElemType data[], int n)
{
if (n < 2)
return;
compare = move = 0;
QuickSort(data, 0, n-1);
}
//Shell排序
template<class ElemType>
void ShellSort(ElemType a[], int n)
{
compare = move = 0;
int gap = n;
int i;
while (gap >= 1)
{
gap = gap/2;
for (i=0; i<gap; ++i)
{
ElemType e;
int t;
for (int j=i+gap; j<n; j++)
{
e = a[j];
t = j - gap;
while(e < a[t])
{
++compare;
a[t+gap] = a[t];
++move;
t = t - gap;
if (t <= i) break;
}
++compare;
a[t+gap] = e;
++move;
}
}
}
}
//堆排序
inline int LeftChild(int i)
{
return 2 * i + 1;
}
template<class ElemType>
void HeapAdjust(ElemType data[], int i, int n)
{
ElemType tmp;
int child;
for ( tmp = data[i]; LeftChild(i) < n; i = child){
child = LeftChild(i);
if(child != n - 1 && data[child + 1] > data[child]) //取较大的孩子结点
child++;
compare++;
compare++;
if(tmp < data[child]){
data[i] = data[child];
move++;
}
else
break;
}
data[i] = tmp;
move += 2;
}
template<class ElemType>
void HeapSort(ElemType data[], int n)
{
int i;
compare = move = 0;
for (i = n/2; i >= 0; i--) //建堆
HeapAdjust(data, i, n);
for (i = n - 1;i > 0; i--){ //将堆的根结点与最后的一个叶结点交换,并进行调整
Swap(data[0],data[i]);
move += 3;
HeapAdjust(data, 0, i);
}
}
//归并排序
template<class ElemType>
void Merge(ElemType data[], ElemType 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++];
compare++;
move++;
}
while (lptr <= leftEnd){
tmpArr[ptr++] = data[lptr++];
move++;
}
while (rptr <= rightEnd){
tmpArr[ptr++] = data[rptr++];
move++;
}
for (;i <= rightEnd; i++){
data[i] = tmpArr[i];
move++;
}
}
//递归实现
//将数组data中,[begin...end]的元素进行归并排序
template<class ElemType>
void MSort(ElemType data[], ElemType 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]进行归并
}
template<class ElemType>
void MergeSort(ElemType data[], int n)
{
ElemType* pArr = NULL;
pArr = new ElemType[n];
compare = move = 0;
MSort( data,pArr,0,n-1);
delete[] pArr;
}
#include "sortCmp.h"
int main()
{
int *pData,*bakData, length;
cout << "输入产生的随机数数目:"<< endl;
cin >> length;
pData = new int[length];
bakData = new int[length];
Randomize(length, pData);
cout << "待排序元素:" << endl;
Print( pData, length);
CopyArray( pData, bakData, length);
/*
GetLocalTime( &start);
BubbleSort( pData, length);
GetLocalTime( &stop);
PrintResult("冒泡排序");
CopyArray( bakData, pData, length);
GetLocalTime( &start);
InsertSort( pData, length);
GetLocalTime( &stop);
PrintResult("直接插入排序");
CopyArray( bakData, pData, length);
GetLocalTime( &start);
SelectionSort( pData, length);
GetLocalTime( &stop);
PrintResult("简单选择排序");*/
//CopyArray( bakData, pData, length);
GetLocalTime( &start);
QuickSort( pData, length);
GetLocalTime( &stop);
PrintResult("快速排序");
/*
CopyArray( bakData, pData, length);
GetLocalTime( &start);
ShellSort( pData, length);
GetLocalTime( &stop);
PrintResult("Shell排序");*/
CopyArray( bakData, pData, length);
GetLocalTime( &start);
HeapSort( pData, length);
GetLocalTime( &stop);
PrintResult("堆排序");
CopyArray( bakData, pData, length);
GetLocalTime( &start);
MergeSort( pData, length);
GetLocalTime( &stop);
PrintResult("归并排序");
delete[] bakData;
delete[] pData;
system("pause");
return 0;
}