数组常见算法

来源:互联网 发布:蓝桥杯全国软件大赛 编辑:程序博客网 时间:2024/06/15 01:09

5, 关于数组常见算法的代码剖析:

5.1拆分查找:

[java] view plain copy
 print?
  1. class HalfSearch   
  2. {  
  3.     /* 
  4.     在一个有序的数组中查找一个元素的位置; 
  5.     例如:有数组{0,2,4,6,8,10,12}查找10的位置index=5 
  6.     */  
  7.     public static void main(String[] args)   
  8.     {  
  9.         int[] arr=new int[]{0,2,4,6,8,10,12};  
  10.         int index=getIndex(arr,12);  
  11.         System.out.println("index="+index);  
  12.     }  
  13.     //一般性做法,效率较低的算法  
  14.     /* 
  15.     public static int getIndex(int[] arr,int key) 
  16.     { 
  17.         for(int i=0;i<arr.length;i++) 
  18.         { 
  19.         if(arr[i]==key) 
  20.             return i; 
  21.         } 
  22.         return -1; 
  23.     } 
  24.     */  
  25.     //折半查找法  
  26.     /* 
  27.     public static int getIndex(int[] arr,int key) 
  28.     { 
  29.     int min=0; 
  30.     int max=arr.length-1; 
  31.     int mid=(min+max)/2; 
  32.     while(arr[mid]!=key) 
  33.         { 
  34.             if(key>arr[mid]) 
  35.                 min=mid+1; 
  36.             else 
  37.                 max=mid-1; 
  38.             if(min>max)        //判断查找不到的情况,返回-1 
  39.                 return -1; 
  40.                 mid=(min+max)/2;   //折半 
  41.         } 
  42.         return mid; 
  43.     } 
  44.     */  
  45.     //折半查找法的第二种方式:效率稍高  
  46.   
  47.     public static int getIndex(int[] arr,int key)  
  48.     {  
  49.         int min=0;  
  50.         int max=arr.length-1;  
  51.         int mid;  
  52.         while(min<=max)  
  53.         {  
  54.             mid=(min+max)/2;  
  55.             if(key>arr[mid])  
  56.                 min=mid+1;  
  57.             else if(key<arr[mid])  
  58.                 max=mid-1;  
  59.             else  
  60.                 return mid;  
  61.         }  
  62.         return -1;  
  63.     }  
  64. }  

5.2选择排序:

[java] view plain copy
 print?
  1. class SelectSort   
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.         int[] arr=new int[]{2,1,5,3,6,4,8,9,7};  
  6.         System.out.println("原数组:");  
  7.         Print(arr);  
  8.         System.out.println();  
  9.         System.out.println("排序后:");  
  10.         SelectSort(arr);  
  11.     }  
  12.     //选择排序,对一个数组进行升序排列  
  13.     //打印原数组  
  14.     public static void Print(int[] arr)  
  15.     {  
  16.         System.out.print("[ ");  
  17.         for(int i=0;i<arr.length;i++)  
  18.             {  
  19.                 if(i!=arr.length-1)  
  20.                     System.out.print(arr[i]+", ");  
  21.                 else  
  22.                     System.out.print(arr[i]+"]");  
  23.             }  
  24.     }  
  25.     //选择排序  
  26.     public static void SelectSort(int[] arr)  
  27.     {  
  28.         for(int i=0;i<arr.length-1;i++)  
  29.             {  
  30.                 for(int j=i+1;j<arr.length;j++)  
  31.                     {  
  32.                         if(arr[i]>arr[j])  
  33.                             {  
  34.                                 int temp=arr[i];  
  35.                                 arr[i]=arr[j];  
  36.                                 arr[j]=temp;  
  37.                             }  
  38.                     }  
  39.             }  
  40.         Print(arr);  
  41.     }  
  42. }  

5.3冒泡排序:

[java] view plain copy
 print?
  1. class  BubbleSort  
  2. {  
  3.     public static void main(String[] args)   
  4.     {  
  5.         int[] arr=new int[]{2,1,5,3,6,4,8,9,7};  
  6.         System.out.println("原数组:");  
  7.         Print(arr);  
  8.         System.out.println();  
  9.         System.out.println("排序后:");  
  10.         BubbleSort(arr);  
  11.     }  
  12.     //打印原数组  
  13.     public static void Print(int[] arr)  
  14.     {  
  15.         System.out.print("[ ");  
  16.         for(int i=0;i<arr.length;i++)  
  17.             {  
  18.                 if(i!=arr.length-1)  
  19.                     System.out.print(arr[i]+", ");  
  20.                 else  
  21.                     System.out.print(arr[i]+"]");  
  22.             }  
  23.     }  
  24.     //冒泡排序  
  25.     public static void BubbleSort(int[] arr)  
  26.     {  
  27.         for(int i=0;i<arr.length;i++)                  //for(int i=arr.length-1;i>0;i--)  
  28.         {  
  29.             for(int j=0;j<arr.length-i-1;j++)        //for(int j=0;j<x;j++)  
  30.             {  
  31.                 if(arr[j]>arr[j+1])  
  32.                 {  
  33.                 int temp=arr[j];  
  34.                 arr[j]=arr[j+1];  
  35.                 arr[j+1]=temp;  
  36.                 }  
  37.             }  
  38.         }  
  39.         Print(arr);  
  40.     }  
  41. }  

6.操作数组的工具类<Arrays>

Java.util包中封装了很多关于java数组的工具方法,这些方法都是静态的。

binarySearch;;(type[] a, type key) : 使用二分搜索法来搜索指定的 type 型数组,以获得指定的值。

binarySearchtype[] a, int fromIndex,int toIndex, type key) : 使用二分搜索法来搜索指定的 type 型数组的范围,以获得指定的值。

copyOfRange(type[] original, int from,int to)de> : 将指定数组的指定范围复制到一个新数组。

equals(type[] a, type[] a2) :如果两个指定的 char 型数组彼此相等,则返回true

fill(type[] a, type val) :将指定的 byte 值分配给指定 type 节型数组的每个元素。

fill(type[] a, int fromIndex,int toIndex, type val) :将指定的 type 值分配给指定 type 型数组指定范围中的每个元素。

sort(type[] a) :对指定的 type 型数组按数字升序进行排序。

sort(type[] a, int fromIndex,int toIndex) :对指定type 型数组的指定范围按数字升序进行排序。

toString(type[] a):返回指定数组内容的字符串表示形式。

详情参见JDK 1.6API文档,有更好的解释。


总结:关于数组这一节的知识并不多,主要要学会数组的定义,理解好数组在内存中的表现形式,这对日后开发有很好的帮助。除此之外,还要掌握以上常见的排序算法,尤其是冒泡排序,虽然在开发中不常见,但是在面试时出现的概率很大。

此外,通过查阅API文档,发现了操作数组工具类Arrays有很多方法,在这里做了一些常见方法的总结,这些方法都是静态的,相当实用,例如sort方法,最好牢记下来,以便开发时直接拿来用,不必翻阅API文档。


0 0