??xml version="1.0" encoding="utf-8" standalone="yes"?>国产三线在线,老司机2019福利精品视频导航,日本一区二区不卡视频http://www.aygfsteel.com/stevenjohn/category/53877.html那些青春的岁?/description>zh-cnFri, 27 Mar 2015 19:03:45 GMTFri, 27 Mar 2015 19:03:45 GMT60二分查找旉复杂度的计算(? http://www.aygfsteel.com/stevenjohn/archive/2015/03/26/423859.htmlabinabinThu, 26 Mar 2015 08:30:00 GMThttp://www.aygfsteel.com/stevenjohn/archive/2015/03/26/423859.htmlhttp://www.aygfsteel.com/stevenjohn/comments/423859.htmlhttp://www.aygfsteel.com/stevenjohn/archive/2015/03/26/423859.html#Feedback0http://www.aygfsteel.com/stevenjohn/comments/commentRss/423859.htmlhttp://www.aygfsteel.com/stevenjohn/services/trackbacks/423859.html二分查找的基本思想是将n个元素分成大致相{的两部分,去a[n/2]与x做比较,如果x=a[n/2],则找到x,法中止Q如果x<a[n/2],则只要在数组a的左半部分l搜索x,如果x>a[n/2],则只要在数组a的右半部搜烦x.

旉复杂度无非就是while循环的次敎ͼ

d有n个元素,

渐渐跟下d是n,n/2,n/4,....n/2^kQ其中k是循环的次?/span>

׃你n/2^k取整?gt;=1

即on/2^k=1

可得k=log2n,Q是?为底Qn的对敎ͼ

所以时间复杂度可以表示O()=O(logn)



abin 2015-03-26 16:30 发表评论
]]>
两个长度相等的有序数l求中位?/title><link>http://www.aygfsteel.com/stevenjohn/archive/2014/11/17/420207.html</link><dc:creator>abin</dc:creator><author>abin</author><pubDate>Mon, 17 Nov 2014 13:47:00 GMT</pubDate><guid>http://www.aygfsteel.com/stevenjohn/archive/2014/11/17/420207.html</guid><wfw:comment>http://www.aygfsteel.com/stevenjohn/comments/420207.html</wfw:comment><comments>http://www.aygfsteel.com/stevenjohn/archive/2014/11/17/420207.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/stevenjohn/comments/commentRss/420207.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/stevenjohn/services/trackbacks/420207.html</trackback:ping><description><![CDATA[<div>设x[1…n],y[1…n]Z个数l,每个包含n个已知的排好序的敎ͼl出一个数lx和y中所?n个元素的中位敎ͼ要求旉复杂度ؓO(lgN)<br /><br />q是法D上面的一道题目:<br /><br /><div>public class FindMedianTwoSortedArray {</div><div></div><div><span style="white-space:pre"> </span>public static int median(int[] arr1, int l1, int h1, int[] arr2, int l2, int h2)</div><div>    {</div><div><span style="white-space:pre"> </span>System.out.println("-----------");</div><div>        int mid1 = (h1 + l1 ) / 2;</div><div>        int mid2 = (h2 + l2 ) / 2;</div><div></div><div>        if (h1 - l1 == 1)</div><div>            return (Math.max(arr1[l1] , arr2[l2]) + Math.min(arr1[h1] , arr2[h2]))/2;</div><div>        else if (arr1[mid1] > arr2[mid2])</div><div>            return median(arr1, l1, mid1 , arr2, mid2 , h2);    </div><div>        else</div><div>            return median(arr1, mid1 , h1, arr2, l2 , mid2 );    </div><div>    }     </div><div><span style="white-space:pre"> </span>public static void main(String[] args) {</div><div><span style="white-space:pre"> </span>int[] a = new int[]{0,1,2};</div><div><span style="white-space:pre"> </span>int[] b = new int[]{1,2,3};</div><div><span style="white-space:pre"> </span>int result = median(a, 0, a.length-1,b,0,b.length-1);</div><div><span style="white-space:pre"> </span>System.out.println(result);</div><div><span style="white-space:pre"> </span>}</div><div>}</div><br /><br /></div><img src ="http://www.aygfsteel.com/stevenjohn/aggbug/420207.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/stevenjohn/" target="_blank">abin</a> 2014-11-17 21:47 <a href="http://www.aygfsteel.com/stevenjohn/archive/2014/11/17/420207.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>插入排序(java)http://www.aygfsteel.com/stevenjohn/archive/2014/10/19/418868.htmlabinabinSat, 18 Oct 2014 17:20:00 GMThttp://www.aygfsteel.com/stevenjohn/archive/2014/10/19/418868.htmlhttp://www.aygfsteel.com/stevenjohn/comments/418868.htmlhttp://www.aygfsteel.com/stevenjohn/archive/2014/10/19/418868.html#Feedback0http://www.aygfsteel.com/stevenjohn/comments/commentRss/418868.htmlhttp://www.aygfsteel.com/stevenjohn/services/trackbacks/418868.htmlpackage com.abin.lee.algorithm.insert;
import java.util.Arrays;
public class InsertSort {
public static void main(String[] args) {
int[] input = {6,1,4,2,5,8,3,7,9,0};
input = InsertSort.sort(input);
System.out.println(Arrays.toString(input));
}
public static int[] sort(int[] input){
int temp = 0;
for(int i=0;i<input.length;i++){
temp = input[i];
int j=i-1;
for(;j>=0&&temp<input[j];j--){
input[j+1] = input[j];
}
input[j+1] = temp;
}
return input;
}
}


