6、排序算法比较(必做)(排序)
[问题描述]
利用随机函数产生10个样本,每个样本有50000个随机整数(并使第一个样本是正序,第二个样本是逆序),利用直接插入排序、希尔排序,冒泡排序、快速排序、选择排序、堆排序,归并排序、基数排序8种排序方法进行排序(结果为由小到大的顺序),并统计每一种排序算法对不同样本所耗费的时间。
[基本要求]
(1)原始数据存在文件中,用相同样本对不同算法进行测试;
(2)屏幕显示每种排序算法对
#include<iostream>
#include<time.h>
#include<assert.h>
#include<fstream>
#include<string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 50000
#define TEN 10
#define HUNDRED 100
using namespace std;
typedef int ElemType;
typedef int Status;
typedef struct SqQueue{
ElemType data;
struct SqQueue *next;
}LNode,Queue,SeqQueue,*QueuePtr;
typedef struct LQueue{
QueuePtr front;
QueuePtr rear;
}LQueue,LinkQueue;
clock_t start, stop; //clock_t是clock()函数返回的变量类型
double duration;
//(Queue部分
Status InitQueue(LQueue &Q){//1
Q.front = Q.rear = (LNode *)malloc(sizeof(LNode));
if(Q.front = NULL){
exit(0);
}
Q.front->next = NULL;
return OK;
}
Status EnQueue(LQueue &Q,ElemType e){//8
LNode *s =(LNode *)malloc(sizeof (LNode));
if(s == NULL) exit(-1);
s->data = e;
Q.rear->next = s;
Q.rear = s;
return OK;
}
Status DeQueue(LQueue &Q,ElemType &e){//9
if(Q.front == Q.rear) return ERROR;//Queue Empty
LNode *p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if(Q.rear == p){
Q.rear = Q.front;
}
free(p);
return OK;
}
Status QueueEmpty(LQueue Q){//4
if(Q.front == Q.rear)
return TRUE;
else
return FALSE;
}
//Queue部分)
//直接插入排序
void InsertSort(int array[], int n){
for (int i = 1; i < n; i++){
int temp = array[i];
int j = i - 1;
while (j >= 0 && array[j] > temp){
array[j + 1] = array[j];
j--;
}
array[j + 1] = temp;
}
}
//希尔排序
int D[26] = {1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
int B[26];
int bb = 0;
int temp;
void CreatD(int d[]){
for(int iii = sizeof(d)/4; iii >=0; iii--){
B[bb] = d[iii];
bb++;
}
}
void ShellInsert(int array[],int d){
for(int i = d+1;i<= sizeof(array)/4;i++){
if(array[i] < array[i-d]){
temp = array[i];
for(int j = i; j > d; j = j - d){
if(temp < array[j-d]){
array[j] = array[j-d];
}
else{
break;
}
}//end for j
}
}
}
void ShellSort(int array[],int d[],int t){
for(int k = 0; k < t; k++){
ShellInsert(array,d[k]);
}
}
//冒泡排序
void BubblingSort(int array[], int n){
int i, j, temp, m = 0;
int flag;
for (i = 0; i < n - 1; i++)
{
flag = 0;
for (j = n - 1; j > i; j--)
{
if (array[j - 1] > array[j])
{
temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
flag = 1;
}
}
if (flag == 0)break;
}
}
//快速排序
int Partition(int array[], int left, int right){
int i = left + 1;
int j = right;
int temp = array[left];
while (i <= j)
{
while (array[i] < temp)
{
i++;
}
while (array[j] > temp)
{
j--;
}
if (i < j)
swap(array[i++], array[j--]);
else i++;
}
swap(array[j], array[left]);
return j;
}
void QuickSort(int array[], int left, int right){
if (left > right)
return;
int j = Partition(array, left, right);
QuickSort(array, left, j - 1);
QuickSort(array, j + 1, right);
}
//选择排序
void SelectSort(int a[], int n)
{
int minindex, temp;
for (int i = 0; i < n - 1; i++)
{
minindex = i;
for (int j = i + 1; j < n; j++)
{
if (a[j] < a[minindex])
minindex = j;
}
temp = a[i];
a[i] = a[minindex];
a[minindex] = temp;
}
}
//堆排序
void AdjustDown(int array[], size_t n, int root){
size_t parent = root;
size_t child = parent * 2 + 1;
while (child < n)
{
if (child + 1 < n && array[child + 1] > array[child])
{
++child;
}
if (array[child] > array[parent])
{
swap(array[child], array[parent]);
parent = child;;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void HeapSort(int array[], size_t n){
assert(array);
int parent = (n - 2) >> 1;
for (; parent >= 0; --parent)
{
AdjustDown(array, n, parent);
}
for (int i = n - 1; i >= 0; --i)
{
swap(array[0], array[i]);
AdjustDown(array, i, 0);
}
}
//归并排序
void Merge(int S[],int T[],int low,int mid,int high) {
int i = low;//0
int j = mid +1;//3+1
int k = low;//0
while(i <= mid && j <= high){
if(S[i] <= S[j]){
T[k] = S[i];
i++;
k++;
}
else{
T[k] = S[j];
j++;
k++;
}
}
while(i <= mid){
T[k] = S[i];
i++;
k++;
}
while(j <= high){
T[k] = S[j];
j++;
k++;
}
}
void MSort(int A[],int Tmp[],int left,int right){
int cent;
if(left<right){
cent = (left + right)/2;
MSort(A,Tmp,left,cent);
MSort(A,Tmp,cent+1,right);
Merge(A,Tmp,left,cent+1,right);
}
}
void MergeSort(int A[],int N){
int *Tmp;
Tmp = (int*)malloc(N *sizeof(int));
if(Tmp != NULL){
MSort(A,Tmp,0,N-1);
free(Tmp);
}
else{
cout<<"Error"<<endl;
return;
}
}
//基数排序
int bucket[TEN];
int temp2[HUNDRED];
int MaxBit(int a[],int n){
int max = a[0];
for(int i=1;i<n;i++){
if(max < a[i])
max = a[i];
}
int d=1;
while(max >= TEN){
max /= TEN;
d++;
}
return d;
}
void RadixSort(int array[],int n){
int d = MaxBit(array,n);
int i,j,k;
int radix = 1;
for(i=1;i<=d;i++){
for(j=0;j<TEN;j++){
bucket[j]=0;
}
for(j=0;j<n;j++){
k=(array[j]/radix)%TEN;
bucket[k]++;
}
for(j = 1; j < TEN; j++)
bucket[j] = bucket[j - 1] + bucket[j];
for(j = n-1; j>=0; j--){
k = (array[j] / radix) % TEN;
temp2[bucket[k] - 1] = array[j];
bucket[k]--;
}
for(j = 0; j < n; j++)
array[j] = temp2[j];
radix = radix * TEN;
}
}
int main()
{
int i, j, temp, m = 0;
int a[MAXSIZE];
const char* fileName = "text6.txt";
fstream file;
file.open(fileName,ios::out);
if (file.fail()){
cout << "Open False" << endl;
exit(1);
}
cout << "生成"<< MAXSIZE<< "个随机数..."<< endl<<endl;
for (i = 0; i < MAXSIZE; i++){
a[i] = rand() % MAXSIZE;
file<<a[i]<<" "; //重复运行文件数据会被覆盖
}
start = clock(); // start time
InsertSort(a, MAXSIZE); //
stop = clock(); //stop time
duration = (double)(stop - start) / CLOCKS_PER_SEC;
cout<<"直接插入排序运行时间是:"<< duration<<"秒"<< endl;
for (i = 0; i < MAXSIZE; i++){
a[i] = rand() % MAXSIZE;
file<<a[i]<<" "; //重复运行文件数据会被覆盖
}
start = clock();
ShellSort(a,B,26);
stop = clock();
duration = (double)(stop - start) / CLOCKS_PER_SEC;
cout<<"希尔排序运行时间是: "<< duration<<"秒"<< endl;
for (i = 0; i < MAXSIZE; i++){
a[i] = rand() % MAXSIZE;
file<<a[i]<<" "; //重复运行文件数据会被覆盖
}
start = clock();
BubblingSort(a, MAXSIZE);
stop = clock();
duration = (double)(stop - start) / CLOCKS_PER_SEC;
cout<<"冒泡排序运行时间是: "<< duration<<"秒"<< endl;
for (i = 0; i < MAXSIZE; i++){
a[i] = rand() % MAXSIZE;
file<<a[i]<<" "; //重复运行文件数据会被覆盖
}
start = clock();
QuickSort(a , 0, MAXSIZE);
stop = clock();
duration = (double)(stop - start) / CLOCKS_PER_SEC;
cout << "快速排序运行时间是: " << duration <<"秒"<< endl;
for (i = 0; i < MAXSIZE; i++){
a[i] = rand() % MAXSIZE;
file<<a[i]<<" "; //重复运行文件数据会被覆盖
}
start = clock();
SelectSort(a, MAXSIZE);
stop = clock();
duration = (double)(stop - start) / CLOCKS_PER_SEC;
cout << "选择排序运行时间是: " << duration << "秒"<< endl;
for (i = 0; i < MAXSIZE; i++){
a[i] = rand() % MAXSIZE;
file<<a[i]<<" "; //重复运行文件数据会被覆盖
}
start = clock();
HeapSort(a, MAXSIZE);
stop = clock();
duration = (double)(stop - start) / CLOCKS_PER_SEC;
cout << "堆排序运行时间是: " << duration << "秒"<< endl;
for (i = 0; i < MAXSIZE; i++){
a[i] = rand() % MAXSIZE;
file<<a[i]<<" "; //重复运行文件数据会被覆盖
}
start = clock();
MergeSort(a, MAXSIZE);
stop = clock();
duration = (double)(stop - start) / CLOCKS_PER_SEC;
cout << "归并排序运行时间是: " << duration << "秒"<< endl;
for (i = 0; i < MAXSIZE; i++){
a[i] = rand() % MAXSIZE;
file<<a[i]<<" "; //重复运行文件数据会被覆盖
}
start = clock();
RadixSort(a,100);
stop = clock();
duration = (double)(stop - start) / CLOCKS_PER_SEC;
cout << "基数排序运行时间是: " << duration << "秒"<< endl;
file.close();
return 0;
}
不同样本所花的时间;