【数据结构与算法01】数组

    数组是应用最广泛的数据存储结构。它被植入到大部分的编程语言中,由于数组十分易懂,所以在这里就不赘述,主要附上两端代码,一个是普通的数组,另一个是有序数组。有序数组是按关键字升序(或降序)排列的,这种排列使快速查找数据项成为可能,即可以使用二分查找。

    普通数组的java代码:

public class GeneralArray {
	private int[] a;
	private int size; //数组的大小
	private int nElem; //数组中有多少项
	public GeneralArray(int max) { //初始化数组
		this.a = new int[max];
		this.size = max;
		this.nElem = 0;
	}
	public boolean find(int searchNum) { //查找某个值	
		int j;
		for(j = 0; j < nElem; j++){
			if(a[j] == searchNum)
				break;
		}
		if(j == nElem)
			return false;
		else
			return true;
	}
	public boolean insert(int value) { //插入某个值
		if(nElem == size){
			System.out.println("数组已满!");
			return false;
		}
		a[nElem] = value;
		nElem++;		
		return true;
	}
	public boolean delete(int value) {//删除某个值
		int j;
		for(j = 0; j < nElem; j++) {
			if(a[j] == value) {
				break;
			}
		}
		if(j == nElem) 
			return false;
		if(nElem == size) {
			for(int k = j; k < nElem - 1; k++) {
				a[k] = a[k+1];
			}
		}
		else {
			for(int k = j; k < nElem; k++) {
				a[k] = a[k+1];
			}
		}
		nElem--;
		return true;
	}
	public void display() { //打印整个数组
		for(int i = 0; i < nElem; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.println("");
	}
}  

 

    有序数组的java代码:

public class OrderedArray {
	private long[] a;
	private int size; //数组的大小
	private int nElem; //数组中有多少项
	public OrderedArray(int max) { //初始化数组
		this.a = new long[max];
		this.size = max;
		this.nElem = 0;
	}
	public int size() { //返回数组实际有多少值
		return this.nElem;
	}
//--------------二分法查找某个值----------------//
	public int find(long searchNum) {
		int lower = 0;
		int upper = nElem - 1;
		int curr;
		while(true) {
			curr = (lower + upper) / 2;
			if(a[curr] == searchNum)
				return curr;
			else if(lower > upper) 
				return -1;
			else {
				if(a[curr] < searchNum) 
					lower = curr + 1;
				else
					upper = curr - 1;
			}
			
		}
	}
	public boolean insert(long value) { //插入某个值
		if(nElem == size){
			System.out.println("数组已满!");
			return false;
		}
		int j;
		for(j = 0; j < nElem; j++){
			if(a[j] > value)
				break;
		}
		
		for(int k = nElem; k > j; k--) {
			a[k] = a[k-1];
		}
		a[j] = value;
		nElem++;		
		return true;
	}
	public boolean delete(long value) { //删除某个值
		int j = find(value);
		if(j  == -1){
			System.out.println("没有该元素!");
			return false;
		}
		if(nElem == size) {
			for(int k = j; k < nElem - 1; k++) {
				a[k] = a[k+1];
			}		
			a[nElem-1] = 0;
		}
		else {
			for(int k = j; k < nElem; k++) {
				a[k] = a[k+1];
			}
		}
		nElem--;
		return true;
	}
	public void display() { //打印整个数组
		for(int i = 0; i < nElem; i++) {
			System.out.print(a[i] + " ");
		}
		System.out.println("");
	}
}

    对于数组这种数据结构,线性查找的话,时间复杂度为O(N),二分查找的话时间为O(longN),无序数组插入的时间复杂度为O(1),有序数组插入的时间复杂度为O(N),删除操作的时间复杂度均为O(N)。

    数组比较简单,就讨论这么多,如有错误,欢迎留言指正~

    欢迎大家关注我的公众号:“武哥聊编程”,一个有温度的公众号~

    关注回复:资源,可以领取海量优质视频资料
    程序员私房菜

_____________________________________________________________________________________________________________________________________________________

-----乐于分享,共同进步!

-----更多文章请看:http://blog.csdn.net/eson_15

  • 16
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值