abin 2014-10-19 01:20 发表评论
]]>
归ƈ排序(java)http://www.aygfsteel.com/stevenjohn/archive/2014/10/17/418861.htmlabinabinFri, 17 Oct 2014 10:31:00 GMThttp://www.aygfsteel.com/stevenjohn/archive/2014/10/17/418861.htmlhttp://www.aygfsteel.com/stevenjohn/comments/418861.htmlhttp://www.aygfsteel.com/stevenjohn/archive/2014/10/17/418861.html#Feedback0http://www.aygfsteel.com/stevenjohn/comments/commentRss/418861.htmlhttp://www.aygfsteel.com/stevenjohn/services/trackbacks/418861.htmlpackage com.abin.lee.algorithm.merge;
import java.util.Arrays;
/**
 * 归ƈ排序
 */
public class MergeSort {
public static void main(String[] args) {
int[] input = {2,7,3,9,1,6,0,5,4,8};
MergeSort.sort(input, 0, input.length-1);
System.out.println("input="+Arrays.toString(input));
}
//首先分而自?/div>
/** 
     * 归ƈ排序 
     * ?两个(或两个以上)有序表合q成一个新的有序表 x待排序序列分q个子序列,每个子序列是有序的。然后再把有序子序列合ƈ为整体有序序?nbsp;
     * 旉复杂度ؓO(nlogn) 
     * E_排序方式 
     * @param nums 待排序数l?nbsp;
     * @return 输出有序数组 
     */  
public static int[] sort(int[] input,int low,int high){
int middle = (low+high)/2;
if(low<high){
//左边
sort(input,low,middle);
//双
sort(input,middle+1,high);
//左右归ƈ
merge(input,low,high,middle);
}
return input;
}
public static void merge(int[] input,int low,int high,int middle){
int[] temp = new int[high-low+1];
int i = low;//左指?/div>
int j = middle+1;//x?/div>
int k=0;
// 把较的数先Ud新数l中  
while(i<=middle&&j<=high){
if(input[i]<input[j]){
temp[k++] = input[i++];
}else{
temp[k++] = input[j++];
}
}
// 把左边剩余的数移入数l? 
while(i<=middle){
temp[k++] = input[i++];
}
// 把右边边剩余的数Ud数组  
while(j<=high){
temp[k++] = input[j++];
}
// 把新数组中的数覆盖input数组  
for(int m=0;m<temp.length;m++){
input[m+low] = temp[m];
}
}
}


abin 2014-10-17 18:31 发表评论
]]>二分查找?java)http://www.aygfsteel.com/stevenjohn/archive/2014/10/11/418605.htmlabinabinFri, 10 Oct 2014 16:03:00 GMThttp://www.aygfsteel.com/stevenjohn/archive/2014/10/11/418605.htmlhttp://www.aygfsteel.com/stevenjohn/comments/418605.htmlhttp://www.aygfsteel.com/stevenjohn/archive/2014/10/11/418605.html#Feedback0http://www.aygfsteel.com/stevenjohn/comments/commentRss/418605.htmlhttp://www.aygfsteel.com/stevenjohn/services/trackbacks/418605.html//递归?br />package com.abin.lee.algorithm.binary;
public class BinarySearch {
public static void main(String[] args) {
int[] input = new int[]{2,3,4,5,6,7,8,9}; 
int result = search(input, 5, 0, input.length-1);
System.out.println("result="+result);
}
public static int search(int[] input,int data,int low,int high){
int middle = (low+high)/2;
if(data == input[middle]){
return middle;
}else if(data > input[middle]){
return search(input, data, middle+1, high);
}else if(data < input[middle]){
return search(input, data, low, middle-1);
}else{
return -1;
}
}
}


