第四知识单元

工具类与算法

Java程序设计 第8讲,主讲人:李欣

Created: 2024-09-04 Wed 20:52

0.1. 互动课堂

Click to host the seminar.

0.2. 签到

https://dash.memopixel.com/tool/attendance/

0.3. 本次课的目标

  • 数据结构类
    • 学会使用Java语言构建数据结构类;
    • 了解 链表队列二叉树 等数据结构的实现。
  • 查找与排序
    • 了解 多种 排序 算法的特点;
    • 掌握 系统类实现的 排序 算法与使用;
    • 了解 多种 查找 算法的特点;
    • 掌握 系统类实现的 查找 算法与使用。
  • 集合与范型
    • 了解 集合 的基本概念与体系结构;
    • 理解 List 集合、 Map 集合的使用;
    • 了解 Java 范型 的基本概念和应用。

1. 递归

递归 是常用的编程技术,其基本思想就是 自己调用自己 , 一个使用递归技术的方法即是 直接间接 调用 自身 的方法。

1.1. Fibonacci numbers

https://en.wikipedia.org/wiki/Fibonacci_number

fibonacci-squares.png

Figure 1: A tiling with squares whose side lengths are successive Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13 and 21.

1.1.1. Definition

The Fibonacci numbers may be defined by the recurrence relation.

\[F_{0}=0,\quad F_{1}=1\] and \[F_{n}=F_{n-1}+F_{n-2}\] for \(n > 1\).

Under some older definitions, the value \(F_{0}=0\) is omitted, so that the sequence starts with \(F_{1}=F_{2}=1\) and the recurrence \(F_{n}=F_{n-1}+F_{n-2}\) is valid for \(n > 2\).

–Wikipedia

The first 20 Fibonacci numbers \(F_n\) are:

\(F_0\) \(F_1\) \(F_2\) \(F_3\) \(F_4\) \(F_5\) \(F_6\) \(F_7\) \(F_8\) \(F_9\)
0 1 1 2 3 5 8 13 21 34
\(F_{10}\) \(F_{11}\) \(F_{12}\) \(F_{13}\) \(F_{14}\) \(F_{15}\) \(F_{16}\) \(F_{17}\) \(F_{18}\) \(F_{19}\)
55 89 144 233 377 610 987 1597 2584 4181

1.1.2. Algorithm

class Fibonacci {
    public static void main(String[] args) {
    }
}
class Fibonacci {
    static long f(int n) {
    }
    public static void main(String[] args) {
    }
}
class Fibonacci {
    static long f(int n) {
        if( n == 0 || n == 1 ) {
            return n;
        }
    }
    public static void main(String[] args) {
    }
}
class Fibonacci {
    static long f(int n) {
        if( n == 0 || n == 1 ) {
            return n;
        } else {
            return f(n - 1) + f(n - 2);
        }
    }
    public static void main(String[] args) {
    }
}
class Fibonacci {
    static long f(int n) {
        if( n == 0 || n == 1 ) {
            return n;
        } else {
            return f(n - 1) + f(n - 2);
        }
    }
    public static void main(String[] args) {
        for(int n = 0; n < 20; n++) {
            System.out.print("n: " + n + ", Fn: " + f(n) + ";  ");
        }
    }
}
class Fibonacci {
    static long f(int n) {
        if( n == 0 || n == 1 ) {
            return n;
        } else {
            return f(n - 1) + f(n - 2);
        }
    }
    public static void main(String[] args) {
        for(int n = 0; n < 20; n++) {
            System.out.print("n: " + n + ", Fn: " + f(n) + ";  ");
            if(n % 5 == 4) {
                System.out.println();
            }
        }
    }
}
class Fibonacci {
    static long f(int n) {
        if( n == 0 || n == 1 ) {
            return n;
        } else {
            return f(n - 1) + f(n - 2);
        }
    }
    public static void main(String[] args) {
        for(int n = 0; n < 20; n++) {
            System.out.print("n: " + n + ", Fn: " + f(n) + ";  ");
            if(n % 5 == 4) {
                System.out.println();
            }
        }
    }
}
n: 0, Fn: 0;  n: 1, Fn: 1;  n: 2, Fn: 1;  n: 3, Fn: 2;  n: 4, Fn: 3;  
n: 5, Fn: 5;  n: 6, Fn: 8;  n: 7, Fn: 13;  n: 8, Fn: 21;  n: 9, Fn: 34;  
n: 10, Fn: 55;  n: 11, Fn: 89;  n: 12, Fn: 144;  n: 13, Fn: 233;  n: 14, Fn: 377;  
n: 15, Fn: 610;  n: 16, Fn: 987;  n: 17, Fn: 1597;  n: 18, Fn: 2584;  n: 19, Fn: 4181;  

