#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int cnt,ci=0;
void shellsort(int r[],int n)
{
int i,j,gap;
char tmp;
gap=n;
while(gap>0)
{ for(i=gap;i<n;i++)
{
tmp=r[i];
j=i-gap;
while(j>=0&&tmp<r[j])
{
r[j+gap]=r[j];
j-=gap;
}
r[j+gap]=tmp;
int ii;
for( ii=0;ii!=9;ii++)
{
cout<<r[ii]<<' ';
}
cout<<endl;
}
gap/=2;
cout<<endl;
}
}
void select(int r[],int n)
{
int i,j,k;
int tmp;
for(i=0;i<=n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(r[j]<r[i])
{
k=j;
tmp=r[i];
r[i]=r[k];
r[k]=tmp;
}
}
}
void bubble(int r[],int n)
{
int i,j,exchang;
int tmp;
for(i=0;i<n-1;i++)
{
exchang=0;
for(j=n-1;j>i;j--)
if(r[j]<r[j-1])
{
tmp=r[j];
r[j]=r[j-1];
r[j-1]=tmp;
exchang=1;
}
if(exchang==0)
return ;
}
}
void quick(int r[],int zuo, int you)
{
int zi=zuo,yj=you;
int tmp;
if(zuo<you)
{
tmp=r[zuo];
while(zi!=yj)
{
while(yj>zi&&r[yj]>tmp)
yj--;
if(zi<yj)
{
r[zi]=r[yj];
zi++;
}
while(zi<yj&&r[zi]<tmp)
zi++;
if(zi<yj)
{
r[yj]=r[zi];
yj--;
}
r[zi]=tmp;
quick(r,zuo,zi-1);
quick(r,zi+1,you);
}
}
}
int sum=0;
void mergearray(int a[], int first, int mid, int last, int temp[])
{
cnt=0;
int i = first, j = mid + 1;
int m = mid, n = last;
int k = 0;
while (i <= m && j <= n)
{
if (a[i] < a[j])
{temp[k++] = a[i++]; }
else
{temp[k++] = a[j++]; cnt+=(j-i); }
}
while (i <= m)
temp[k++] = a[i++];
while (j <= n)
temp[k++] = a[j++];
for (i = 0; i < k; i++)
a[first + i] = temp[i];
//sum+=cnt;
// cout<<" "<<cnt<<endl;
}
void mergesort(int a[], int first, int last, int temp[])
{
if (first < last)
{
int mid = (first + last) / 2;
mergesort(a, first, mid, temp);
mergesort(a, mid + 1, last, temp);
mergearray(a, first, mid, last, temp);
}
}
void MergeSort(int a[], int n)
{
int *p = new int[n];
if (p == NULL)
return ;
mergesort(a, 0, n - 1, p);
delete[] p;
return ;
}
int w[5]={3,32,4,5,1};
int cmp(const int i,const int j)
{
return w[i]<w[j];
}
int main()
{
//int a[13]={38,26,97,66,25,17,8,50,11,45,32,41};
int b[10]={48,36,25,90,13,36,87,56,23,9};
int a[3]={2,11,5};
int sz;
char ha[12]={'Q','H','C','Y','P','A','M','S','R','D','F','X'};
sz=sizeof(a)/sizeof(int);
MergeSort(w,5);
cout<<cnt-1<<endl;
int r[5];
for(int i=0;i!=5;i++)
{
r[i]=i;
}
sort(r,r+5,cmp);
int i;
for( i=0;i!=5;i++)
{
cout<<w[i]<<' ';
}
cout<<endl;
for(i=0;i!=5;i++)
{
cout<<r[i]<<' ';
}
cout<<endl;
for(i=0;i!=5;i++)
cout<<w[r[i]]<<' ';
//cout<<" "<<cnt<<' ';
return 0;
}
排序
最新推荐文章于 2023-04-10 20:38:13 发布