三九宝宝网宝宝教育教学论文

C语言怎么用二分查找插入排序

01月02日 编辑 39baobao.com

[用二分法求方程解]/*算法:1、输入有根区间两端点a、X1和精度 2、计算x=(b+a)/2 3、若f(b)*f(x)<0,则x0=x,转2,否则x1=x,转2 4、若|b-a|&lt;精度,则输出根x,结束。否则转2*/ #include<stdio.h> #includ...+阅读

一般来说,插入排序都采用in-place在数组上实现。具体算法描述如下: 1. 从第一个元素开始,该元素可以认为已经被排序 2. 取出下一个元素,在已经排序的元素序列中从后向前扫描 3. 如果该元素(已排序)大于新元素,将该元素移到下一位置 4. 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置 5. 将新元素插入到下一位置中 6. 重复步骤2 如果比较操作的代价比交换操作大的话,可以采用二分查找法来减少比较操作的数目。该算法可以认为是插入排序的一个变种,称为二分查找排序。

输入参数中,需要排序的数组为array[],起始索引为first,终止索引为last。示例代码的函数采用in-place排序,调用完成后,array[]中从first到last处于升序排列。

void insertion_sort(int array[], unsigned int first, unsigned int last)

{ int i,j;

int temp;

for (i = first+1; i<=last;i++)

{ temp = array[i]; j=i-1; //与已排序的数逐一比较, 大于temp时, 该数移后

while((j>=first) & (array[j] > temp))

{ array[j+1] = array[j];

j--;

}

array[j+1] = temp;

}

}

这个更好:

void InsertSort(char array[],unsigned int n)

{ int i,j;

int temp;

for(i=1;i

{

temp = array[i];//store the original sorted array in temp

for(j=i ; j>0 & temp < array[j-1] ; j--)//compare the new array with temp

{

array[j]=array[j-1];//all larger elements are moved one pot to the right

}

array[j]=temp;

}

}

以下为关联文档:

c语言二分法求解方程程序#include "stdio.h" #define MAX 100 typedef struct node { int key; }NODE; int binsearch(NODE r[MAX],int k,int n) { int t=1,h=n,m; while(t<=h) { m=(t+h)/2; if(k==r[...

C语言采用二分法迭代求方程的解的程序#include #include double f(double x){ return ( x*x+6*x+225);/*这里是那个函数 好像在00?(y1-y2):-(y1-y2); return ( t>0.001 ? 0 :1 ); } double solve(){ double y1=65...

编个C语言程序用二分法求方程x^3x^4 4x^21 0在区间0 1内的根#include #include double f(double d) { return pow(d,3)+4*d-10; } void main() { int k=0;double a,b,limit; printf("\nplease input the 区间:"); scanf(""%lf %lf",&a,&b); p...

C语言编程题:初始化一个已排序的整型数组用二分法查找其中是#include <stdio.h> #include <string.h> void Sort(int a[],int n) { int i,j,k; int num; for(i = 0; i < n - 1; ++i) { k = i; for(j = i + 1; j < n; ++j) if(a[k] > a...

word2003的2个问题:1用插入尾注的方法引用参考文献后本人推荐一种简单方法:尾注法 方法如下(以Word2003为例): 1.光标移到要插入参考文献的地方,菜单中“插入”——“引用”-“脚注和尾注”。 2.对话框中选择“尾注”,编号方式选“自...

C语言中字符插入字符串函数#include#include//方便在控制台打印中英文混合字符 int main() { char s[]="1234.5678"; int i=0; char*p=s; for(i=11;i>=4;i--)//第二个数字2后的字符整体后移2位以便最后...

几何画板文件怎么插入ppt中楼上两位的做法都失去了几何画板的优点,这样就只能当图片用了, 正确做法是插入 “几何画板控件” 这个你自己搜索下载(实在找不到PM我我给你发) 几何画板控件下载及使用说明 1....

ppt中如何插入几何画板首先安装几何画板控件进行环境Vb6dll,之后安装Active X几何画板控件,控件文件名称setup。这两种软件在网上很容易就可下载得到。 ①利用Active x几何画板控件插入 Powerpoint...

C语言编程二分#include <math.h> #include <stdio.h> double fun(double x) { return 2 * x * x * x - 4 * x * x + 3 * x - 6; } double root(double a, double b, double e) { double x...

推荐阅读
图文推荐