java面向对象的有序数组和无序数组的比较

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);

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

原文地址:http://www.cnblogs.com/iwebkit/p/7498776.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值