package aa;
class Array{
//定义一个有序数组
private long[] a;
//定义数组长度
private int nElems;
//构造函数初始化
public Array(int max){
a = new long[max];
nElems = 0;
}
//size函数
public int size(){
return nElems;
}
//定义添加函数
public void insert(long value){
//将value赋值给数组成员
a[nElems] = value;
//然后将数组长度加一
nElems ++;
long temp;
//用冒泡法排序
for(int i = 0; i < nElems - 1; i ++)
{
for(int j = 0; j < nElems - 1 - i; j++)
{
if(a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
//定义查找方法
public int find(long searchKey){
//因为是有序数组,我们可以用二分法来查找时间为 O(logN),如果线性查找则为O(N) //下限
int lowerBound = 0;
//上限
int upperBound = nElems -1;
//中间值
int curIndex; while(true)
{
curIndex = (lowerBound + upperBound) / 2;
if(a[curIndex] == searchKey)
{
return curIndex;
}
else if(lowerBound > upperBound)
{
return nElems;
}
else
{
if(a[curIndex] > searchKey)
{
upperBound = curIndex -1;
}
else
{
lowerBound = curIndex + 1;
} }
} }
//定义删除方法
public boolean delete(long value){
int index = find(value);
if(index == size())
{
return false;
}
else
{
for(int i = index; i < size(); i++)
{
a[i] = a[i + 1];
}
nElems --;
return false;
}
}
//定义显示方法
public void display(){
for(int j = 0; j < nElems; j++)
{
System.out.println(a[j] + " ");
}
System.out.println("");
} }
public class Arr {
public static void main(String[] args)
{
int maxSize = 100;
Array arr = new Array(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(22);
arr.insert(66);
arr.insert(33);
arr.display();
int searchKey = 54;
if(arr.find(searchKey) != arr.size())
{
System.out.println("found" + searchKey);
}
else
{
System.out.println("cant find" + searchKey);
} arr.delete(22);
arr.delete(55);
arr.delete(99); arr.display();
}
}
package aa;
//定义一个无序数组
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.println(a[j] + "");
}
System.out.println("");
}
}
public class highArrayApp {
public static void main(String[] args)
{
int maxSize = 100;
HighArray 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("Found" + searchKey);
}
else
{
System.out.println("cant find" + searchKey); }
arr.delete(00);
arr.delete(55);
arr.delete(99); arr.display();
}
}

大O表示法

  O(1):优秀。例如无须数组插入。

  O(logN):良好。例如有序的二分查找。

  O(N):及格。例如无序数组的删除,有序数组的删除和插入,线性查找。

  O(N2):不及格。例如冒泡排序。

总结有序数组和无序数组

  有序数组:插入+ 查找 +删除 = O(N) +O(logN)+O(N);

  无序数组:插入 + 查找 + 删除 = O(1) + O(N) + O(N);

  所以在数据偏向查找操作的时候用有序数组快一些,在数据偏向插入的时候,无序数组好一些。删除操作效率一样。

最新文章

  1. iOS - NSMutableAttributedString富文本的实现
  2. 奇怪的bug(ant-design)
  3. Mongodb的安装
  4. 有return的情况下try catch finally的执行顺序(转)
  5. jenkins---配置邮件
  6. postgres中几个复杂的sql语句
  7. eclipse配置文件导出问题
  8. stdout( 标准输出流)和 stderr( 标准输入流) 重定向问题
  9. Yii源码阅读笔记(二十七)
  10. 10.11 pod 安装
  11. Python中的join()函数split()函数
  12. Implement Trie (Prefix Tree) 解答
  13. 用KnockoutJS实现ToDoMVC代码分析
  14. HttpServletRequest.getServletContext()一直提示找不到,而引出的问题
  15. Spring3.2AOP实现需要添加的三个包
  16. MySql foreach属性
  17. Vue重修02
  18. JDBC中execute、executeQuery和executeUpdate的区别
  19. idea not found for the web module
  20. fiddler 实现代理的操作

热门文章

  1. Model验证简单易懂
  2. #leetcode刷题之路21-合并两个有序链表
  3. mininet+floodlight使用(一)
  4. html中如何移除video下载按钮
  5. 大专生自学html5到找到工作的心得
  6. 2.6 USB摄像头驱动之USB描述符
  7. 物联网通信 - RESTDemo示例程序(C#版本)
  8. U盘直接读写(今天用到了)
  9. WPF几个基础概念的浅显理解
  10. PHP学习笔记之析构函数以及static,self,parent关键字