|
常用链接
留言簿(1)
随笔分类
随笔档案
文章分类
文章档案
相册
搜索
最新评论

Powered by: 博客园
模板提供:沪江博客
|
|
|
|
|
发新文章 |
|
|
大二下学期学习了数据结构,期末复习考试时,我曾经为了复习重写了代码,姑且贴上来,希望对需要的人有帮助,其中各种排序的名字在函数中有表现,在此不再赘述。
void bullsort(int*a,int len)
  {
for(int i=0;i<len;++i)
for(int j=0;j<len-i-1;++j)
if(a[j]>a[j+1])
 {
int temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
void searchsort(int*a,int len)
  {
for(int i=0;i<len;++i)
 {
int k=i;
for(int j=i+1;j<len;++j)
if(a[j]<a[k])
 {
k=j;
}
int temp=a[i];
a[i]=a[k];
a[k]=temp;
}

}
void insertsort(int*a,int len)
  {
for(int i=1;i<len;++i)
 {
int temp=a[i];
int j=i-1;
while(j>=0&&temp<a[j])
 {
a[j+1]=a[j];
--j;

}
a[j+1]=temp;
}
}

void qksort(int*a,int low,int high)
  {
int i=low,j=high;
if(low<high)
 {
int temp=a[i];
while(i<j)
 {
while(i<j&&a[j]>temp)
--j;
if(i<j)
 {
a[i]=a[j];
++i;
}
while(i<j&&a[i]<temp)
++i;
if(i<j)
 {
a[j]=a[i];
--j;
}
}
a[i]=temp;
qksort(a,low,i-1);
qksort(a,i+1,high);
}
}

void shellsort(int*a,int len,int d)
  {
while(d>0)
 {
for(int i=d;i<len;i+=d)
 {
int temp=a[i];
int j=i-d;
while(j>=0&&temp<a[j])
 {
a[j+d]=a[j];
j-=d;

}
a[j+d]=temp;
}
d=d/2;
}
}

void makeheap(int*b,int low,int high)
  {
int i=low,j=2*i;
int temp=b[i];
while(j<=high)
 {
if(j<high&&b[j+1]>b[j])
++j;
if(temp<b[j])
 {
b[i]=b[j];
i=j;
j=2*i;
}
else
break;
}
b[i]=temp;
}
void heapsort(int* b,int high)
  {
int i;
for(i=high/2;i>0;--i)
makeheap(b,i,high);
copy(b+1,b+high,ostream_iterator<int>(cout," "));
cout<<endl;
for (int i=high-1;i>=2;--i)
 {
int temp=b[1];
b[1]=b[i];
b[i]=temp;
makeheap(b,1,i-1);

}
}
//int _tmain(int argc, _TCHAR* argv[])
//{
// int a[]={11,0,9,7,2,10,3};
// int len=7;
// int len2=8;
// int d=len2/2;
// int b[8];
// b[0]=0;
// for(int i=1;i<8;++i)
// b[i]=a[i-1];
// heapsort(b,len2);
//copy(b+1,b+len2,ostream_iterator<int>(cout," "));
//
//}
|
|