2. 排序

random-org.png

Figure 2: RANDOM.ORG

random-numbers.png

Figure 3: Random numbers

2.1. 冒泡排序

bubble-sort.gif

Figure 4: 冒泡排序示意图

int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];
int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}
int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}

for(int i = 0; i < array.length - 1; i++) {
    for(int j = 0; j < array.length - i - 1; j++) {
    }
    for(int j = 0; j < array.length; j++) {
    }
}
int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}

for(int i = 0; i < array.length - 1; i++) {
    for(int j = 0; j < array.length - i - 1; j++) {
        if(array[j] > array[j + 1]) {
            int temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
            exchangeCount[i]++;
        }
    }
    for(int j = 0; j < array.length; j++) {
        sortArray[i + 1][j] = array[j];
    }
}
int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}

for(int i = 0; i < array.length - 1; i++) {
    for(int j = 0; j < array.length - i - 1; j++) {
        if(array[j] > array[j + 1]) {
            int temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
            exchangeCount[i]++;
        }
    }
    for(int j = 0; j < array.length; j++) {
        sortArray[i + 1][j] = array[j];
    }
    if(exchangeCount[i] == 0) break;
}

for(int i = 0; i < sortArray.length; i++) {
    System.out.print("[" + i + "] ");
    for(int j = 0; j < sortArray[i].length; j++) {
        System.out.print(sortArray[i][j] + " ");
    }
    if(i != 0) {
        System.out.println(" -> " + exchangeCount[i - 1]);
    } else {
        System.out.println();
    }
}
int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}

for(int i = 0; i < array.length - 1; i++) {
    for(int j = 0; j < array.length - i - 1; j++) {
        if(array[j] > array[j + 1]) {
            int temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
            exchangeCount[i]++;
        }
    }
    for(int j = 0; j < array.length; j++) {
        sortArray[i + 1][j] = array[j];
    }
    if(exchangeCount[i] == 0) break;
}

for(int i = 0; i < sortArray.length; i++) {
    System.out.print("[" + i + "] ");
    for(int j = 0; j < sortArray[i].length; j++) {
        System.out.print(sortArray[i][j] + " ");
    }
    if(i != 0) {
        System.out.println(" -> " + exchangeCount[i - 1]);
    } else {
        System.out.println();
    }
}
[0] 27 8 59 44 28 88 46 1 89 44 
[1] 8 27 44 28 59 46 1 88 44 89  -> 6
[2] 8 27 28 44 46 1 59 44 88 89  -> 4
[3] 8 27 28 44 1 46 44 59 88 89  -> 2
[4] 8 27 28 1 44 44 46 59 88 89  -> 2
[5] 8 27 1 28 44 44 46 59 88 89  -> 1
[6] 8 1 27 28 44 44 46 59 88 89  -> 1
[7] 1 8 27 28 44 44 46 59 88 89  -> 1
[8] 1 8 27 28 44 44 46 59 88 89  -> 0
[9] 0 0 0 0 0 0 0 0 0 0  -> 0
// int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[] array = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}

for(int i = 0; i < array.length - 1; i++) {
    for(int j = 0; j < array.length - i - 1; j++) {
        if(array[j] > array[j + 1]) {
            int temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
            exchangeCount[i]++;
        }
    }
    for(int j = 0; j < array.length; j++) {
        sortArray[i + 1][j] = array[j];
    }
    if(exchangeCount[i] == 0) break;
}

for(int i = 0; i < sortArray.length; i++) {
    System.out.print("[" + i + "] ");
    for(int j = 0; j < sortArray[i].length; j++) {
        System.out.print(sortArray[i][j] + " ");
    }
    if(i != 0) {
        System.out.println(" -> " + exchangeCount[i - 1]);
    } else {
        System.out.println();
    }
}
[0] 10 9 8 7 6 5 4 3 2 1 
[1] 9 8 7 6 5 4 3 2 1 10  -> 9
[2] 8 7 6 5 4 3 2 1 9 10  -> 8
[3] 7 6 5 4 3 2 1 8 9 10  -> 7
[4] 6 5 4 3 2 1 7 8 9 10  -> 6
[5] 5 4 3 2 1 6 7 8 9 10  -> 5
[6] 4 3 2 1 5 6 7 8 9 10  -> 4
[7] 3 2 1 4 5 6 7 8 9 10  -> 3
[8] 2 1 3 4 5 6 7 8 9 10  -> 2
[9] 1 2 3 4 5 6 7 8 9 10  -> 1
// int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}