//while循环?br />
public static int binary(int[] input,int low,int high,int target){
while(low <= high){
int middle = (low+high)/2;
if(input[middle]>target){
high = middle-1;
}else if(input[middle]<target){
low = middle+1;
}else{
return middle;
}
}
return -1;
}


abin 2014-10-11 00:03 发表评论
]]>快速排?java)http://www.aygfsteel.com/stevenjohn/archive/2014/10/10/418603.htmlabinabinFri, 10 Oct 2014 15:43:00 GMThttp://www.aygfsteel.com/stevenjohn/archive/2014/10/10/418603.htmlhttp://www.aygfsteel.com/stevenjohn/comments/418603.htmlhttp://www.aygfsteel.com/stevenjohn/archive/2014/10/10/418603.html#Feedback0http://www.aygfsteel.com/stevenjohn/comments/commentRss/418603.htmlhttp://www.aygfsteel.com/stevenjohn/services/trackbacks/418603.htmlpackage com.abin.lee.algorithm.fast;
public class SpeedSort {
public static void main(String[] args) {
int[] input = new int[]{1,6,3,5,2,4};
quickSort(input, 0, input.length-1);
for(int i=0;i<input.length;i++){
System.out.println("input["+i+"]="+input[i]);
}
}
//分ؓ参照物的大小两组
public static int getMiddle(int[] input,int low,int high){
int temp = input[low];
while(low<high){
while(low<high && input[high]>temp){
high--;
}
input[low]=input[high];
while(low<high && input[low]<temp){
low++;
}
input[high]=input[low];
}
input[low]=temp;
return low;
}
//分而自?/div>
public static void quickSort(int[] input,int low,int high){
if(low<high){
int middle = getMiddle(input, low, high);
quickSort(input,low,middle-1);
quickSort(input,middle+1,high);
}
}
}


