zoukankan      html  css  js  c++  java
  • Java数组操作工具

    原文地址:http://blog.csdn.net/qq446282412/article/details/8913690
    2013-05-11 10:27
     
    看到网上的一段关于对数组操作的代码,觉得有用,在此备用。
    [java] view plaincopy
     
    1. <pre name="code" class="java">import java.util.ArrayList;  
    2. import java.util.Arrays;  
    3. import java.util.List;  
    4. import java.util.Map;  
    5. import java.util.Random;  
    6. import java.util.TreeMap;  
    7.   
    8. /** 
    9.  * @desc 数组操作工具 
    10.  * @author OuyangPeng 
    11.  * @datatime 2013-5-11 10:31:02 
    12.  *  
    13.  */  
    14. public class MyArrayUtils {  
    15.   
    16.     /** 
    17.      * 排序算法的分类如下: 1.插入排序(直接插入排序、折半插入排序、希尔排序); 2.交换排序(冒泡泡排序、快速排序); 
    18.      * 3.选择排序(直接选择排序、堆排序); 4.归并排序; 5.基数排序。 
    19.      *  
    20.      * 关于排序方法的选择: (1)若n较小(如n≤50),可采用直接插入或直接选择排序。 
    21.      * (2)若文件初始状态基本有序(指正序),则应选用直接插人、冒泡或随机的快速排序为宜; 
    22.      * (3)若n较大,则应采用时间复杂度为O(nlgn)的排序方法:快速排序、堆排序或归并排序。 
    23.      *  
    24.      */  
    25.   
    26.     /** 
    27.      * 交换数组中两元素 
    28.      *  
    29.      * @since 1.1 
    30.      * @param ints 
    31.      *            需要进行交换操作的数组 
    32.      * @param x 
    33.      *            数组中的位置1 
    34.      * @param y 
    35.      *            数组中的位置2 
    36.      * @return 交换后的数组 
    37.      */  
    38.     public static int[] swap(int[] ints, int x, int y) {  
    39.         int temp = ints[x];  
    40.         ints[x] = ints[y];  
    41.         ints[y] = temp;  
    42.         return ints;  
    43.     }  
    44.   
    45.     /** 
    46.      * 冒泡排序方法:相邻两元素进行比较 性能:比较次数O(n^2),n^2/2;交换次数O(n^2),n^2/4<br> 
    47.      * 冒泡排序(Bubble Sort)是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,<br> 
    48.      * 如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,<br> 
    49.      * 也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。<br> 
    50.           冒泡排序算法的运作如下:<br> 
    51.          1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。<br> 
    52.          2. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。<br> 
    53.          3. 针对所有的元素重复以上的步骤,除了最后一个。<br> 
    54.          4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。<br> 
    55.      * @since 1.1 
    56.      * @param source 
    57.      *            需要进行排序操作的数组 
    58.      * @return 排序后的数组 
    59.      */  
    60.     public static int[] bubbleSort(int[] source) {  
    61.         /*for (int i = 0; i < source.length - 1; i++) { // 最多做n-1趟排序 
    62.             for (int j = 0; j < source.length - i - 1; j++) { // 对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的) 
    63.                 if (source[j] > source[j + 1]) { // 把大的值交换到后面 
    64.                     swap(source, j, j + 1); 
    65.                 } 
    66.             } 
    67.         }*/  
    68.         for (int i = source.length - 1; i>0 ; i--) {   
    69.             for (int j = 0; j < i; j++) {   
    70.                 if (source[j] > source[j + 1]) {   
    71.                     swap(source, j, j + 1);  
    72.                 }  
    73.             }  
    74.         }  
    75.         return source;  
    76.     }  
    77.   
    78.     /** 
    79.      * 选择排序法 方法:选择排序(Selection sort)是一种简单直观的排序算法,其平均时间复杂度为O(n2)。 
    80.      *      它的工作原理如下。首先在未排序序列中找到最小元素,存放到排序序列的起始位置,然后, 
    81.      *      再从剩余未排序元素中继续寻找最小元素,然后放到排序序列末尾。以此类推,直到所有元素均排序完毕。 
    82.      * 性能:选择排序的交换操作介于0和(n-1)次之间, 选择排序的比较操作为n(n-1)/2次之间, 
    83.      *       选择排序的赋值操作介于0和3(n-1)次之间,其平均时间复杂度为O(n2) 
    84.      * 交换次数比冒泡排序少多了,由于交换所需CPU时间比比较所需的CUP时间多,所以选择排序比冒泡排序快。 
    85.      * 但是N比较大时,比较所需的CPU时间占主要地位,所以这时的性能和冒泡排序差不太多,但毫无疑问肯定要快些。 
    86.      *  
    87.      * @since 1.1 
    88.      * @param source 
    89.      *            需要进行排序操作的数组 
    90.      * @return 排序后的数组 
    91.      */  
    92.     public static int[] selectSort(int[] source) {  
    93.         for (int i = 0; i < source.length; i++) {  
    94.             for (int j = i + 1; j < source.length; j++) {  
    95.                 if (source[i] > source[j]) {  
    96.                     swap(source, i, j);  
    97.                 }  
    98.             }  
    99.         }  
    100.         return source;  
    101.     }  
    102.   
    103.     /** 
    104.      * 插入排序 方法:将一个记录插入到已排好序的有序表(有可能是空表)中,从而得到一个新的记录数增1的有序表。 性能:比较次数O(n^2),n^2/4 
    105.      * 复制次数O(n),n^2/4 比较次数是前两者的一般,而复制所需的CPU时间较交换少,所以性能上比冒泡排序提高一倍多,而比选择排序也要快。 
    106.      *  
    107.      * @since 1.1 
    108.      * @param source 
    109.      *            需要进行排序操作的数组 
    110.      * @return 排序后的数组 
    111.      */  
    112.     public static int[] insertSort(int[] source) {  
    113.   
    114.         for (int i = 1; i < source.length; i++) {  
    115.             for (int j = i; (j > 0) && (source[j] < source[j - 1]); j--) {  
    116.                 swap(source, j, j - 1);  
    117.             }  
    118.         }  
    119.         return source;  
    120.     }  
    121.   
    122.     /** 
    123.      * 快速排序 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists)。 步骤为: 
    124.      * 1. 从数列中挑出一个元素,称为 "基准"(pivot), 2. 
    125.      * 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面 
    126.      * (相同的数可以到任一边)。在这个分割之后,该基准是它的最后位置。这个称为分割(partition)操作。 3. 
    127.      * 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。 
    128.      * 递回的最底部情形,是数列的大小是零或一,也就是永远都已经被排序好了 
    129.      * 。虽然一直递回下去,但是这个算法总会结束,因为在每次的迭代(iteration)中,它至少会把一个元素摆到它最后的位置去。 
    130.      *  
    131.      * @since 1.1 
    132.      * @param source 
    133.      *            需要进行排序操作的数组 
    134.      * @return 排序后的数组 
    135.      */  
    136.     public static int[] quickSort(int[] source) {  
    137.         return qsort(source, 0, source.length - 1);  
    138.     }  
    139.   
    140.     /** 
    141.      * 快速排序的具体实现,排正序 
    142.      *  
    143.      * @since 1.1 
    144.      * @param source 
    145.      *            需要进行排序操作的数组 
    146.      * @param low 
    147.      *            开始低位 
    148.      * @param high 
    149.      *            结束高位 
    150.      * @return 排序后的数组 
    151.      */  
    152.     private static int[] qsort(int source[], int low, int high) {  
    153.         int i, j, x;  
    154.         if (low < high) {  
    155.             i = low;  
    156.             j = high;  
    157.             x = source[i];  
    158.             while (i < j) {  
    159.                 while (i < j && source[j] > x) {  
    160.                     j--;  
    161.                 }  
    162.                 if (i < j) {  
    163.                     source[i] = source[j];  
    164.                     i++;  
    165.                 }  
    166.                 while (i < j && source[i] < x) {  
    167.                     i++;  
    168.                 }  
    169.                 if (i < j) {  
    170.                     source[j] = source[i];  
    171.                     j--;  
    172.                 }  
    173.             }  
    174.             source[i] = x;  
    175.             qsort(source, low, i - 1);  
    176.             qsort(source, i + 1, high);  
    177.         }  
    178.         return source;  
    179.     }  
    180.   
    181.     // /////////////////////////////////////////////  
    182.     // 排序算法结束  
    183.     // ////////////////////////////////////////////  
    184.     /** 
    185.      * 二分法查找 查找线性表必须是有序列表 
    186.      *  
    187.      * @since 1.1 
    188.      * @param source 
    189.      *            需要进行查找操作的数组 
    190.      * @return 需要查找的值在数组中的位置,若未查到则返回-1 
    191.      */  
    192.     public static int[] binarySearch(int[] source) {  
    193.         int i,j;  
    194.         int low, high, mid;  
    195.         int temp;  
    196.         for (i = 0; i < source.length; i++) {  
    197.             temp=source[i];  
    198.             low=0;  
    199.             high=i-1;  
    200.             while (low <= high) {  
    201.                 mid = (low + high)/2;  
    202.                 if (source[mid]>temp) {  
    203.                     high=mid-1;  
    204.                 } else {  
    205.                     low = mid + 1;  
    206.                 }  
    207.             }  
    208.             for (j= i-1; j>high;j--)   
    209.                 source[j+1]=source[j];  
    210.             source[high+1]=temp;  
    211.         }  
    212.         return source;  
    213.     }  
    214.   
    215.     /** 
    216.      * 反转数组 
    217.      *  
    218.      * @since 1.1 
    219.      * @param source 
    220.      *            需要进行反转操作的数组 
    221.      * @return 反转后的数组 
    222.      */  
    223.     public static int[] reverse(int[] source) {  
    224.         int length = source.length;  
    225.         int temp = 0;  
    226.         for (int i = 0; i < length >> 1; i++) {  
    227.             temp = source[i];  
    228.             source[i] = source[length - 1 - i];  
    229.             source[length - 1 - i] = temp;  
    230.         }  
    231.         return source;  
    232.     }  
    233.   
    234.     /** 
    235.      * 在当前位置插入一个元素,数组中原有元素向后移动; 如果插入位置超出原数组,则抛IllegalArgumentException异常 
    236.      *  
    237.      * @param array 
    238.      * @param index 
    239.      * @param insertNumber 
    240.      * @return 
    241.      */  
    242.     public static int[] insert(int[] array, int index, int insertNumber) {  
    243.         if (array == null || array.length == 0) {  
    244.             throw new IllegalArgumentException();  
    245.         }  
    246.         if (index - 1 > array.length || index <= 0) {  
    247.             throw new IllegalArgumentException();  
    248.         }  
    249.         int[] dest = new int[array.length + 1];  
    250.         System.arraycopy(array, 0, dest, 0, index - 1);  
    251.         dest[index - 1] = insertNumber;  
    252.         System.arraycopy(array, index - 1, dest, index, dest.length - index);  
    253.         return dest;  
    254.     }  
    255.   
    256.     /** 
    257.      * 整形数组中特定位置删除掉一个元素,数组中原有元素向前移动; 如果插入位置超出原数组,则抛IllegalArgumentException异常 
    258.      *  
    259.      * @param array 
    260.      * @param index 
    261.      * @return 
    262.      */  
    263.     public static int[] remove(int[] array, int index) {  
    264.         if (array == null || array.length == 0) {  
    265.             throw new IllegalArgumentException();  
    266.         }  
    267.         if (index > array.length || index <= 0) {  
    268.             throw new IllegalArgumentException();  
    269.         }  
    270.         int[] dest = new int[array.length - 1];  
    271.         System.arraycopy(array, 0, dest, 0, index - 1);  
    272.         System.arraycopy(array, index, dest, index - 1, array.length - index);  
    273.         return dest;  
    274.     }  
    275.   
    276.     /** 
    277.      * 2个数组合并,形成一个新的数组 
    278.      *  
    279.      * @param array1 
    280.      * @param array2 
    281.      * @return 
    282.      */  
    283.     public static int[] merge(int[] array1, int[] array2) {  
    284.         int[] dest = new int[array1.length + array2.length];  
    285.         System.arraycopy(array1, 0, dest, 0, array1.length);  
    286.         System.arraycopy(array2, 0, dest, array1.length, array2.length);  
    287.         return dest;  
    288.     }  
    289.   
    290.     /** 
    291.      * 数组中有n个数据,要将它们顺序循环向后移动k位, 即前面的元素向后移动k位,后面的元素则循环向前移k位, 
    292.      * 例如,0、1、2、3、4循环移动3位后为2、3、4、0、1。 
    293.      *  
    294.      * @param array 
    295.      * @param offset 
    296.      * @return 
    297.      */  
    298.     public static int[] offsetArray(int[] array, int offset) {  
    299.         int length = array.length;  
    300.         int moveLength = length - offset;  
    301.         int[] temp = Arrays.copyOfRange(array, moveLength, length);  
    302.         System.arraycopy(array, 0, array, offset, moveLength);  
    303.         System.arraycopy(temp, 0, array, 0, offset);  
    304.         return array;  
    305.     }  
    306.   
    307.     /** 
    308.      * 随机打乱一个数组 
    309.      *  
    310.      * @param list 
    311.      * @return 
    312.      */  
    313.     public static List shuffle(List list) {  
    314.         java.util.Collections.shuffle(list);  
    315.         return list;  
    316.     }  
    317.   
    318.     /** 
    319.      * 随机打乱一个数组 
    320.      *  
    321.      * @param array 
    322.      * @return 
    323.      */  
    324.     public int[] shuffle(int[] array) {  
    325.         Random random = new Random();  
    326.         for (int index = array.length - 1; index >= 0; index--) {  
    327.             // 从0到index处之间随机取一个值,跟index处的元素交换  
    328.             exchange(array, random.nextInt(index + 1), index);  
    329.         }  
    330.         return array;  
    331.     }  
    332.   
    333.     // 交换位置  
    334.     private void exchange(int[] array, int p1, int p2) {  
    335.         int temp = array[p1];  
    336.         array[p1] = array[p2];  
    337.         array[p2] = temp;  
    338.     }  
    339.   
    340.     /** 
    341.      * 对两个有序数组进行合并,并将重复的数字将其去掉 
    342.      *  
    343.      * @param a 
    344.      *            :已排好序的数组a 
    345.      * @param b 
    346.      *            :已排好序的数组b 
    347.      * @return 合并后的排序数组 
    348.      */  
    349.     private static List<Integer> mergeByList(int[] a, int[] b) {  
    350.         // 用于返回的新数组,长度可能不为a,b数组之和,因为可能有重复的数字需要去掉  
    351.         List<Integer> c = new ArrayList<Integer>();  
    352.         // a数组下标  
    353.         int aIndex = 0;  
    354.         // b数组下标  
    355.         int bIndex = 0;  
    356.         // 对a、b两数组的值进行比较,并将小的值加到c,并将该数组下标+1,  
    357.         // 如果相等,则将其任意一个加到c,两数组下标均+1  
    358.         // 如果下标超出该数组长度,则退出循环  
    359.         while (true) {  
    360.             if (aIndex > a.length - 1 || bIndex > b.length - 1) {  
    361.                 break;  
    362.             }  
    363.             if (a[aIndex] < b[bIndex]) {  
    364.                 c.add(a[aIndex]);  
    365.                 aIndex++;  
    366.             } else if (a[aIndex] > b[bIndex]) {  
    367.                 c.add(b[bIndex]);  
    368.                 bIndex++;  
    369.             } else {  
    370.                 c.add(a[aIndex]);  
    371.                 aIndex++;  
    372.                 bIndex++;  
    373.             }  
    374.         }  
    375.         // 将没有超出数组下标的数组其余全部加到数组c中  
    376.         // 如果a数组还有数字没有处理  
    377.         if (aIndex <= a.length - 1) {  
    378.             for (int i = aIndex; i <= a.length - 1; i++) {  
    379.                 c.add(a[i]);  
    380.             }  
    381.             // 如果b数组中还有数字没有处理  
    382.         } else if (bIndex <= b.length - 1) {  
    383.             for (int i = bIndex; i <= b.length - 1; i++) {  
    384.                 c.add(b[i]);  
    385.             }  
    386.         }  
    387.         return c;  
    388.     }  
    389.   
    390.     /** 
    391.      * 对两个有序数组进行合并,并将重复的数字将其去掉 
    392.      *  
    393.      * @param a 
    394.      *            :已排好序的数组a 
    395.      * @param b 
    396.      *            :已排好序的数组b 
    397.      * @return合并后的排序数组,返回数组的长度=a.length + b.length,不足部分补0 
    398.      */  
    399.     private static int[] mergeByArray(int[] a, int[] b) {  
    400.         int[] c = new int[a.length + b.length];  
    401.   
    402.         int i = 0, j = 0, k = 0;  
    403.   
    404.         while (i < a.length && j < b.length) {  
    405.             if (a[i] <= b[j]) {  
    406.                 if (a[i] == b[j]) {  
    407.                     j++;  
    408.                 } else {  
    409.                     c[k] = a[i];  
    410.                     i++;  
    411.                     k++;  
    412.                 }  
    413.             } else {  
    414.                 c[k] = b[j];  
    415.                 j++;  
    416.                 k++;  
    417.             }  
    418.         }  
    419.         while (i < a.length) {  
    420.             c[k] = a[i];  
    421.             k++;  
    422.             i++;  
    423.         }  
    424.         while (j < b.length) {  
    425.             c[k] = b[j];  
    426.             j++;  
    427.             k++;  
    428.         }  
    429.         return c;  
    430.     }  
    431.   
    432.     /** 
    433.      * 对两个有序数组进行合并,并将重复的数字将其去掉 
    434.      *  
    435.      * @param a 
    436.      *            :可以是没有排序的数组 
    437.      * @param b 
    438.      *            :可以是没有排序的数组 
    439.      * @return合并后的排序数组 打印时可以这样: Map<Integer,Integer> map=sortByTreeMap(a,b); 
    440.      *                 Iterator iterator = map.entrySet().iterator(); while 
    441.      *                 (iterator.hasNext()) { Map.Entry mapentry = 
    442.      *                 (Map.Entry)iterator.next(); 
    443.      *                 System.out.print(mapentry.getValue()+" "); } 
    444.      */  
    445.     private static Map<Integer, Integer> mergeByTreeMap(int[] a, int[] b) {  
    446.         Map<Integer, Integer> map = new TreeMap<Integer, Integer>();  
    447.         for (int i = 0; i < a.length; i++) {  
    448.             map.put(a[i], a[i]);  
    449.         }  
    450.         for (int i = 0; i < b.length; i++) {  
    451.             map.put(b[i], b[i]);  
    452.         }  
    453.         return map;  
    454.     }  
    455.   
    456.     /** 
    457.      * 在控制台打印数组,之间用逗号隔开,调试时用 
    458.      *  
    459.      * @param array 
    460.      */  
    461.     public static String print(int[] array) {  
    462.         StringBuffer sb = new StringBuffer();  
    463.         for (int i = 0; i < array.length; i++) {  
    464.             sb.append("," + array[i]);  
    465.         }  
    466.         System.out.println(" "+sb.toString().substring(1));  
    467.         return sb.toString().substring(1);  
    468.     }  
    469. }</pre><br>  
    470. <br>  
    471. <pre></pre>  
    472. <pre></pre>  
  • 相关阅读:
    LeetCode113. 路径总和 II
    LeetCode257. 二叉树的所有路径
    LeetCode222. 完全二叉树的节点个数
    LeetCode404. 左叶子之和
    LeetCode110. 平衡二叉树
    LeetCode101. 对称二叉树
    LeetCode100. 相同的树
    llustrator CC2017下载AI2020
    vs code 代码格式化整理
    人生格言
  • 原文地址:https://www.cnblogs.com/m-xy/p/3203052.html
Copyright © 2011-2022 走看看