数组
数组使应用最广泛的数据存储结构。它被植入到大部分编程语言中
用我的理解来说说数组吧。就行胡萝卜填坑一样,事先准备好坑,然后来一个填一个。拔掉随便一个胡萝卜,则要将拔掉的那个胡萝卜的后面的所有胡萝卜向前移动一位,将拔掉的胡萝卜的坑填住。
在给胡萝卜排序时,有相同的胡萝卜时的比较次数:
代码一波:
查找数组的一个数据项
package Array.Arrays;
public class LowArray {
private long[] a;
public LowArray(int size) {
a = new long[size];
}
public void setElem(int index, long value) {
a[index] = value;
}
public long getElem(int index) {
return a[index];
}
public static void main(String[] args) {
LowArray arr = new LowArray(100);
int nEilems = 0;
int j;
arr.setElem(0, 88);
arr.setElem(1, 77);
arr.setElem(2, 99);
arr.setElem(3, 44);
arr.setElem(4, 55);
arr.setElem(5, 22);
arr.setElem(6, 11);
arr.setElem(7, 00);
arr.setElem(8, 86);
arr.setElem(9, 33);
nEilems = 10;
for (j = 0; j < nEilems; j++) {
System.out.print(arr.getElem(j) + " ");
}
System.out.println("");
int searchKey = 26;
for (j = 0; j < nEilems; j++)
if (arr.getElem(j) == searchKey)
break;
if (j == nEilems)
System.out.println("Can't find " + searchKey);
else
System.out.println("Found " + searchKey);
for (j = 0; j < nEilems; j++)
if (arr.getElem(j) == 26)
break;
for (int k = j; k < nEilems; k++)
arr.setElem(k, arr.getElem(k + 1));
nEilems--;
for (j = 0; j < nEilems; j++) {
System.out.print(arr.getElem(j) + " ");
}
System.out.println("");
}
}
l l l l
V V
代码优化
package Array.Arrays;
public class HighArray {
//数组
private long a[];
//数组长度
private int nElems;
public HighArray(int max) {
a = new long[max];
nElems = 0;
}
//查找数据项
public boolean find(long searchKey) {
int j;
for (j = 0; j < nElems; j++)
if (a[j] == searchKey)
break;
if (j == nElems)
return false;
else
return true;
}
//添加一个数据
public void insert(long value) {
a[nElems] = value;
nElems++;
}
//删除一个数据
public boolean delete(long value) {
int j;
for (j = 0; j < nElems; j++) {
if (value == a[j])
break;
}
if (j == nElems)
return false;
else
for (int k = j; k < nElems; k++) {
a[k] = a[k + 1];
}
nElems--;
return true;
}
//展示数组
public void display() {
for (int j = 0; j < nElems; j++) {
System.out.print(a[j] + " ");
}
System.out.println("");
}
public static void main(String[] args) {
int maxSize = 100;
HighArray arr = new HighArray(maxSize);
arr = new HighArray(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
int searchKey = 35;
if (arr.find(searchKey)) {
System.out.println("Fount " + searchKey);
} else {
System.out.println("Can't Fount " + searchKey);
}
arr.delete(00);
arr.delete(55);
arr.display();
}
}
二分查找:
首先将数组变为有序数组,然后进行二分查找
package Array.Arrays;
public class OrderArray {
private long[] a;
private int nElems;
public OrderArray(int max) {
a = new long[max];
nElems = 0;
}
public int size() {
return nElems;
}
public int find(long searchKey) {
int lowerBound = 0;
int upperBound = nElems - 1;
int cirIn;
while (true) {
cirIn = (lowerBound + upperBound) / 2;
if (a[cirIn] == searchKey)
return cirIn;
else if (lowerBound > upperBound)
return nElems;
else {
if (a[cirIn] < searchKey)
lowerBound = cirIn + 1;
else
upperBound = cirIn - 1;
}
}
}
public void insert(long value) {
int j;
for (j = 0; j < nElems; j++)
if (a[j] > value)
break;
for (int k = nElems; k > j; k--)
a[k] = a[k - 1];
a[j] = value;
nElems++;
}
public boolean delete(long value) {
int j = find(value);
if (j == nElems)
return false;
else
for (int k = j; k < nElems; k++)
a[k] = a[k + 1];
nElems--;
return true;
}
public void display() {
for (int j = 0; j < nElems; j++)
System.out.print(a[j] + " ");
System.out.println("");
}
public static void main(String[] args) {
int maxSize = 100;
OrderArray arr = new OrderArray(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
int searKey = 55;
if (arr.find(searKey) != arr.size())
System.out.println("Fount " + searKey);
else
System.out.println("Can't find " + searKey);
arr.delete(00);
arr.display();
}
}
在插入时则就进行数组的有序化,然后进行二分查找
使用大O表示:
算法 | 大O表示的运行时间 |
线性查找 | O(N) |
二分查找 | O(logN) |
无序数组的插入 | O(1) |
有序数组的插入 | O(N) |
无序数组的删除 | O(N) |
有序数组的删除 | O(N) |
关于大O表示的解释: