帝国网站如何做中英文切换,详情页设计原则,做建材上哪个网站比较好,济南市建设招标中心网站1、二分查找
二分查找又叫折半查找#xff0c;要求待查找的序列有序。每次取中间位置的值与待查关键字比较#xff0c;如果中间位置 的值比待查关键字大#xff0c;则在前半部分循环这个查找的过程#xff0c;如果中间位置的值比待查关键字小#xff0c; 则在后半部分循环…1、二分查找
二分查找又叫折半查找要求待查找的序列有序。每次取中间位置的值与待查关键字比较如果中间位置 的值比待查关键字大则在前半部分循环这个查找的过程如果中间位置的值比待查关键字小 则在后半部分循环这个查找的过程。直到查找到了为止否则序列中没有待查的关键字。 示例
public static int biSearch(int []array,int a){int lo0;int hiarray.length-1;int mid;while(lohi){mid(lohi)/2;//中间位置if(array[mid]a){return mid1;}else if(array[mid]a){ //向右查找lomid1;}else{ //向左查找himid-1;}}return -1;}2、冒泡排序算法
1比较前后相邻的二个数据如果前面数据大于后面的数据就将这二个数据交换。 2这样对数组的第 0 个数据到 N-1 个数据进行一次遍历后最大的一个数据就“沉”到数组第 N-1 个位置。
3NN-1如果 N 不为 0 就重复前面二步否则排序完成。
public static void bubbleSort1(int[] a, int n) {int i, j;for (i 0; i n; i) {//表示 n 次排序过程for (j 1; j n - i; j) {if (a[j - 1] a[j]) {//前面的数字大于后面的数字就交换//交换 a[j-1]和 a[j]int temp;temp a[j - 1];a[j - 1] a[j];a[j] temp;}}}}3、插入排序算法
通过构建有序序列对于未排序数据在已排序序列中从后向前扫描找到相应的位置并插入。 插入排序非常类似于整扑克牌。在开始摸牌时左手是空的牌面朝下放在桌上。接着一次从 桌上摸起一张牌并将它插入到左手一把牌中的正确位置上。为了找到这张牌的正确位置要将 它与手中已有的牌从右到左地进行比较。无论什么时候左手中的牌都是排好序的。 如果输入数组已经是排好序的话插入排序出现最佳情况其运行时间是输入规模的一个线性函 数。如果输入数组是逆序排列的将出现最坏情况。平均情况与最坏情况一样其时间代价是(n2)。
public void sort(int arr[]){for (int i 1; i arr.length; i) {//插入的数int insertVal arr[i];//被插入的位置(准备和前一个数比较)int index i - 1;//如果插入的数比被插入的数小while (index 0 insertVal arr[index]) {//将把 arr[index] 向后移动arr[index 1] arr[index];//让 index 向前移动index--;}//把插入的数放入合适位置arr[index 1] insertVal;}}4、快速排序算法
快速排序的原理选择一个关键值作为基准值。比基准值小的都在左边序列一般是无序的 比基准值大的都在右边一般是无序的。一般选择序列的第一个元素。 一次循环从后往前比较用基准值和最后一个值比较如果比基准值小的交换位置如果没有 继续比较下一个直到找到第一个比基准值小的值才交换。找到这个值之后又从前往后开始比 较如果有比基准值大的交换位置如果没有继续比较下一个直到找到第一个比基准值大的 值才交换。直到从前往后的比较索引从后往前比较的索引结束第一次循环此时对于基准值 来说左右两边就是有序的了
public void sort(int[] a, int low, int high) {int start low;int end high;int key a[low];while (end start) {//从后往前比较while (end start a[end] key)//如果没有比关键值小的比较下一个直到有比关键值小的交换位置然后又从前往后比较end--;if (a[end] key) {int temp a[end];a[end] a[start];a[start] temp;}//从前往后比较while (end start a[start] key)//如果没有比关键值大的比较下一个直到有比关键值大的交换位置start;if (a[start] key) {int temp a[start];a[start] a[end];a[end] temp;}//此时第一次循环比较结束关键值的位置已经确定了。左边的值都比关键值小右边的值都比关键值大但是两边的顺序还有可能是不一样的进行下面的递归调用}//递归if (start low) sort(a, low, start - 1);//左边序列。第一个索引位置到关键值索引-1if (end high) sort(a, end 1, high);//右边序列。从关键值索引1 到最后一个}5、希尔排序算法
基本思想先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序待整个序列 中的记录“基本有序”时再对全体记录进行依次直接插入排序。
操作方法 选择一个增量序列 t1t2…tk其中 titjtk1按增量序列个数 k对序列进行 k 趟排序每趟排序根据对应的增量 ti将待排序列分割成若干长度为 m 的子序列分别对各子表进 行直接插入排序。仅增量因子为1 时整个序列作为一个表来处理表长度即为整个序列的长 度。
private void shellSort(int[] a) {int dk a.length / 2;while (dk 1) {ShellInsertSort(a, dk);dk dk / 2;}}private void ShellInsertSort(int[] a, int dk) {
//类似插入排序只是插入排序增量是 1这里增量是 dk,把 1 换成 dk 就可以了for (int i dk; i a.length; i) {if (a[i] a[i - dk]) {int j;int x a[i];//x 为待插入元素a[i] a[i - dk];for (j i - dk; j 0 x a[j]; j j - dk) {
//通过循环逐个后移一位找到要插入的位置。a[j dk] a[j];}a[j dk] x;//插入}}}6、归并排序算法
归并Merge排序法是将两个或两个以上有序表合并成一个新的有序表即把待排序序列 分为若干个子序列每个子序列是有序的。然后再把有序子序列合并为整体有序序列。
public static void main(String[] args) {int[] data new int[]{5, 3, 6, 2, 1, 9, 4, 8, 7};print(data);mergeSort(data);System.out.println(排序后的数组);print(data);}public static void mergeSort(int[] data) {sort(data, 0, data.length - 1);}public static void sort(int[] data, int left, int right) {if (left right)return;// 找出中间索引int center (left right) / 2;// 对左边数组进行递归sort(data, left, center);// 对右边数组进行递归sort(data, center 1, right);// 合并merge(data, left, center, right);print(data);}/*** 将两个数组进行归并归并前面 2 个数组已有序归并后依然有序* Page 239 of 283** param data 数组对象* param left 左数组的第一个元素的索引* param center 左数组的最后一个元素的索引center1 是右数组第一个元素的索引* param right 右数组最后一个元素的索引*/public static void merge(int[] data, int left, int center, int right) {// 临时数组int[] tmpArr new int[data.length];// 右数组第一个元素索引int mid center 1;// third 记录临时数组的索引int third left;// 缓存左数组第一个元素的索引int tmp left;while (left center mid right) {// 从两个数组中取出最小的放入临时数组if (data[left] data[mid]) {tmpArr[third] data[left];} else {tmpArr[third] data[mid];}}// 剩余部分依次放入临时数组实际上两个 while 只会执行其中一个while (mid right) {tmpArr[third] data[mid];}while (left center) {tmpArr[third] data[left];}// 将临时数组中的内容拷贝回原数组中// 原 left-right 范围的内容被复制回原数组while (tmp right) {data[tmp] tmpArr[tmp];}}public static void print(int[] data) {for (int i 0; i data.length; i) {System.out.print(data[i] \t);}System.out.println();}7、桶排序算法
桶排序的基本思想是 把数组 arr 划分为 n 个大小相同子区间桶每个子区间各自排序最 后合并 。计数排序是桶排序的一种特殊情况可以把计数排序当成每个桶里只有一个元素的情况。 1.找出待排序数组中的最大值 max、最小值 min 2.我们使用 动态数组 ArrayList 作为桶桶里放的元素也用 ArrayList 存储。桶的数量为(maxmin)/arr.length1 3.遍历数组 arr计算每个元素 arr[i] 放的桶 4.每个桶各自排序
public static void bucketSort(int[] arr) {int max Integer.MIN_VALUE;int min Integer.MAX_VALUE;for (int i 0; i arr.length; i) {max Math.max(max, arr[i]);min Math.min(min, arr[i]);}//创建桶int bucketNum (max - min) / arr.length 1;ArrayListArrayListInteger bucketArr new ArrayList(bucketNum);for (int i 0; i bucketNum; i) {bucketArr.add(new ArrayListInteger());}//将每个元素放入桶for (int i 0; i arr.length; i) {int num (arr[i] - min) / (arr.length);bucketArr.get(num).add(arr[i]);}for (int i 0; i bucketArr.size(); i) {Collections.sort(bucketArr.get(i));}}8、基数排序算法
将所有待比较数值正整数统一为同样的数位长度数位较短的数前面补零。然后从最低位 开始依次进行一次排序。这样从最低位排序一直到最高位排序完成以后,数列就变成一个有序序列。
public class radixSort {int a[] {49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 101, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51};public radixSort() {sort(a);for (int i 0; i a.length; i) {System.out.println(a[i]);}}public void sort(int[] array) {//首先确定排序的趟数;int max array[0];for (int i 1; i array.length; i) {if (array[i] max) {max array[i];}}int time 0;//判断位数;while (max 0) {max / 10;time;}//建立 10 个队列;ListArrayList queue new ArrayListArrayList();for (int i 0; i 10; i) {ArrayListInteger queue1 new ArrayListInteger();queue.add(queue1);}//进行 time 次分配和收集;for (int i 0; i time; i) {//分配数组元素;for (int j 0; j array.length; j) {//得到数字的第 time1 位数;int x array[j] % (int) Math.pow(10, i 1) / (int) Math.pow(10, i);ArrayListInteger queue2 queue.get(x);queue2.add(array[j]);queue.set(x, queue2);}int count 0;//元素计数器;//收集队列元素;for (int k 0; k 10; k) {while (queue.get(k).size() 0) {ArrayListInteger queue3 queue.get(k);array[count] queue3.get(0);queue3.remove(0);count;}}}}}9、剪枝算法
在搜索算法中优化中剪枝就是通过某种判断避免一些不必要的遍历过程形象的说就是 剪去了搜索树中的某些“枝条”故称剪枝。应用剪枝优化的核心问题是设计剪枝判断方法即 确定哪些枝条应当舍弃哪些枝条应当保留的方法
10、回溯算法
回溯算法实际上一个类似枚举的搜索尝试过程主要是在搜索尝试过程中寻找问题的解当发现 已不满足求解条件时就“回溯”返回尝试别的路径。
11、最短路径算法
从某顶点出发沿图的边到达另一顶点所经过的路径中各边上权值之和最小的一条路径叫做最 短路径。解决最短路的问题有以下算法Dijkstra 算法Bellman-Ford 算法Floyd 算法和 SPFA 算法等。
12、最小生成树算法
现在假设有一个很实际的问题我们要在 n 个城市中建立一个通信网络则连通这 n 个城市需要 布置 n-1 一条通信线路这个时候我们需要考虑如何在成本最低的情况下建立这个通信网 于是我们就可以引入连通图来解决我们遇到的问题n 个城市就是图上的 n 个顶点然后边表示 两个城市的通信线路每条边上的权重就是我们搭建这条线路所需要的成本所以现在我们有 n 个 顶点的连通网可以建立不同的生成树每一颗生成树都可以作为一个通信网当我们构造这个连 通网所花的成本最小时搭建该连通网的生成树就称为最小生成树。
构造最小生成树有很多算法但是他们都是利用了最小生成树的同一种性质MST 性质假设 N(V,{E})是一个连通网U 是顶点集 V 的一个非空子集如果uv是一条具有最小权值的边 其中 u 属于 Uv 属于 V-U则必定存在一颗包含边uv的最小生成树下面就介绍两种使 用 MST 性质生成最小生成树的算法普里姆算法和克鲁斯卡尔算法。 文章转载自: http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com http://www.morning.yrbhf.cn.gov.cn.yrbhf.cn http://www.morning.qnzld.cn.gov.cn.qnzld.cn http://www.morning.mzkn.cn.gov.cn.mzkn.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.grqlc.cn.gov.cn.grqlc.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.lbgfz.cn.gov.cn.lbgfz.cn http://www.morning.kmbgl.cn.gov.cn.kmbgl.cn http://www.morning.mlfmj.cn.gov.cn.mlfmj.cn http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.jnvivi.com.gov.cn.jnvivi.com http://www.morning.hmktd.cn.gov.cn.hmktd.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.tlpgp.cn.gov.cn.tlpgp.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.clkyw.cn.gov.cn.clkyw.cn http://www.morning.qsyyp.cn.gov.cn.qsyyp.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.zxrtt.cn.gov.cn.zxrtt.cn http://www.morning.gzzncl.cn.gov.cn.gzzncl.cn http://www.morning.qgtbx.cn.gov.cn.qgtbx.cn http://www.morning.pdmml.cn.gov.cn.pdmml.cn http://www.morning.sfdky.cn.gov.cn.sfdky.cn http://www.morning.qhrsy.cn.gov.cn.qhrsy.cn http://www.morning.bwygy.cn.gov.cn.bwygy.cn http://www.morning.hqnsf.cn.gov.cn.hqnsf.cn http://www.morning.zpxwg.cn.gov.cn.zpxwg.cn http://www.morning.tbwsl.cn.gov.cn.tbwsl.cn http://www.morning.smspc.cn.gov.cn.smspc.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.wddmr.cn.gov.cn.wddmr.cn http://www.morning.cyjjp.cn.gov.cn.cyjjp.cn http://www.morning.crdtx.cn.gov.cn.crdtx.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.kryr.cn.gov.cn.kryr.cn http://www.morning.tmxfn.cn.gov.cn.tmxfn.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.jybj.cn.gov.cn.jybj.cn http://www.morning.ffptd.cn.gov.cn.ffptd.cn http://www.morning.ntyanze.com.gov.cn.ntyanze.com http://www.morning.yrdn.cn.gov.cn.yrdn.cn http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn http://www.morning.ygrkg.cn.gov.cn.ygrkg.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.pyncm.cn.gov.cn.pyncm.cn http://www.morning.stmkm.cn.gov.cn.stmkm.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.gcszn.cn.gov.cn.gcszn.cn http://www.morning.hrnrx.cn.gov.cn.hrnrx.cn http://www.morning.nwzcf.cn.gov.cn.nwzcf.cn http://www.morning.qbrs.cn.gov.cn.qbrs.cn http://www.morning.gjfym.cn.gov.cn.gjfym.cn http://www.morning.jwtjf.cn.gov.cn.jwtjf.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.xhsxj.cn.gov.cn.xhsxj.cn http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.syxmx.cn.gov.cn.syxmx.cn http://www.morning.lqlc.cn.gov.cn.lqlc.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.wrtw.cn.gov.cn.wrtw.cn http://www.morning.mjbkp.cn.gov.cn.mjbkp.cn http://www.morning.tjqcfw.cn.gov.cn.tjqcfw.cn http://www.morning.clxpp.cn.gov.cn.clxpp.cn http://www.morning.phxdc.cn.gov.cn.phxdc.cn