C++折半插入排序

来源:互联网 发布:淘宝差评后售后退款 编辑:程序博客网 时间:2024/04/29 03:51
<pre name="code" class="cpp">//折半插入排序#include<iostream>using namespace std;void BinInsertSort(int a[],int len){int i,j,low,mid,high;int temp;for(i=1;i<len;i++){if(a[i-1]>a[i]){temp=a[i];low=0;high=i-1;while(low<=high){mid=(low+high)/2;if(temp<a[mid])high=mid-1;elselow=mid+1;}for(j=i-1;j>=high+1;j--)a[j+1]=a[j];a[j+1]=temp;}}}int main(){int a[]={2,5,-1,0,16,89,-32,-1,-4,90,0};int len=sizeof(a)/sizeof(a[0]);BinInsertSort(a,len);for(int i=0;i<len;i++){cout<<a[i]<<" ";}cout<<endl;system("pause");return 0;}


                                             
0 0
原创粉丝点击