數組的經常使用操做

1、排序(冒泡排序、選擇排序、插入排序)數組

  1.冒泡排序spa

 1 /**
 2   * 對指定的 int 型數組按數字升序進行冒泡排序
 3   * @param a 待排序的數組
 4   */
 5 public static void sortBubble(int[] a) {
 6     int temp;
 7     for (int i = 1; i < a.length; i++) {
 8         for (int j = 0; j < a.length - i; j++) {
 9             if (a[j] > a[j+1]) {
10                 temp = a[j];//三步交換
11                 a[j] = a[j+1];
12                 a[j+1] = temp;
13             }
14         }
15     }
16 }  

 注:若要降序排序,僅需將if()條件裏的">"改爲"<"便可code

  2.選擇排序對象

 1 /**
 2  * 對指定的 int 型數組按數字升序進行選擇排序
 3  * @param a 待排序的數組
 4  */
 5 public static void sortSelection(int[] a) {
 6     int flag;//標記最小值得位置
 7     int temp;//用於交換的中間變量
 8     for (int i = 0; i < a.length; i++) {
 9         flag = i;
10         for (int j = i; j < a.length; j++) {
11             if (a[j] < a[flag]) {
12                 flag = j;
13             }
14         }
15         temp = a[i];//三步交換
16         a[i] = a[flag];
17         a[flag] = temp;
18     }
19 }

 注:若要降序排序,僅需將if()條件裏的"<"改爲">"便可blog

  3.插入排序排序

 1 /**
 2  * 對指定的 int 型數組按數字升序進行插入排序
 3  * @param a 待排序的數組
 4  */
 5 public static void sortInsertion(int[] a) {
 6     int flag = 0;//標記須要插入的位置
 7     int temp = 0;//存儲待插入的數
 8     for (int i = 1; i < a.length; i++) {
 9         temp = a[i];
10         for (int j = i; j > 0; j--) {
11             if (a[j-1] > temp) {
12                 a[j] = a[j-1];
13                 flag = j-1;
14             }else {
15                 flag = j;
16                 break;
17             }
18         }
19         a[flag] = temp;
20     }
21 }

 注:若要降序排序,僅需將if()條件裏的">"改爲"<"便可索引

 

2、查找(順序查找、折半查找)字符串

  1.順序查找io

 1 /**
 2  * 使用線性搜索法來搜索指定的 int 型數組中搜索指定的值,以得到該的值的位置
 3  * @param a 要搜索的數組
 4  * @param key 要搜索的值 
 5  * @return -若是這個值被找到,則返回該值得索引,不然返回 -1<br>
 6  *         -若是數組中有多個能夠匹配的值,則返回第一的值的索引<br>
 7  *         -調用該方法以前不須要對數組進行排序
 8  */
 9 public static int searchLinear(int[] a,int key) {
10     for (int i = 0; i < a.length; i++) {
11         if (a[i] == key) {
12             return i;
13         }
14     }
15     return -1;
16 }

  注:線性搜索是最保險的查找方法,但也是效率最低的class

  2.折半查找

 1 /**
 2  * 使用二分查找來搜索指定的 int 型數組中搜索指定的值,以得到該的值的位置
 3  * @param a 要搜索的數組
 4  * @param key 要搜索的值 
 5  * @return -若是這個值被找到,則返回該值得索引,不然返回 -1<br>
 6  *         -若是數組包含多個等於指定對象的元素,則沒法保證找到的是哪個<br>
 7  *         -在進行此調用以前,必須根據元素的天然順序對數組進行升序排序
 8  *         -若是沒有對數組進行排序,則結果是不肯定的
 9  */
10 public static int searchBinary(int[] a,int key) {
11     int minIndex = 0;//首位置
12     int maxIndex = a.length - 1;//末位置
13     int middleIndex;//中間位置
14     while (maxIndex >= minIndex) {
15         middleIndex = (minIndex + maxIndex)/2;
16         if (a[middleIndex] == key) {
17             return middleIndex;
18         }else if (a[middleIndex] > key) {
19             maxIndex = middleIndex - 1;
20         }else if (a[middleIndex] < key){
21             minIndex = middleIndex + 1;
22         }
23     }
24     return -1;
25 }

 注:折半查找有點繞,但相對來講也是效率最高的,但必須是已經排序完成的數組,且對排序的升序或者降序也有要求

 

3、插入

 1 /**
 2  * 插入--將一個int型數據插入到指定數組的指定位置
 3  * @param arr 待插入的數組
 4  * @param k 要插入的位置--從0開始算
 5  * @param a 要插入的數據
 6  * @return -返回插入後的新數組<br>
 7  *             -返回的是一個新數組,原待插入數組不變
 8  */
 9 public static int[] insert(int[] arr,int k,int a) {
10     int[] brr = Arrays.copyOf(arr, arr.length+1);
11     int flag = brr.length - 1;
12     for (int i = brr.length - 2; i >= k; i--) {
13         brr[i + 1] = brr[i];
14         flag = i;
15     }
16     brr[flag] = a;
17     return brr;
18 }

 看註釋

 

4、刪除

 1 /**
 2  * 從指定數組中刪除指定的數  若沒有找到要刪除的數,則返回原數組
 3  * @param arr 待處理的數組
 4  * @param num 要刪除的數
 5  * @return -返回處理後的新數組
 6  *             -原數組不會被改變
 7  */
 8 public static int[] delete(int[] arr,int num) {
 9     int flag = searchLinear(arr, num);//要刪除值得索引
10     if (flag == -1) {//沒有找到刪除點
11         return arr;
12     }
13     int[] brr = new int[arr.length - 1];
14     for (int i = 0; i < flag; i++) {
15         brr[i] = arr[i];
16     }
17     for (int i = flag; i < brr.length; i++) {
18         brr[i] = arr[i + 1];
19     }
20     return brr;
21 }

 看註釋

 

5、倒敘

 1 /**
 2  * 對int型數組進行倒敘操做  
 3  * 該方法直接對原數組進行操做,不會返回新的數組
 4  * @param arr 待反轉的數組
 5  */
 6 public static void reversal(int[] arr) {
 7     int temp;
 8     for (int i = 0; i < arr.length/2; i++) {
 9         temp = arr[i];
10         arr[i] = arr[arr.length-i-1];
11         arr[arr.length-i-1] = temp;
12     }
13 }

 看註釋

 

附加:對字符串數組進行排序

 1 /**
 2  * 對指定的 String 型數組按ASCII升序進行排序
 3  * @param str -要排序的數組
 4  */
 5 public static void sort(String[] str) {
 6     int flag = 0;//標記須要插入的位置
 7     String temp = "";//存儲待插入的字符串
 8     for (int i = 1; i < str.length; i++) {
 9         temp = str[i];
10         for (int j = i; j > 0; j--) {
11             if (str[j-1].compareTo(temp) > 0) {
12                 str[j] = str[j-1];
13                 flag = j-1;
14             }else {
15                 flag = j;
16                 break;
17             }
18         }
19         str[flag] = temp;
20     }
21 }

 注:這也是普通的排序,僅僅須要把if()條件稍微改一下

同理,改過以後也能夠對其它數組進行排序,如類的數組

 

 

禁止轉載

張巖

相關文章
相關標籤/搜索