public class ArrayUtil extends Object
| Constructor and Description |
|---|
ArrayUtil() |
| Modifier and Type | Method and Description |
|---|---|
static int |
binarySearch(int[] source,
int key)
二分法查找 查找线性表必须是有序列表
|
static int[] |
bubbleSort(int[] source)
冒泡排序 方法:相邻两元素进行比较 性能:比较次数O(n^2),n^2/2;交换次数O(n^2),n^2/4
|
static int |
indexOf(byte[] s,
byte[] value,
int index) |
static int |
indexOf(byte[] s,
byte value,
int index)
首次出现的位置
|
static int[] |
insert(int[] array,
int index,
int insertNumber)
在当前位置插入一个元素,数组中原有元素向后移动; 如果插入位置超出原数组,则抛IllegalArgumentException异常
|
static int[] |
insertSort(int[] source)
插入排序 方法:将一个记录插入到已排好序的有序表(有可能是空表)中,从而得到一个新的记录数增1的有序表。 性能:比较次数O(n^2),n^2/4
复制次数O(n),n^2/4 比较次数是前两者的一般,而复制所需的CPU时间较交换少,所以性能上比冒泡排序提高一倍多,而比选择排序也要快。
|
static int[] |
merge(int[] array1,
int[] array2)
2个数组合并,形成一个新的数组
|
static int[] |
offsetArray(int[] array,
int offset)
数组中有n个数据,要将它们顺序循环向后移动k位, 即前面的元素向后移动k位,后面的元素则循环向前移k位,
例如,0、1、2、3、4循环移动3位后为2、3、4、0、1。
|
static String |
print(int[] array)
在控制台打印数组,之间用逗号隔开,调试时用
|
static int[] |
quickSort(int[] source)
快速排序 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists)。 步骤为:
1.
|
static int[] |
remove(int[] array,
int index)
整形数组中特定位置删除掉一个元素,数组中原有元素向前移动; 如果插入位置超出原数组,则抛IllegalArgumentException异常
|
static int[] |
reverse(int[] source)
反转数组
|
static int |
searchOf(byte[] s,
byte value,
int index)
查找个数
|
static int[] |
selectSort(int[] source)
直接选择排序法 方法:每一趟从待排序的数据元素中选出最小(或最大)的一个元素, 顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。
性能:比较次数O(n^2),n^2/2 交换次数O(n),n
交换次数比冒泡排序少多了,由于交换所需CPU时间比比较所需的CUP时间多,所以选择排序比冒泡排序快。
但是N比较大时,比较所需的CPU时间占主要地位,所以这时的性能和冒泡排序差不太多,但毫无疑问肯定要快些。
|
int[] |
shuffle(int[] array)
随机打乱一个数组
|
static List |
shuffle(List list)
随机打乱一个数组
|
static int[] |
swap(int[] ints,
int x,
int y)
交换数组中两元素
|
public static int[] swap(int[] ints,
int x,
int y)
ints - 需要进行交换操作的数组x - 数组中的位置1y - 数组中的位置2public static int[] bubbleSort(int[] source)
source - 需要进行排序操作的数组public static int[] selectSort(int[] source)
source - 需要进行排序操作的数组public static int[] insertSort(int[] source)
source - 需要进行排序操作的数组public static int[] quickSort(int[] source)
source - 需要进行排序操作的数组public static int binarySearch(int[] source,
int key)
source - 需要进行查找操作的数组key - 需要查找的值public static int[] reverse(int[] source)
source - 需要进行反转操作的数组public static int[] insert(int[] array,
int index,
int insertNumber)
array - 数组index - 下标insertNumber - 插入数量public static int[] remove(int[] array,
int index)
array - 数组index - 下标public static int[] merge(int[] array1,
int[] array2)
array1 - 数组array2 - 数组public static int[] offsetArray(int[] array,
int offset)
array - 数组offset - 位数public int[] shuffle(int[] array)
array - 数组public static String print(int[] array)
array - 数组public static int searchOf(byte[] s,
byte value,
int index)
s - 原始对象value - 查找值index - 起始下标public static int indexOf(byte[] s,
byte value,
int index)
s - 原始对象value - 查找值index - 起始下标public static int indexOf(byte[] s,
byte[] value,
int index)
Copyright © 2017. All rights reserved.