快速排?nbsp;对冒泡排序的一U改q,若初始记录序列按关键字有序或基本有序Q蜕化ؓ冒排序。用的是递归原理Q在所有同数量UO(n longn) 的排序方法中Q其q_性能最好。就q_旉而言Q是目前被认为最好的一U?u>内部排序Ҏ
基本思想是:通过一w排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小Q然后再按此Ҏ对这两部分数据分别进行快速排序,整个排序q程可以递归q行Q以此达到整个数据变成有序序列?br /> 三个指针: W一个指针称为pivotkey指针Q枢_Q第二个指针和第三个指针分别为left指针和right指针Q分别指向最左边的值和最双的倹{left指针和right指针从两边同时向中间DQ在D的过E中不停的与枢u比较Q将比枢轴小的元素移C端,比枢u大的元素Ud高端Q枢轴选定后永q不变,最l在中间Q前后大?br />

需要两个函敎ͼ

① 递归函数  public static void quickSort(int[]n ,int left,int right)
② 分割函数Q一快速排序函敎ͼ public static int partition(int[]n ,int left,int right)

JAVA源代码(成功q行Q?/span>Q?br />


package testSortAlgorithm;
public class QuickSort {
public static void main(String[] args) {
int [] array = {49,38,65,97,76,13,27};
quickSort(array, 0, array.length - 1);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
/*先按照数lؓ数据原型写出法Q再写出扩展性算法。数l{49,38,65,97,76,13,27}
* */
public static void quickSort(int[]n ,int left,int right){
int pivot;
if (left < right) {
//pivot作ؓ枢uQ较之小的元素在左,较之大的元素在右
pivot = partition(n, left, right);
//对左xl递归调用快速排序,直到序完全正确
quickSort(n, left, pivot - 1);
quickSort(n, pivot + 1, right);
}
}
public static int partition(int[]n ,int left,int right){
int pivotkey = n[left];
//枢u选定后永q不变,最l在中间Q前后?/div>
while (left < right) {
while (left < right && n[right] >= pivotkey) --right;
//比枢u的元素Ud低端Q此时right位相当于I,{待低位比pivotkey大的数补?/div>
n[left] = n[right];
while (left < right && n[left] <= pivotkey) ++left;
//比枢u大的元素Ud高端Q此时left位相当于I,{待高位比pivotkey的数补?/div>
n[right] = n[left];
}
//当left == rightQ完成一快速排序,此时left位相当于I,{待pivotkey补上
n[left] = pivotkey;
return left;
}
}






abin 2014-10-10 23:43 发表评论
]]>单链表的实现(java)http://www.aygfsteel.com/stevenjohn/archive/2014/10/10/418602.htmlabinabinFri, 10 Oct 2014 14:46:00 GMThttp://www.aygfsteel.com/stevenjohn/archive/2014/10/10/418602.htmlhttp://www.aygfsteel.com/stevenjohn/comments/418602.htmlhttp://www.aygfsteel.com/stevenjohn/archive/2014/10/10/418602.html#Feedback0http://www.aygfsteel.com/stevenjohn/comments/commentRss/418602.htmlhttp://www.aygfsteel.com/stevenjohn/services/trackbacks/418602.htmlpackage com.abin.lee.list.test;
public class SingleList {
private Object obj;
private transient SingleList singleList;
private SingleList next;
private SingleList pre;
private transient int size;
public SingleList() {
singleList = new SingleList(null,null,null);
singleList.next = singleList.pre = singleList;
size=0;
}
public SingleList(Object obj,SingleList next,SingleList pre) {
this.obj=obj;
this.next=next;
this.pre=pre;
}
public void add(Object obj){
SingleList current = new SingleList(obj,singleList,singleList.pre);
current.next.pre = current;
current.pre.next = current;
size++;
singleList = current;
}
public int size(){
return size;
}
public Object get(int index){
SingleList current = singleList.next;
for(int i=0;i<index;i++){
current = current.next;
}
return current.obj;
}
public static void main(String[] args) {
SingleList single = new SingleList();
for(int i=0;i<5;i++){
single.add("abin"+i);
}
System.out.println("single="+single);
int size = single.size();
System.out.println("size="+size);
Object element = single.get(0);
System.out.println("element="+element);
}
}


abin 2014-10-10 22:46 发表评论
]]>Java 常常被问到的法http://www.aygfsteel.com/stevenjohn/archive/2013/09/24/404354.htmlabinabinTue, 24 Sep 2013 00:50:00 GMThttp://www.aygfsteel.com/stevenjohn/archive/2013/09/24/404354.htmlhttp://www.aygfsteel.com/stevenjohn/comments/404354.htmlhttp://www.aygfsteel.com/stevenjohn/archive/2013/09/24/404354.html#Feedback0http://www.aygfsteel.com/stevenjohn/comments/commentRss/404354.htmlhttp://www.aygfsteel.com/stevenjohn/services/trackbacks/404354.html 二叉树相关的问的比较?br />二分查找
treemap和某些数据库索引的的底层是红黑树
链表的相交和闭环
 针对二叉树,比如分层遍历Q找最q父节点
在字W串中找回文Ԍ数组中寻N复的数字或相M和的最大串
 U黑树不是B树么
 如果没有遇到专业考算法的公司Q只能说q没面试q牛公司Q就相当于编E感觉不到数据结构,相当于编E还没入?/div>
 是对UC叉B?br />q是有点不一P是其子集Q误g?/div>


