题记:
数组是应用最广泛的数据存储结构。它被植入到大部分的编程语言中,由于数组十分易懂,所以在这里就不赘述,主要附上两端代码,一个是普通的数组,另一个是有序数组。有序数组是按关键字升序(或降序)排列的,这种排列使快速查找数据项成为可能,即可以使用二分查找。
普通数组
/**
* 初始化-普通数组
* @author fancy
* @date 2018-12-04 14:29
*/
public class GeneralArray {
// 数组
private int[] array;
// 初始数组大小
private int size;
// 集合大小
private int nElem;
public GeneralArray (int size) {
this.array = new int[size];
this.size = size;
this.nElem = 0;
}
/**
* @author fancy
* 新增数据
* @param value
* @throws Exception
*/
public void insert (int value) throws Exception {
if (nElem == size) {
throw new Exception("数组已满!");
}
array[nElem] = value;
nElem ++;
}
/**
* 删除所有的
* @param value
*/
public void deleteAllByValue (int value) {
//获取当前value在数组中的第一个下标
int index;
for (index = 0; index < nElem; index ++){
if ( array[index] == value) {
deleteByIndex(index);
}
}
}
/**
* 根据值删除第一个下标的数据
* @param value
*/
public void deleteFristByValue (int value) {
//获取当前value在数组中的第一个下标
int index;
for (index = 0; index < nElem; index ++){
if ( array[index] == value) {
break;
}
}
deleteByIndex(index);
}
/**
* 根据下标删除
* @param index
*/
public void deleteByIndex (int index) {
if (index != nElem) {
//将下标后面的数据覆盖掉下标前面的数据
for (;index < nElem; index ++) {
array[index] = array [index + 1];
}
} else {
array[index] = 0;
}
// 当前的长度减去一
nElem --;
}
public int findByIndex (int index) throws ArrayIndexOutOfBoundsException {
if (index < 0 && index > nElem) {
throw new ArrayIndexOutOfBoundsException("数组下标越界!");
}
return array[index];
}
public int[] getArray () {
return array;
}
}
排序数组
/**
* 有序数组
* @author fancy
* @date 2018-12-04 15:45
*/
public class OrderedArray {
private int[] array;
private int size; //数组的大小
private int nElem; //数组中有多少项
public OrderedArray(int max) { //初始化数组
this.array = new int[max];
this.size = max;
this.nElem = 0;
}
public int size() { //返回数组实际有多少值
return this.nElem;
}
public int[] getArray() {
return this.array;
}
/**
* 二分法
* @param value
* @return
*/
public int findIndexByValue(int value) {
int lowerIndex = 0;
int upperIndex = nElem;
int currIndex;
while (true){
currIndex = (lowerIndex + upperIndex) / 2 ;
if (array[currIndex] == value){
break;
} else if (lowerIndex > upperIndex) {
currIndex = -1;
break;
} else {
if (array[currIndex] > value) {
upperIndex = currIndex;
} else {
lowerIndex = currIndex;
}
}
}
return currIndex;
}
/**
* 优化 二分法
* @param value
* @return
*/
public int optimizationFindIndexByValue(int value) {
int lowerIndex = 0;
int upperIndex = nElem;
int currIndex;
while (true){
currIndex = (lowerIndex + upperIndex) / 2 ;
if (array[currIndex] == value){
break;
} else if (lowerIndex > upperIndex) {
currIndex = -1;
break;
} else {
if (array[currIndex] > value) {
upperIndex = currIndex - 1;
} else {
lowerIndex = currIndex + 1;
}
}
}
return currIndex;
}
public static int binSearch(int srcArray[], int start, int end, int key) {
int mid = (end - start) / 2 + start;
if (srcArray[mid] == key) {
return mid;
}
if (start >= end) {
return -1;
} else if (key > srcArray[mid]) {
return binSearch(srcArray, mid + 1, end, key);
} else if (key < srcArray[mid]) {
return binSearch(srcArray, start, mid - 1, key);
}
return -1;
}
/**
* 排序插入
* @param value
* @return
*/
public void insert(int value) throws Exception {
if (nElem == size) {
throw new Exception("数组已满");
}
if (nElem == 0) {
array[0] = value;
nElem ++;
} else {
// 排序插入数据
int index;
for (index = 0; index < nElem ; index++) {
if (array[index] > value) {
break;
}
}
nElem ++;
int temp = nElem;
for (;index < temp ; temp --) {
array[temp] = array[temp - 1];
}
}
}
}
对于数组这种数据结构,线性查找的话,时间复杂度为O(N),二分查找的话时间复杂度为O(log N),无序数组插入的时间复杂度为O(1),有序数组插入的时间复杂度为O(N),删除操作的时间复杂度均为O(N)。