练习1:冒泡法排序

 1 /*
 2 2 5 1 8 4 比较趟次i 比较次数j j=数组长度-i
 3 2 1 5 4 8
 4 1 2 4 5 8
 5 1 2 4 5 8
 6 */
 7 class  BubbleMethod
 8 {
 9     public static void main(String[] args) 
10     {
11         int[] a={2,92,34,54,28};
12         bubbleMethod(a);
13     }
14     public static void bubbleMethod(int[] a)
15     {
16         for(int i=1;i<a.length;i++)
17         {
18             for(int j=1;j<=a.length-i;j++)
19             {
20                 if(a[j-1]>a[j])
21                 {
22                     int temp = a[j-1];
23                     a[j-1] = a[j];
24                     a[j] = temp;
25                 }
26             }
27         }
28         for(int i=0;i<a.length;i++)
29         {
30             System.out.print(a[i]+" ");
31         }
32     }
33 }

 

练习2:求最值

 1 class  ZuiZhi
 2 {
 3     public static void main(String[] args) 
 4     {
 5         int[] a={2,92,34,54,28};
 6         minMethod(a);
 7         maxMethod(a);
 8     }
 9     public static void minMethod(int[] a)
10     {
11         int min=a[0];//不能初始化为0 数组中可能有负数
12         for (int i=0;i<a.length;i++ )
13         {
14             if(a[i]<min)
15                 min=a[i];
16         }
17         System.out.println("数组内最小的元素是:"+min);
18     }
19     public static void maxMethod(int[] a)
20     {
21         int max=a[0];//不能初始化为0 数组中可能有负数
22         for (int i=0;i<a.length;i++ )
23         {
24             if(a[i]>max)
25                 max=a[i];
26         }
27         System.out.println("数组内最大的元素是:"+max);
28     }
29 
30 }

 

练习3:选择排序

 

 1 class  SelectSort
 2 {
 3     public static void main(String[] args) 
 4     {
 5         int[] a={2,92,34,54,28};
 6         selectSort(a);
 7     }
 8     public static void selectSort(int[] a)
 9     {
10         for (int i=0;i<a.length-1;i++ )
11         {
12             for (int j=i+1;j<a.length;j++ )
13             {
14                 if(a[j]<a[i])
15                 {
16                     int temp = a[i];
17                     a[i] = a[j];
18                     a[j] =temp;
19                 }
20 
21             }
22         }
23         for(int i=0;i<a.length;i++)
24         {
25             System.out.print(a[i]+" ");
26         }
27     }
28 }

 

练习4:二分查找

 1 /*
 2 折半查找:
 3 3 5 7 9 10 14 
 4 min=0 max=length-1 mid=(max+min)/2
 5 */
 6 class BinarySearch 
 7 {
 8     public static void main(String[] args) 
 9     {
10         int[] a={3,5,7,9,10,14};
11         int index=binarySearch_1(a,7);
12         System.out.println(index);
13         index=binarySearch_2(a,14);
14         System.out.println(index);    
15     }
16     public static int binarySearch_1(int[] a,int key)
17     {
18         int min=0,mid,max=a.length-1;
19         mid=(min+max)/2;
20         while (key!=a[mid])
21         {
22             if(a[mid]>key)
23             {
24                 max=mid-1;
25             }
26             else  if (a[mid]<key)
27             {
28                 min=mid+1;
29             }
30             if (max<min)
31                 return -1;
32             mid=(min+max)/2;
33 
34         }
35         return mid;
36     }
37     public static int binarySearch_2(int[] a,int key)
38     {
39         int min=0,max=a.length-1,mid;
40         while (min<=max)
41         {
42             mid =(max+min)>>1;
43             if(a[mid]>key)
44             {
45                 max=mid-1;
46             }
47             else  if (a[mid]<key)
48             {
49                 min=mid+1;
50             }
51             else 
52                 return mid;
53         }
54         return -1;
55     }
56     
57 }

 

posted on 2012-06-03 14:06  Lincon Ma  阅读(108)  评论(0)    收藏  举报