折半插入排序.cpp

来自「共有10个文件代码」· C++ 代码 · 共 41 行

CPP
41
字号
#include<iostream.h>
#include<iomanip.h>
void Binsertsort(int A[],int n)
{
	int i,j,low,high,mid,x,p;
	for(i=1;i<n;i++)
	{
		low=0;high=i-1;
		x=A[i];
		while(low<=high)
		{
			mid=(low+high)/2;
			if(x<A[mid])
				high=mid-1;
			else
				low=mid+1;
		}
		p=low;
		for(j=i-1;j>=p;j--)
			A[j+1]=A[j];
		A[p]=x;
	}
}
void main()
{
	int A[10],s,n=10;
	cout<<"请输入要求排序的无序序列(10个数据):";
	for(s=0;s<n;s++)
		cin>>A[s];
	cout<<"折半插入排序"<<endl<<"排序前:";
	for(s=0;s<n;s++)
		cout<<setw(3)<<A[s];
	cout<<endl;
	Binsertsort(A,n);
	cout<<"排序后:";
	for(s=0;s<n;s++)
		cout<<setw(3)<<A[s];
	cout<<endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?