Java数据结构和算法总结-数组、二分查找

时间:2022-04-19 16:46:11

  前言:在平时开发中数组几乎是最基本也是最常用的数据类型,相比链表、二叉树等又简单很多,所以在学习数据和算法时用数组来作为一个起点再合适不过了。本篇博文的所有代码已上传 github ,对应工程的 array 模块,下载地址:https://github.com/lgliuwei/DataStructureStudy,项目工程为 IntelliJ IDEA 环境,童鞋不妨下载下来,参照着代码看博文岂不是效果更好~

  首先介绍一下工程的目录结构和作用,本工程的各个模块之间以 Module 形式划分,每个 Module 大致对应一个专题(例如:链表对应工程中的 link),其中 libuitils 作为工具模块为其他的模块提供基础的工具类,例如输出语句的打印、字符串处理等等。

  Java数据结构和算法总结-数组、二分查找

一、数组

  俗话说磨刀不误砍柴工,为了后续的方便,先做一些准备工作,首先创建一个抽象类 BaseArray,包含的几个关键方法如下:

  ·initArrayByRandom(int size) :使用随机数生成一个数组。

  ·initArrayByRandomNoRepeat(int size):不重复的随机数生成一个数组。

  ·swap(int aIndex, int bIndex):交换数组中两个下标的值。

  详细代码如下:    

 /**
* 数组基类
* Created by liuwei on 17/7/21.
*/
public abstract class BaseArray {
protected int[] mArray;
protected int mSize;
protected int mMaxSize;
public BaseArray(int maxSize){
mMaxSize = maxSize;
mArray = new int[mMaxSize];
mSize = 0;
}
public abstract int insert(int e) throws ArrayIndexOutOfBoundsException;
public abstract int delete(int e);
/**
* 随机数创建数组
* @param size
* @return
*/
public void initArrayByRandom(int size) throws ArrayIndexOutOfBoundsException {
if (size > mMaxSize) {
throw new ArrayIndexOutOfBoundsException("size不能大于数组的maxSize");
} else {
mSize = size;
for (int i = 0; i < size; i++) {
mArray[i] = getRandomInt(size);
}
}
}
/**
* 随机数创建数组(无重复)
* @param size
* @return
*/
public void initArrayByRandomNoRepeat(int size) throws ArrayIndexOutOfBoundsException {
if (size > mMaxSize) {
throw new ArrayIndexOutOfBoundsException("size不能大于数组的maxSize");
} else {
mSize = size;
int n = 0;
boolean noRepeat;
while (n < mSize) {
noRepeat = true;
int temp = getRandomInt(mSize * 10);
for (int i = 0; i < n; i++) {
if (temp == mArray[i]) {
noRepeat = false;
break;
}
}
if (noRepeat) {
mArray[n] = temp;
n++;
}
} }
}
public void initArray(int[] array) {
mSize = array.length;
for (int i = 0; i < mSize; i++) {
mArray[i] = array[i];
}
}
public int size(){
return mSize;
}
/**
* 获取一个随机整数
* @return
*/
public int getRandomInt(int bounder){
return new Random().nextInt(bounder);
}
public void display(){
for (int i = 0; i < mSize; i++) {
print(mArray[i] + ", ");
}
println("");
}
protected void swap(int aIndex, int bIndex) {
int temp = mArray[aIndex];
mArray[aIndex] = mArray[bIndex];
mArray[bIndex] = temp;
}
protected void print(Object o){
Logger.print(o);
}
protected void println(Object o){
Logger.println(o);
}

  看到这个类比较长也不要害怕,它里面只是包含一些工具性质的方法,目的是为我们提供方便,使我们在后续的二分查找和排序中可以更加专注于算法之中。

  接着通过继承 BaseArray 创建一个有序数组类 OrderedArray ,普通的插入对于数组来说再简单不过了,直接往对应的下标中赋值即可,就不多说了,这里为创建的实体数组添加一个有序插入(正序)的方法,初步想了一下有序插入大致需要三步:

  1、从数组的0下标开始往后找,直到发现大于带插入的值时停下,记录下标。

  2、从数组的最后一个下标开始依次后移一位,直到第一步中记录的下标。

  3、将带插入的值赋给第一步中纪律的下标。

  Java数据结构和算法总结-数组、二分查找

  详细代码如下:

     /**
* 有序插入
*/
@Override
public int insert(int e) throws ArrayIndexOutOfBoundsException {
if (mSize == mMaxSize) {
throw new ArrayIndexOutOfBoundsException("数组已经满了");
}
int i;
for (i = 0; i < mSize; i++) {
if (e < mArray[i]) break;
}
for (int j = mSize; j > i; j--) {
mArray[j] = mArray[j-1];
}
mArray[i] = e;
mSize++;
return i;
}

二、线性查找

  如果我们想从一个有序数组中查找一个元素有两种方法,线性查找和二分查找,线性查找就是最最常规的方法,从数组的0下标开始依次往后查找,直到找到要查找的元素,则查找成功,如果在数组中不存在带查找的元素,因为是有序数组,我们只需找到比待查元素大时即可退出。

  线性查找详细代码如下:

     /**
* 线性查找
* @param e
* @return
*/
public int findByLiner(int e) {
for(int i = 0; i < mSize; i++) {
if (e == mArray[i]) {
return i;
} else if (mSize > (i + 1) &&e > mArray[i] && e < mArray[i + 1]) {
return -1;
}
}
return -1;
}

  线性查找比较简单,这里不过过多分析,很容易我们就能看出来线性查找的平均比较次数是数组元素个数的一半,所花费的时间与元素个数(假设是N)的一半成正比,在算法中描述时间复杂度是我们通常忽略常数,习惯性用大O表示法,所以线性查找的时间复杂度表示为:O(N)。

三、二分查找

  二分查找类似于我们朋友聚会喝酒时玩的猜字游戏,游戏中,通常会给出一个范围例如0-100,然后由一方从中默默挑出一个字让你来猜,你猜的时候他会告诉你是否猜中,或者比他挑的字大或小。为了尽快的猜中,我们会选择首先从中间开始猜,根据对方的提示我们来选择偏大的一半还是偏小的一半然后再从新范围的一半开始猜,这样很快就能猜中答案。

  具体的算法思路如下(假设数组下标范围是0-N):

  1、首先定义两个下标边界变量lowBounder=0,highBounder=N-1

  2、让当前下标为lowBounder和highBounder的中间与待查找的元素比较:

    ·如果相等,则查找成功。

    ·如果小于待查找元素,则将lowBounder赋值为当前下标+1。

    ·如果大于带查找元素,则将hightBounder赋值为当前下标-1。

    ·如果此过程发现lowBounder大于highBounder,则表示未找到。

  3、循环执行第二步。

  详细代码如下:

     /**
* 二分查找
* @param e
* @return
*/
public int findByHalf(int e) {
int lowIndex = 0;
int highIndex = mSize - 1;
int currentIndex;
while(true){
currentIndex = (lowIndex + highIndex) / 2;
if (e == mArray[currentIndex]) {
return currentIndex;
} else if (lowIndex >= highIndex) {
return -1;
} else if (e > mArray[currentIndex]) {
lowIndex = currentIndex + 1;
} else {
highIndex = currentIndex - 1;
}
}
}

  单从思路我们就可以分析出二分查找的平均查找时间要比线性查找快的多。

  它的时间复杂度为:O(logN)。