for(int i = 0; i < array.length - 1; i++) {
    for(int j = 0; j < array.length - i - 1; j++) {
        if(array[j] > array[j + 1]) {
            int temp = array[j];
            array[j] = array[j + 1];
            array[j + 1] = temp;
            exchangeCount[i]++;
        }
    }
    for(int j = 0; j < array.length; j++) {
        sortArray[i + 1][j] = array[j];
    }
    if(exchangeCount[i] == 0) break;
}

for(int i = 0; i < sortArray.length; i++) {
    System.out.print("[" + i + "] ");
    for(int j = 0; j < sortArray[i].length; j++) {
        System.out.print(sortArray[i][j] + " ");
    }
    if(i != 0) {
        System.out.println(" -> " + exchangeCount[i - 1]);
    } else {
        System.out.println();
    }
}
[0] 1 2 3 4 5 6 7 8 9 10 
[1] 1 2 3 4 5 6 7 8 9 10  -> 0
[2] 0 0 0 0 0 0 0 0 0 0  -> 0
[3] 0 0 0 0 0 0 0 0 0 0  -> 0
[4] 0 0 0 0 0 0 0 0 0 0  -> 0
[5] 0 0 0 0 0 0 0 0 0 0  -> 0
[6] 0 0 0 0 0 0 0 0 0 0  -> 0
[7] 0 0 0 0 0 0 0 0 0 0  -> 0
[8] 0 0 0 0 0 0 0 0 0 0  -> 0
[9] 0 0 0 0 0 0 0 0 0 0  -> 0

2.2. 选择排序

selection-sort.gif

Figure 5: 选择排序示意图

int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}

for(int i = 0; i < array.length - 1; i++) {
    int minIndex = i;
    int minNum = array[i];
    for(int j = i + 1; j < array.length; j++) {
        if(array[j] < minNum) {
            minNum = array[j];
            minIndex = j;
            exchangeCount[i]++;
        }
    }
    array[minIndex] = array[i];
    array[i] = minNum;
    for(int j = 0; j < array.length; j++) {
        sortArray[i + 1][j] = array[j];
    }
}
for(int i = 0; i < sortArray.length; i++) {
    System.out.print("[" + i + "] ");
    for(int j = 0; j < sortArray[i].length; j++) {
        System.out.print(sortArray[i][j] + " ");
    }
    if(i != 0) {
        System.out.println(" -> " + exchangeCount[i - 1]);
    } else {
        System.out.println();
    }
}
[0] 27 8 59 44 28 88 46 1 89 44 
[1] 1 8 59 44 28 88 46 27 89 44  -> 2
[2] 1 8 59 44 28 88 46 27 89 44  -> 0
[3] 1 8 27 44 28 88 46 59 89 44  -> 3
[4] 1 8 27 28 44 88 46 59 89 44  -> 1
[5] 1 8 27 28 44 88 46 59 89 44  -> 0
[6] 1 8 27 28 44 44 46 59 89 88  -> 2
[7] 1 8 27 28 44 44 46 59 89 88  -> 0
[8] 1 8 27 28 44 44 46 59 89 88  -> 0
[9] 1 8 27 28 44 44 46 59 88 89  -> 1
// int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[] array = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}

