冒泡排序、二分查找法

冒泡排序的基本思想是对比相邻的元素值,如果满足条件就交换元素值,把较小的元素移动到数组前面,把大的元素移动到数组后面(也就是交换两个元素的位置),这样数组元素就像气泡一样从底部上升到顶部。

 1 package com.hanqi;
 2 
 3 public class maopao {
 4 
 5     public static void main(String[] args) {
 6         int[]array=new int[]{63,4,24,1,3,13};
 7         System.out.println("冒泡排序法的过程是:");
 8         for(int i=1;i<array.length;i++)
 9         {
10             for(int j=0;j<array.length-i;j++)
11             {
12                 if(array[i]>array[j+1])
13                 {
14                     int temp=array[j];
15                     array[j]=array[j+1];
16                     array[j+1]=temp;
17                 }
18                 System.out.print(array[j]+" ");
19             }
20             System.out.print("【");
21             for(int j=array.length-i;j<array.length;j++)
22             {
23                 System.out.print(array[j]+" ");
24             }
25             System.out.print("】");
26         }
27 
28     }
29 
30 }

 

 1 //冒泡排序  从小到大排
 2 public class maopao {
 3 
 4             public static void main(String[] args) {
 5         int[]a=new int[]{23,45,67,12,97,78,8,36};
 6         
 7         System.out.println("原始顺序是:");
 8         
 9         for(int t:a)
10         {
11             System.out.print(t+" ");
12         }
13         
14         System.out.println();
15         
16         int m=0;
17         
18         //循环次数0-6
19         for(int j=0;j<a.length-1;j++)
20             
21         {
22         //前后比较循环
23         for(int i=0;i<a.length-1-j;i++)
24         {
25             //比较前后元素的大小顺序
26             if(a[i]>a[i+1])
27             {
28                 //临时存放
29                 int b=a[i];
30                 
31                 a[i]=a[i+1];
32                 
33                 a[i+1]=b;
34             }
35             
36             m++;
37         }
38         System.out.print("第"+(j+1)+" 次循环后是:");
39         for(int t:a)
40         {
41             System.out.print(t+" ");
42         }
43         
44         System.out.println();
45         }
46         System.out.println("m="+m);
47     }
48 
49 }    

二分查找法

假定有序数组,首先查找中间元素,记录中间元素下标,如果中间元素不是待查找的数,则递归调用该方法;  

如果待查找的数比该中间元素小,则左边下标不变,右边下标变成中间元素下标减1,反之右边下标不变,左边下标变为中间元素下标加上1 ;

 指定递归调用跳出条件。右边下标大与左边下标时才去执行递归方法。

 1  package com.hanqi;
 2 
 3 public class maopao {
 4 
 5     public static void main(String[] args) {
 6         int middelIndex = (leftIndex + rightIndex) / 2;         
 7         int middelVal = arr[middelIndex];
 8          // 递归执行的条件        
 9          if (rightIndex >= leftIndex) 
10         {  
11             if (middelVal > findNum) 
12 13                 find(leftIndex, middelIndex - 1, findNum, arr);                         } 
14             else if (middelVal < findNum) 
15 16                 find(middelIndex + 1, rightIndex, findNum, arr);                     } 
17             else 
18 19                 System.out.println(findNum + "在数组中的下标是: " + middelIndex); 
20             }         
21 22         else 
23 24             System.out.println("数组中不存在: " + findNum);        
25          }      
26         }  
27         public static void main(String[] args) 
28 29         int arr[] = new int[] { 3, 2, 6, 9, 2, 1, 5, 7, 22, 11, 78 };         find(0, arr.length - 1, 22, arr);    
30          }     
     }

 

 

 

posted @ 2016-02-28 23:12  烟_雨_江_南  阅读(220)  评论(0编辑  收藏  举报