C++における10種類の内部ソートアルゴリズムの比較分析
5070 ワード
C++における10種類の内部ソートアルゴリズムの比較分析
以上が本稿で述べたすべての内容であり,この10種類のソートアルゴリズムの習得に役立つことを期待する.
#include
#include
#include
using namespace std;
#define MAXSIZE 1000 //
#define SORTNUM 10 // 10
#define max 100 // ;
typedef struct node {
int data3;
int next;
} node;
typedef int 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]=pivotkey) {compCount++;--high;}
Shift(data,data,low,high);
compCount++;
while(low0&&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=1;left--) Sift(left,size);
for(right=size;right>=2;right--)
{
Swap(1,right);
Sift(1,right-1);
}
output();
}
void BInsertSort()//
{
BeforeSort();
int i,low,high,m,j;
for(i=2;i<=size;i++)
{
Shift(data,data,0,i);
low=1;
high=i-1;
while(low<=high)
{
m=(low+high)/2;
if(Less(0,m)) high=m-1;
else low=m+1;
}
for(j=i-1;j>=high+1;j--) Shift(data,data,j+1,j);
Shift(data,data,high+1,0);
}
output();
}
void Binsort()//2-
{
BeforeSort();
int i,k,j;
int first,last;
first=last=1;
Shift(R1,data,1,1);
for(i=2;i<=size;i++)
{
compCount++;
if(data[i]>=R1[1])
{
compCount++;
j=last;
while(j>=1&&R1[j]>data[i])
{
Shift(R1,R1,j+1,j);
j--;
compCount++;
}
Shift(R1,data,j+1,i);
last++;
}
else
{
first--;
if(first==0) first=size;
j=first+1;
compCount++;
while(j<=size&&R1[j]<=data[i])
{
Shift(R1,R1,j-1,j);
j++;
compCount++;
}
Shift(R1,data,j-1,i);
}
}
k=1;
j=first;
while(k<=size)
{
Shift(data,R1,k,j);
k++;
j=(j+1)%(size+1);
if(j==0) j=j+1;
}
output();
}
void Merge(int low,int m,int high)
{
int i=low,j=m+1,p=1;
while(i<=m&&j<=high)
{
if(Less(i,j)) Shift(R1,data,p++,i++);
else Shift(R1,data,p++,j++);
}
while(i<=m)
Shift(R1,data,p++,i++);
while(j<=high)
Shift(R1,data,p++,j++);
for(p=1,i=low;i<=high;p++,i++)
Shift(data,R1,i,p);
}
void MSort(int low, int high)
{
int mid;
if (low>size;
cout<>size;
cout<>data[i];
CopyData(data,data2);
out_stream<>cmd;
Interpret(cmd);
}while(cmd!=4);
}
以上が本稿で述べたすべての内容であり,この10種類のソートアルゴリズムの習得に役立つことを期待する.