编译环境:vs2013
#include<iostream>
using namespace std;
#include<stdlib.h>
#include<time.h>
#include<fstream>
#define LOW 1
#define HIGH 1000000
#define SUCCESS 1
#define UNSUCESS 0
#define DUPLICATE -1
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 30002
#define LISTINCREAMENT 200
#define MEMBER 30
#define MAX_SIZE 200
typedef int Status;
typedef struct
{
int exchange;
int compare;
}SortO;
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *elem;
int Length;
int ListSize;
}SqList;
Status InitList_Sq(SqList &L)
{
L.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L.elem) return OVERFLOW;
L.Length = 0;
L.ListSize = LIST_INIT_SIZE;
return OK;
}
Status CreatList(SqList &L,int len)
{
ifstream file;
int num;
InitList_Sq(L);
file.open("随机数.txt", ios::in);
if (!file)
{
cout << "打开文件失败" << endl;
exit(0);
}
for (int i = 1; i <= len; i++)
{
file >> num;
if (!num)
{
i = i - 1;
continue;
}
L.elem[i] = num;
num = 0;
L.Length++;
if (L.Length + 1>L.ListSize)
{
L.elem = (ElemType*)realloc(L.elem, (L.ListSize + LISTINCREAMENT)*sizeof(ElemType));
if (!L.elem) return ERROR;
}
}
file.close();
return OK;
}
Status RandomNum()
{
ofstream file;
int num;
file.open("随机数.txt", ios::out);
if (!file)
exit(OVERFLOW);
srand((unsigned)time(NULL));
for (int i = 0; i < 100000; i++)
{
num = LOW + rand() % (HIGH - LOW + 1);
file << num << endl;
}
file.close();
return OK;
}
Status InserSort(SqList &L, SortO &O)
{
for (int i = 2; i <= L.Length; i++)
{
O.compare++;
if (L.elem[i] < L.elem[i - 1])
{
L.elem[0] = L.elem[i];
L.elem[i] = L.elem[i - 1];
O.exchange = O.exchange + 2;
int j = i - 2;
O.compare++;
while (L.elem[0]<L.elem[j])
{
O.exchange++;
L.elem[j+1] = L.elem[j];
j--;
}
L.elem[j + 1] = L.elem[0];
O.exchange++;
}
}
return OK;
}
Status BInsertSort(SqList &L, SortO &O)
{
int low, high,m;
for (int i = 2; i <= L.Length; i++)
{
L.elem[0] = L.elem[i];
O.exchange++;
low = 1, high = i - 1;
O.compare++;
while (low <= high)
{
m = (low + high) / 2;
O.compare++;
if (L.elem[0] < L.elem[m])
high = m - 1;
else
low = m + 1;
}
for (int j = i - 1; j >= high + 1; j--)
{
L.elem[j + 1] = L.elem[j];
O.exchange++;
}
L.elem[high + 1] = L.elem[0];
O.exchange++;
}
return OK;
}
Status BubbleSort(SqList &L, SortO &O)
{
for (int i = 1; i <= L.Length; i++)
{
for (int j = 1; j <= L.Length - i;j++)
{
O.compare = O.compare + 1;
if (L.elem[j] > L.elem[j + 1])
{
L.elem[0] = L.elem[j];
L.elem[j] = L.elem[j + 1];
L.elem[j + 1] = L.elem[0];
O.exchange += 3;
}
}
}
return OK;
}
int Partition(SqList &L, int low, int high, SortO &O)
{
int privotkey=0;
L.elem[0] = L.elem[low];
privotkey = L.elem[low];
O.exchange = O.exchange + 2;
O.compare++;
while (low < high)
{
O.compare += 4;
O.exchange += 2;
while (low < high&&L.elem[high] >= L.elem[0])
high--;
if (high == low)
continue;
L.elem[low] = L.elem[high];
while (low < high&&L.elem[low] <= L.elem[0])
low++;
if (high == low)
continue;
L.elem[high] = L.elem[low];
}
L.elem[low] = L.elem[0];
L.elem[0] = 0;
O.exchange++;
return low;
}
Status QSort(SqList &L, int l, int h, SortO &O)
{
int Partition(SqList &L, int low, int high, SortO &O);
if (l > h)
return OK;
int privotloc;
int m, n;
if (l < h)
{
privotloc = Partition(L, l, h, O);
m = privotloc - 1;
QSort(L, l, m,O);
n = privotloc + 1;
QSort(L, n, h,O);
}
return OK;
}
void QuickSort(SqList &L, SortO &O)
{
QSort(L, 1, L.Length,O);
}
Status SelectSort(SqList &L, SortO &O)
{
int k;
for (int i = 1; i < L.Length; i++)
{
k = i;
L.elem[0] = L.elem[i];
O.exchange++;
for (int j = i+1; j <= L.Length; j++)
{
O.compare++;
if (L.elem[0] > L.elem[j])
{
O.exchange++;
L.elem[0] = L.elem[j];
k = j;
}
}
if(k!=i)
{
O.exchange += 2;
L.elem[k] = L.elem[i];
L.elem[i] = L.elem[0];
}
}
return OK;
}
Status HeapAdjust(SqList &L, int s, int m, SortO &O)
{
int rc;
rc = L.elem[s];
O.exchange++;
for (int i = 2 * s; i <= m; i = i * 2)
{
O.compare += 2;
if (i < m && L.elem[i] < L.elem[i + 1])
i++;
if (rc >= L.elem[i])
break;
L.elem[s] = L.elem[i];
s = i;
O.exchange++;
}
L.elem[s] = rc;
O.exchange++;
return OK;
}
Status HeapSort(SqList &L, SortO &O)
{
int rc;
for (int i = L.Length / 2; i > 0; i--)
HeapAdjust(L, i, L.Length,O);
for (int i = L.Length; i > 1; --i)
{
O.exchange += 3;
rc = L.elem[1];
L.elem[1] = L.elem[i];
L.elem[i] = rc;
HeapAdjust(L, 1, i - 1,O);
}
return OK;
}
Status RadixSort(SqList &L, SortO &O)
{
int temp[11][23000];
int order[10]={0};
int k;
int n;
int p;
n = 1;
while (n <= 100000)
{
for (int i = 1; i <= L.Length; i++)
{
k = (L.elem[i] / n) % 10;
if (k < 0)
{
i = i - 1;
continue;
}
O.exchange+=2;
temp[k][order[k]] = L.elem[i];
order[k] = 1+order[k];
}
p = 1;
for (int i = 0; i < 10; i++)
{
O.compare++;
if (order[i] != 0)
{
for (int j = 0; j < order[i]; j++)
{
O.exchange++;
L.elem[p] = temp[i][j];
p++;
}
order[i] = 0;
}
}
n =n*10;
}
return OK;
}
void Print(SqList L)
{
for (int i = 1; i <= L.Length; i++)
{
cout << L.elem[i] << "\t";
}
cout << endl;
}
int main()
{
char ch;
int len;
SqList L,LBIS, LSIS, LBS, LQS, LSS, LHS, LRS;
SortO BIS, SIS, BS, QS, SS, HS, RS;
SIS.compare = SIS.exchange = BIS.compare = BIS.exchange = BS.compare
=BS.exchange = QS.compare = QS.exchange = SS.compare = SS.exchange
= HS.compare = HS.exchange = RS.compare = RS.exchange = 0;
cout << "\t\t\t\twelcome!!!" << endl;
cout << "是否要重新输入随机数(是Y,否N):" << endl;
cin >> ch;
if (ch == 'Y' || ch == 'y')
RandomNum();
cout << "请输入随机数的长度" << endl;
cin >> len;
CreatList(L, len);
CreatList(LBIS, len);
CreatList(LSIS, len);
CreatList(LBS, len);
CreatList(LQS, len);
CreatList(LSS, len);
CreatList(LHS, len);
CreatList(LRS, len);
cout << "排序数:" << endl;
Print( L);
cout << "插入排序:" << endl;
InserSort(LSIS, SIS);
Print(LSIS);
cout << "折半插入排序:" << endl;
BInsertSort(LBIS, BIS);
Print(LBIS);
cout << "起泡排序:" << endl;
BubbleSort(LBS, BS);
Print(LBS);
cout << "快速排序:" << endl;
QuickSort(LQS, QS);
Print(LQS);
cout << "选择排序:" << endl;
SelectSort(LSS, SS);
Print(LSS);
cout << "堆排序:" << endl;
HeapSort(LHS, HS);
Print(LHS);
cout << "基数排序:" << endl;
RadixSort(LRS, RS);
Print(LRS);
cout << endl;
cout << "插入排序的比较次数和交换次数为:" << SIS.compare << " " << SIS.exchange << endl;
cout << "折半插入的比较次数和交换次数为:" << BIS.compare << " " << BIS.exchange << endl;
cout << "起泡排序的比较次数和交换次数为:" << BS.compare << " " << BS.exchange << endl;
cout << "快速排序的比较次数和交换次数为:" << QS.compare << " " << QS.exchange << endl;
cout << "选择排序的比较次数和交换次数为:" << SS.compare << " " << SS.exchange << endl;
cout << "用堆排序的比较次数和交换次数为:" << HS.compare << " " << HS.exchange << endl;
cout << "基数排序的比较次数和交换次数为:" << RS.compare << " " << RS.exchange << endl;
cout << endl;
cout << "******************************特殊情况*******************************\n"<<endl;
SIS.compare = SIS.exchange = BIS.compare = BIS.exchange = BS.compare
= BS.exchange = QS.compare = QS.exchange = SS.compare = SS.exchange
= HS.compare = HS.exchange = RS.compare = RS.exchange = 0;
InserSort(LSIS, SIS);
BInsertSort(LBIS, BIS);
BubbleSort(LBS, BS);
QuickSort(LQS, QS);
SelectSort(LSS, SS);
HeapSort(LHS, HS);
RadixSort(LRS, RS);
cout << "插入排序的比较次数和交换次数为:" << SIS.compare << " " << SIS.exchange << endl;
cout << "折半插入的比较次数和交换次数为:" << BIS.compare << " " << BIS.exchange << endl;
cout << "起泡排序的比较次数和交换次数为:" << BS.compare << " " << BS.exchange << endl;
cout << "快速排序的比较次数和交换次数为:" << QS.compare << " " << QS.exchange << endl;
cout << "选择排序的比较次数和交换次数为:" << SS.compare << " " << SS.exchange << endl;
cout << "用堆排序的比较次数和交换次数为:" << HS.compare << " " << HS.exchange << endl;
cout << "基数排序的比较次数和交换次数为:" << RS.compare << " " << RS.exchange << endl;
system("pause");
return 0;
}
源码下载地址