for(int i = 0; i < array.length - 1; i++) {
    int minIndex = i;
    int minNum = array[i];
    for(int j = i + 1; j < array.length; j++) {
        if(array[j] < minNum) {
            minNum = array[j];
            minIndex = j;
            exchangeCount[i]++;
        }
    }
    array[minIndex] = array[i];
    array[i] = minNum;
    for(int j = 0; j < array.length; j++) {
        sortArray[i + 1][j] = array[j];
    }
}
for(int i = 0; i < sortArray.length; i++) {
    System.out.print("[" + i + "] ");
    for(int j = 0; j < sortArray[i].length; j++) {
        System.out.print(sortArray[i][j] + " ");
    }
    if(i != 0) {
        System.out.println(" -> " + exchangeCount[i - 1]);
    } else {
        System.out.println();
    }
}
[0] 10 9 8 7 6 5 4 3 2 1 
[1] 1 9 8 7 6 5 4 3 2 10  -> 9
[2] 1 2 8 7 6 5 4 3 9 10  -> 7
[3] 1 2 3 7 6 5 4 8 9 10  -> 5
[4] 1 2 3 4 6 5 7 8 9 10  -> 3
[5] 1 2 3 4 5 6 7 8 9 10  -> 1
[6] 1 2 3 4 5 6 7 8 9 10  -> 0
[7] 1 2 3 4 5 6 7 8 9 10  -> 0
[8] 1 2 3 4 5 6 7 8 9 10  -> 0
[9] 1 2 3 4 5 6 7 8 9 10  -> 0
// int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}

for(int i = 0; i < array.length - 1; i++) {
    int minIndex = i;
    int minNum = array[i];
    for(int j = i + 1; j < array.length; j++) {
        if(array[j] < minNum) {
            minNum = array[j];
            minIndex = j;
            exchangeCount[i]++;
        }
    }
    array[minIndex] = array[i];
    array[i] = minNum;
    for(int j = 0; j < array.length; j++) {
        sortArray[i + 1][j] = array[j];
    }
}
for(int i = 0; i < sortArray.length; i++) {
    System.out.print("[" + i + "] ");
    for(int j = 0; j < sortArray[i].length; j++) {
        System.out.print(sortArray[i][j] + " ");
    }
    if(i != 0) {
        System.out.println(" -> " + exchangeCount[i - 1]);
    } else {
        System.out.println();
    }
}
[0] 1 2 3 4 5 6 7 8 9 10 
[1] 1 2 3 4 5 6 7 8 9 10  -> 0
[2] 1 2 3 4 5 6 7 8 9 10  -> 0
[3] 1 2 3 4 5 6 7 8 9 10  -> 0
[4] 1 2 3 4 5 6 7 8 9 10  -> 0
[5] 1 2 3 4 5 6 7 8 9 10  -> 0
[6] 1 2 3 4 5 6 7 8 9 10  -> 0
[7] 1 2 3 4 5 6 7 8 9 10  -> 0
[8] 1 2 3 4 5 6 7 8 9 10  -> 0
[9] 1 2 3 4 5 6 7 8 9 10  -> 0

2.3. 插入排序

insertion-sort.gif

Figure 6: 插入排序示意图

int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}

for(int i = 1; i < array.length; i++) {
    int current = array[i];
    int j;
    for(j = i - 1; j >= 0; j--) {
        if(array[j] > current) {
            array[j + 1] = array[j];
            exchangeCount[i - 1]++;
        } else {
            break;
        }
    }
    array[j + 1] = current;
    for(j = 0; j < array.length; j++) {
        sortArray[i][j] = array[j];
    }
}
for(int i = 0; i < sortArray.length; i++) {
    System.out.print("[" + i + "] ");
    for(int j = 0; j < sortArray[i].length; j++) {
        System.out.print(sortArray[i][j] + " ");
    }
    if(i != 0) {
        System.out.println(" -> " + exchangeCount[i - 1]);
    } else {
        System.out.println();
    }
}
[0] 27 8 59 44 28 88 46 1 89 44 
[1] 8 27 59 44 28 88 46 1 89 44  -> 1
[2] 8 27 59 44 28 88 46 1 89 44  -> 0
[3] 8 27 44 59 28 88 46 1 89 44  -> 1
[4] 8 27 28 44 59 88 46 1 89 44  -> 2
[5] 8 27 28 44 59 88 46 1 89 44  -> 0
[6] 8 27 28 44 46 59 88 1 89 44  -> 2
[7] 1 8 27 28 44 46 59 88 89 44  -> 7
[8] 1 8 27 28 44 46 59 88 89 44  -> 0
[9] 1 8 27 28 44 44 46 59 88 89  -> 4
// int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[] array = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}