abin 2013-09-24 08:50 发表评论
]]>java 排序法有多种http://www.aygfsteel.com/stevenjohn/archive/2013/09/05/403727.htmlabinabinThu, 05 Sep 2013 11:49:00 GMThttp://www.aygfsteel.com/stevenjohn/archive/2013/09/05/403727.htmlhttp://www.aygfsteel.com/stevenjohn/comments/403727.htmlhttp://www.aygfsteel.com/stevenjohn/archive/2013/09/05/403727.html#Feedback0http://www.aygfsteel.com/stevenjohn/comments/commentRss/403727.htmlhttp://www.aygfsteel.com/stevenjohn/services/trackbacks/403727.html冒排序Qbubble sortQ?— O(n2)
鸡尾酒排?(Cocktail sort, 双向的冒泡排? — O(n2)
插入排序 Qinsertion sortQ?#8212; O(n2)
桶排?Qbucket sortQ?#8212; O(n); 需?O(k) 额外 记忆?
计数排序 (counting sort) — O(n+k); 需?O(n+k) 额外 记忆?
归ƈ排序 Qmerge sortQ?#8212; O(n log n); 需?O(n) 额外记忆?
原地归ƈ排序 — O(n2)
二叉树排?QBinary tree sortQ?— O(n log n); 需?O(n) 额外记忆?
鸽l排序 (Pigeonhole sort) — O(n+k); 需?O(k) 额外记忆?
基数排序 Qradix sortQ?#8212; O(n·k); 需?O(n) 额外记忆?
Gnome sort — O(n2)
Library sort — O(n log n) with high probability, 需?(1+ε)n 额外记忆体不E_
选择排序 Qselection sortQ?#8212; O(n2)
希尔排序 Qshell sortQ?#8212; O(n log n) 如果使用最佳的现在版本
Comb sort — O(n log n)
堆排?QheapsortQ?#8212; O(n log n)
Smoothsort — O(n log n)
快速排?QquicksortQ?#8212; O(n log n) 期望旉, O(n2) 最坏情? Ҏ大的、ؕC列一般相信是最快的已知排序

abin 2013-09-05 19:49 发表评论
]]>
Java 常用法http://www.aygfsteel.com/stevenjohn/archive/2013/09/05/403717.htmlabinabinThu, 05 Sep 2013 08:27:00 GMThttp://www.aygfsteel.com/stevenjohn/archive/2013/09/05/403717.htmlhttp://www.aygfsteel.com/stevenjohn/comments/403717.htmlhttp://www.aygfsteel.com/stevenjohn/archive/2013/09/05/403717.html#Feedback0http://www.aygfsteel.com/stevenjohn/comments/commentRss/403717.htmlhttp://www.aygfsteel.com/stevenjohn/services/trackbacks/403717.html1、冒泡排序:
冒排序QBubbleSortQ的基本概念是:依次比较盔R的两个数Q将数攑֜前面Q大数放在后面。即在第一:首先比较W?个和W?个数Q将数攑։Q大数放后。然后比较第2个数和第3个数Q将数攑։Q大数放后,如此l箋Q直x较最后两个数Q将数攑։Q大数放后。至此第一结束,最大的数放C最后。在W二:仍从W一Ҏ开始比较(因ؓ可能׃W?个数和第3个数的交换,使得W?个数不再于W?个数Q,小数放前,大数攑֐Q一直比较到倒数W二个数Q倒数W一的位|上已经是最大的Q,W二结束,在倒数W二的位|上得到一个新的最大数Q其实在整个数列中是W二大的敎ͼ。如此下去,重复以上q程Q直xl完成排序?

具体代码例一Q?br />

package com.abin.lee.algorithm.bubble;

public class BubbleSort {
 public static void main(String[] args) {
  int[] start={5,2,1,3,6,4};
  int[] end=sort(start);
  for(int i=0;i<end.length;i++){
   System.out.println("end["+i+"]="+end[i]);
  }
 }
 
 public static int[] sort(int[] input){
  int temp=0;

  //最多做n-1排?/div>  for(int i=0;i<input.length-1;i++){
    //对当前无序区间score[0......length-i-1]q行排序(j的范围很关键Q这个范围是在逐步~小?
   for(int j=0;j<input.length-i-1;j++){
    //把大的g换到后面
    if(input[j]>input[j+1]){
     temp=input[j];
     input[j]=input[j+1];
     input[j+1]=temp;
    }
    StringBuffer stb=new StringBuffer();
    for(int k=0;k<input.length;k++){
     stb.append("input["+k+"]="+input[k]+" ");
    }
    System.out.println("i="+i+",j="+j+" = "+stb.toString());
   }
  }
  return input;
 }

 

}


2、选择排序Q?br />选择排序(Straight Select Sorting) 也是一U简单的排序ҎQ它的基本思想是:W一ơ从R[0]~R[n-1]中选取最|与R[0]交换Q第二次从R{1}~R[n-1]中选取最|与R[1]交换Q?...Q?nbsp;  Wiơ从R[i-1]~R[n-1]中选取最|与R[i-1]交换Q?....Q第n-1ơ从R[n-2]~R[n-1]中选取最|与R[n-2]交换,d通过n-1?得到一个按排序码从到大排列的有序序列.
具体代码Q?br />

package com.abin.lee.algorithm.select;

