二分查找+冒泡排序+快速排序+横向遍历二叉树 如有能改进的地方,请指出,欢迎讨论!

View Code
 1 public class BinarySearch {
 2      
 3      public int startBinarySearch(int[] a, int value){
 4          int max = a.length;
 5          int min = 0;
 6          int mid;
 7          if(1 == a.length){    //判断长度是否为一,否则会导致不能进入while循环,而出错
 8              if(value == a[0]){
 9                  return 0;
10              }else{
11                  return -1;
12              }
13          }
14          while(max>min){
15              mid = min + (max-min)/2;
16              if(value == a[mid]){
17                  return mid;
18              }else if(value > a[mid]){
19                  min = mid+1;
20              }else{
21                  max = mid;
22              }
23          }
24          return -1;
25      }
26      
27      public static void main(String[] args) {
28  
29          int [] a = {};
30          BinarySearch BS = new BinarySearch();
31          System.out.println(BS.startBinarySearch(a, 5));
32      }
33 
34  }

横向遍历二叉树LinkedList和Queue的两种用法

 1 import java.util.ArrayDeque;
 2 import java.util.LinkedList;
 3 import java.util.Queue;
 4  
 5  
 6  public class Node {
 7      Node leftChild;
 8      Node rightChild;
 9      int data;
10      
11      public Node(Node leftChild,Node rightChild,int data){
12          this.leftChild = leftChild;
13          this.rightChild = rightChild;
14          this.data = data;
15      }
16      
17      public void getData(){
18          Queue<Node> queue = new ArrayDeque<Node>();
19          queue.add(this);
20          Node currentNode;
21          while(!queue.isEmpty()){
22              currentNode = (Node) queue.poll();
23              if(null!=currentNode.leftChild){
24                  queue.offer(currentNode.leftChild);
25              }
26              if(null!=currentNode.rightChild){
27                  queue.offer(currentNode.rightChild);
28              }
29              System.out.print(currentNode.data+"\t");
30          }
31      }
32      
33      public void getDataArray(){
34          LinkedList<Node> queue = new LinkedList<Node>();
35          queue.offer(this);
36          Node currentNode;
37          while(!queue.isEmpty()){
38              currentNode = queue.poll();
39              if(null!=currentNode.leftChild){
40                  queue.offer(currentNode.leftChild);
41              }
42              if(null!=currentNode.rightChild){
43                  queue.offer(currentNode.rightChild);
44              }
45              System.out.print(currentNode.data+"\t");
46          }
47      }
48      
49      public static void main(String[] args){
50          Node d = new Node(null,null,4);
51          Node e = new Node(null,null,5);
52          Node f = new Node(null,null,6);
53          Node g = new Node(null,null,7);
54          Node b = new Node(d,e,2);
55          Node c = new Node(f,g,3);
56          Node a = new Node(b,c,1);
57          a.getData();
58          a.getDataArray();
59      }
60  }

