binasort(折半插入排序).cpp

来自「折半插入排序 一种排序的方法」· C++ 代码 · 共 55 行

CPP
55
字号
// binasort(折半插入排序).cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<assert.h>

using namespace std;

typedef struct node
{
	int key;
	int oth;
}node;

void binasort(node r[],int n);
int _tmain(int argc, _TCHAR* argv[])
{
	  	node s[11];
	ifstream infile("D:\\1.txt");
	assert(infile);
	for(int i=1;i<11;i++)
		infile>>s[i].key;
	ofstream outfile("D:\\1.txt",ios_base::app);
	assert(outfile);
	
	binasort(s,10);
	outfile<<endl;
	for(int i=1;i<11;i++)
		outfile<<s[i].key<<' ';
	outfile<<endl;
	return 0;
}

void binasort(node r[],int n)
{
	int i,j;
	int l,h,mid;
	for(i=2;i<=n;i++)
	{
		r[0]=r[i];
		l=1;h=i-1;
		while(l<=h)
		{
			mid=(l+h)/2;
			if(r[0].key<r[mid].key) h=mid-1;
			else l=mid+1;
		}
		for(j=i-1;j>=l;j--)r[j+1]=r[j];
		r[l]=r[0];
	}
}

⌨️ 快捷键说明

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