public class SelectSort {
 public static void main(String[] args) {
  int[] start={5,2,3,1,6,4};
  start=sort(start);
  for(int i=0;i<start.length;i++){
   System.out.println("start["+i+"]="+start[i]);
  }
 }
 public static int[] sort(int[] input){
  int temp=0;
  for(int i=0;i<input.length;i++){
   for(int j=i+1;j<input.length;j++){
    if(input[i]>input[j]){
     temp=input[i];
     input[i]=input[j];
     input[j]=temp;
    }
    StringBuffer stb=new StringBuffer();
    for(int k=0;k<input.length;k++){
     stb.append("input["+k+"]="+input[k]+" ");
    }
    System.out.println("i="+i+",j="+j+" = "+stb.toString());
   }
  }
  return input;
 }
}




3、请输入一个数字,比如4Q输ZؓQ?br />1
2 3
4 5 6
7 8 9 10

代码如下Q?br />

package com.abin.lee.photo;

public class TestInputNumber {
 public static void main(String[] args) {
  int n=4;
  output(n);
 }
 public static void output(int n){
  int temp=1;
  for(int i=1;i<=n;i++){
   for(int j=0;j<i;j++){
    System.out.print(temp+" ");
    temp++;
   }
   System.out.println("");
  }
  
 }
}


4.二叉树算?br />

package com.abin.lee.algorithm.binary;

public class BinaryNode {
 public int data;//根节?br /> BinaryNode left;//左节?br /> BinaryNode right;//双?br /> 
 public BinaryNode(int data,BinaryNode left,BinaryNode right) {
  this.data=data;
  this.left=left;
  this.right=right;
 }
 
 public int getData() {
  return data;
 }
 public void setData(int data) {
  this.data = data;
 }
 public BinaryNode getLeft() {
  return left;
 }
 public void setLeft(BinaryNode left) {
  this.left = left;
 }
 public BinaryNode getRight() {
  return right;
 }
 public void setRight(BinaryNode right) {
  this.right = right;
 }
}




package com.abin.lee.algorithm.binary;

public class BinaryTree {
 //前序遍历
 public static void preOrder(BinaryNode root){
  if(null!=root){
   System.out.print(root.data+"-");
   preOrder(root.left);
   preOrder(root.right);
  }
 }
 //中序遍历
 public static void inOrder(BinaryNode root){
  if(null!=root){
   inOrder(root.left);
   System.out.print(root.data+"--");
   inOrder(root.right);
  }
 }
 //后序遍历
 public static void postOrder(BinaryNode root){
  if(null!=root){
   postOrder(root.left);
   postOrder(root.right);
   System.out.print(root.data+"---");
  }
 }
 
 public static void main(String[] args) {
  BinaryNode one=new BinaryNode(1,null,null);
  BinaryNode three=new BinaryNode(3,null,null);
  BinaryNode two=new BinaryNode(2,three,one);
  BinaryNode four=new BinaryNode(4,one,two);
  BinaryNode five=new BinaryNode(5,four,one);
  System.out.println("preOrder");
  preOrder(five);
  System.out.println();
  System.out.println("inOrder");
  inOrder(five);
  System.out.println();
  System.out.println("postOrder");
  postOrder(five);
  System.out.println();
  
 }

}


输出l果Q?br />preOrder
5-4-1-2-3-1-1-
inOrder
1--4--3--2--1--5--1--
postOrder
1---3---1---2---4---1---5---




5、java插入排序

插入式排序法——插入排序?/span>

插入排序QInsertion SortionQ的基本思想是:?/span>n个待排序的元素看成一个有序表和一个无序表Q开始有序表只包含一个元素,无序表中包含n-1个元素,排序q程中每ơ从无序表中取出W一个元素,把它的排序码依次与有序表元素的排序码q行比较Q将它插入到有序表中的适当位置Q之成为新的有序表?/span>