快排如下,写的比较乱,怎么能更好的判断递归终止?

 1 public class testQuickSort {
 2 
 3     public void quickSort(int[] array,int low,int high) {
 4             int i=low,j=high,temp=0;
 5             while (i!=j) {
 6                 if(array[i]>array[j]){
 7                     temp = array[i];
 8                     array[i] = array[j];
 9                     array[j] = temp;
10                     i++;
11                     while (i!=j) {
12                         if(array[i]>array[j]){
13                             temp = array[i];
14                             array[i] = array[j];
15                             array[j] = temp;
16                             break;
17                         }
18                         i++;
19                     }
20                 }
21                 if(i!=j){
22                     j--;
23                 }
24             }
25             if(low<(i-1)){
26                 quickSort(array, low,i-1);
27             }
28             if(high>(i+1)){
29                 quickSort(array, i+1, high);
30             }
31     }
32     
33     /**
34      * @param args
35      */
36     public static void main(String[] args) {
37         // TODO Auto-generated method stub
38         int[] a = {15,11,87,14,65,1,71,0,65};
39         testQuickSort tQuickSort = new testQuickSort();
40         tQuickSort.quickSort(a,0,a.length-1);
41         for (int i:a) {
42             System.out.print(i+"\t");
43         }
44     }
45 
46 }

 快排改进:

 1 public class QuickSort {
 2   
 3       public void quickSort(int[] array,int low,int high) {
 4              
 5           if(low<high){
 6               int t = partition(array,low,high);
 7               quickSort(array,low,t-1);
 8               quickSort(array,t+1,high);
 9           }
10           
11       }
12       
13       public int partition(int[] array,int i, int j){
14           while (i!=j) {
15               if(array[i]>array[j]){
16                   swap(array,i,j);
17                   i++;
18                   while (i!=j) {
19                       if(array[i]>array[j]){
20                           swap(array,i,j);
21                           break;
22                       }
23                       i++;
24                   }
25               }
26               if(i!=j){
27                   j--;
28               }
29           }
30           return i;
31       }
32       
33       public void swap(int[]a,int low,int high){
34           int temp=0;
35           temp=a[low];
36           a[low]=a[high];
37           a[high]=temp;
38       }
39       
40 
41       public static void main(String[] args) {
42           // TODO Auto-generated method stub
43           int[] a = {15,11,87,14,65,1,71,0,65};
44           testQuickSort tQuickSort = new testQuickSort();
45           tQuickSort.quickSort(a,0,a.length-1);
46           for (int i:a) {
47               System.out.print(i+"\t");
48           }
49       }
50   
51   }

 

深度优先中序遍历二叉树:

 1 package BinaryTree2Array;
 2 
 3 import java.util.LinkedList;
 4 
 5 public class BinaryTree2Array {
 6     
 7     public void getArray(Node root){
 8         Node currentNode = root;
 9         Node parrentNode = null;
10         LinkedList<Node> stack = new LinkedList<Node>();
11         while (currentNode!=null) {
12             if(null!=currentNode.left){
13                 stack.push(currentNode);
14                 currentNode = currentNode.left;
15             }else{
16                 System.out.print(currentNode.value+"\t");
17                 if(stack.size()>0){
18                     parrentNode = stack.pop();
19                     System.out.print(parrentNode.value+"\t");
20                     currentNode = parrentNode.right;
21                 }else{
22                     currentNode = null;
23                 }
24             }
25             
26         }
27         28     }
29 
30     /**
31      * @param args
32      */
33     public static void main(String[] args) {
34         // TODO Auto-generated method stub
35 
36         Node g = new Node(null, null, 7);
37         Node f = new Node(null, null, 6);
38         Node e = new Node(null, null, 5);
39         Node d = new Node(null, null, 4);
40         Node c = new Node(f, g, 3);
41         Node b = new Node(d, e, 2);
42         Node a = new Node(b, c, 1);
43         
44         BinaryTree2Array bTree2Array = new BinaryTree2Array();
45         bTree2Array.getArray(a);
46     }
47 
48 }
49 
50 class Node{
51     Node left;
52     Node right;
53     int value;
54     public Node(Node left,Node right,int value){
55         this.left = left;
56         this.right = right;
57         this.value = value;
58     }
59 }

 

冒泡排序如下:注释中的内容是错误的,为什么

View Code
public class BubbleSort {

    public int[] takeBubbleSort(int[] array){
        int temp;
        for(int i=0;i<array.length;i++){
//            for(int j=i+1;j<array.length;j++){
//                if(array[j-1]>array[j]){
//                    temp = array[j-1];
//                    array[j-1]=array[j];
//                    array[j]=temp;
//                }
//            }
            for(int j=array.length-1;j>i;j--){
                if(array[j-1]>array[j]){
                    temp = array[j-1];
                    array[j-1]=array[j];
                    array[j]=temp;
                }
            }
        }
        for(int t:array){
            System.out.print(t+"\t");
        }
        return array;
    }
    


    public static void main(String[] args) {

        int[] array = {15,58,16,86,18,17,0,54,11,34,41,81,73,46,97,40,1};
        BubbleSort BS = new BubbleSort();
        BS.takeBubbleSort(array);
    }

}

posted on 2012-10-22 16:23  chlde2500  阅读(244)  评论(0)    收藏  举报

导航