for(int i = 1; i < array.length; i++) {
    int current = array[i];
    int j;
    for(j = i - 1; j >= 0; j--) {
        if(array[j] > current) {
            array[j + 1] = array[j];
            exchangeCount[i - 1]++;
        } else {
            break;
        }
    }
    array[j + 1] = current;
    for(j = 0; j < array.length; j++) {
        sortArray[i][j] = array[j];
    }
}
for(int i = 0; i < sortArray.length; i++) {
    System.out.print("[" + i + "] ");
    for(int j = 0; j < sortArray[i].length; j++) {
        System.out.print(sortArray[i][j] + " ");
    }
    if(i != 0) {
        System.out.println(" -> " + exchangeCount[i - 1]);
    } else {
        System.out.println();
    }
}
[0] 10 9 8 7 6 5 4 3 2 1 
[1] 9 10 8 7 6 5 4 3 2 1  -> 1
[2] 8 9 10 7 6 5 4 3 2 1  -> 2
[3] 7 8 9 10 6 5 4 3 2 1  -> 3
[4] 6 7 8 9 10 5 4 3 2 1  -> 4
[5] 5 6 7 8 9 10 4 3 2 1  -> 5
[6] 4 5 6 7 8 9 10 3 2 1  -> 6
[7] 3 4 5 6 7 8 9 10 2 1  -> 7
[8] 2 3 4 5 6 7 8 9 10 1  -> 8
[9] 1 2 3 4 5 6 7 8 9 10  -> 9
// int[] array = { 27, 8, 59, 44, 28, 88, 46, 1, 89, 44 };
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int[][] sortArray = new int[array.length][array.length];
int[] exchangeCount = new int[array.length];

for(int i = 0; i < array.length; i++) {
    sortArray[0][i] = array[i];
}

for(int i = 1; i < array.length; i++) {
    int current = array[i];
    int j;
    for(j = i - 1; j >= 0; j--) {
        if(array[j] > current) {
            array[j + 1] = array[j];
            exchangeCount[i - 1]++;
        } else {
            break;
        }
    }
    array[j + 1] = current;
    for(j = 0; j < array.length; j++) {
        sortArray[i][j] = array[j];
    }
}
for(int i = 0; i < sortArray.length; i++) {
    System.out.print("[" + i + "] ");
    for(int j = 0; j < sortArray[i].length; j++) {
        System.out.print(sortArray[i][j] + " ");
    }
    if(i != 0) {
        System.out.println(" -> " + exchangeCount[i - 1]);
    } else {
        System.out.println();
    }
}
[0] 1 2 3 4 5 6 7 8 9 10 
[1] 1 2 3 4 5 6 7 8 9 10  -> 0
[2] 1 2 3 4 5 6 7 8 9 10  -> 0
[3] 1 2 3 4 5 6 7 8 9 10  -> 0
[4] 1 2 3 4 5 6 7 8 9 10  -> 0
[5] 1 2 3 4 5 6 7 8 9 10  -> 0
[6] 1 2 3 4 5 6 7 8 9 10  -> 0
[7] 1 2 3 4 5 6 7 8 9 10  -> 0
[8] 1 2 3 4 5 6 7 8 9 10  -> 0
[9] 1 2 3 4 5 6 7 8 9 10  -> 0

3. 查找

3.1. 顺序查找

顺序查找 是最简单的查找算法,按照 顺序 查找算法, 程序从数据序列的第一个数据开始,逐个与匹配关键值比较, 直到找到匹配数据为止。

3.2. 对分查找

class BinarySearch {
    public static void main(String[] args) {
        int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        int key = 6;
        int low = 0;
        int high = array.length - 1;
        int mid;
        int index = -1;
        while(low <= high) {
            mid = (low + high) / 2;
            if (array[mid] == key) {
                index = mid;
                break;
            } else if (array[mid] < key) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        System.out.println(index);
    }
}
5
class BinarySearch {
    public static void main(String[] args) {
        int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        int index = binarySearch(array, 0, array.length - 1, 6);
        System.out.println(index);
    }
    public static int binarySearch(int[] array, int low, int high, int key) {
        if (low > high) {
            return -1;
        }
        int mid = (low + high) / 2;
        if (array[mid] == key) {
            return mid;
        } else if (array[mid] < key) {
            return binarySearch(array, mid + 1, high, key);
        } else {
            return binarySearch(array, low, mid - 1, key);
        }
    }
}
5

4. 链表

参考教材

  • 6.9 pp.182-188

5. 队列

参考教材

  • 6.10 pp.188-190

6. 堆栈

参考教材

  • 6.11 pp.190-193

7. 二叉树

参考教材

  • 6.12 pp.193-199