public class InjectionSort  //定义一?InjectionSort c?br />public static void injectionSort(int[] number) //传数l?br />for(int j = 1;j<number.length;j++)//循环
int tmp = number[j]; //循环把数l第二个值放到tmp?br />int i = j-1//li 赋?br />while(tmp<number[i]) //tmp值和数组W一个值比较谁?br />number[i+1] = number[i]; //如果于把W一个D值给W二?br />i--;
if(i == -1)//如果i?-1
break; //退出@?br />number[i+1] = tmp //因ؓ比较数组里的前一个比后一个这样就换交了实C把小的放在前?br />q是W一ơ,因ؓ循环是一个数l,后边的就一ơ往下@环,最后就把数l里的顺序从到大排序了
public static void main(String[] args){
int[] num = {5,46,26,67,2,35};//定义数组num
injectionSort(num);//调用Ҏ
for(int i = 0;i<num.length;i++){
System.out.println(num[i]);//昄排序后的数组Q一行显CZ个?br />
单说是数组里第二个和第一个比谁小Q把的攑ֈW一个里Q大的放到第二个里,然后W二个再和第三个比,的q是攑֜前,一直比到这个数l结束,q样实C从小到大Q希望我说的够详l?/pre>

插入排序代码while循环Q?br />
package com.abin.lee.algorithm.insert;
public class InsertSort {
public static void main(String[] args) {
int[] input = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 };
input=sort(input);
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + "  ");
}
}
public static int[] sort(int[] input) {
for (int i = 1; i < input.length; i++) {
int insertVal = input[i];
// insertValue准备和前一个数比较
int index = i - 1;
while (index >= 0 && insertVal < input[index]) {
// 把input[index]向后Ud
input[index + 1] = input[index];
// 让index向前Ud一?/div>
index--;
}
// insertValue插入到适当位置
input[index + 1] = insertVal;
//下面q个循环是ؓ了打C下中间的循环看看是不是插入排序的正确法
StringBuffer stb=new StringBuffer();
for(int k=0;k<input.length;k++){
stb.append(input[k]+" ");
}
System.out.println("i="+i+" = "+stb.toString());
}
return input;
}
}


插入排序for循环代码Q?br />
package com.abin.lee.algorithm.insert;
public class DoInsertSort {
public static void main(String[] args) {
int[] input={5,4,6,3,7,2,8,1,0,9};
input=sort(input);
for(int i=0;i<input.length;i++){
System.out.print("input["+i+"]="+input[i]+" ");
}
}
public static int[] sort(int[] input){
for(int i=1;i<input.length;i++){
int temp=input[i];
int j;
for(j=i;j>0;j--){
if(temp<input[j-1]){
input[j]=input[j-1];
}else{
break;
}
}
input[j]=temp;
//下面q个循环是ؓ了打C下中间的循环看看是不是插入排序的正确法
StringBuffer stb=new StringBuffer();
for(int k=0;k<input.length;k++){
stb.append(input[k]+" ");
}
System.out.println("i="+i+" = "+stb.toString());
}
return input;
}
}


 

二分查找又称折半查找Q它是一U效率较高的查找Ҏ?/p>

折半查找的算法思想是将数列按有序化(递增或递减)排列Q查找过E中采用跌式方式查找,卛_以有序数列的中点位置为比较对象,如果要找的元素值小于该中点元素Q则待查序列羃ؓ左半部分Q否则ؓ叛_部分。通过一ơ比较,查扑֌间羃一半?折半查找是一U高效的查找Ҏ。它可以明显减少比较ơ数Q提高查找效率。但是,折半查找的先x件是查找表中的数据元素必L序?/span>

package com.abin.algorithm.template.half;

public class BinarySearch {
public static void main(String[] args) {
int[] src=new int[]{1,3,5,7,9,11};
int result=binarySearch(src, 3);
System.out.println("result="+result);
int status=binarySearch(src, 9 ,0 ,src.length);
System.out.println("status="+status);
}
//循环实现
public static int binarySearch(int[] src,int key){
int low=0;
int high=src.length-1;
while(low<=high){
int middle=(low+high)/2;
if(key==src[middle]){
return middle;
}else if(key<src[middle]){
high=middle-1;
}else{
low=middle+1;
}
}
return -1;
}
//递归实现
public static int binarySearch(int[] src,int key,int low,int high){
int middle=(low+high)/2;
if(src[middle]==key){
return middle;
}else if(low>=high){
return -1;
}else if(src[middle]>key){
return binarySearch(src, key, low, middle-1);
}else if(src[middle]<key){
return binarySearch(src, key, middle+1, high);
}
return -1;
}

}




abin 2013-09-05 16:27 发表评论
]]> վ֩ģ壺 ¡| | ĺ| گ| | | | | | | ũ| | Ϫ| | Ҷ| | ī| ɽ| ƺ| Ҿ| ӡ| | | | | | | ߷| | | ˮ| ݳ| Ѩ| | | Զ| ˮ| | ۰| | |