20192307 2020-2021-1 《数据结构与面向对象程序设计》实验七报告

20192307 2020-2021-1 《数据结构与面向对象程序设计》实验七报告

  • 课程:《数据结构与面向对象程序设计》
  • 班级: 1923
  • 姓名: 常万里
  • 学号: 20192307
  • 实验教师:王志强老师
  • 实验日期:2020年11月19日
  • 必修/选修: 必修

一、实验内容

  • 1.定义一个Searching和Sorting类,并在类中实现linearSearch,SelectionSort方法,最后完成测试。
    要求不少于10个测试用例,提交测试用例设计情况(正常,异常,边界,正序,逆序),用例数据中要包含自己学号的后四位
  • 2.重构代码
    把Sorting.java Searching.java放入 cn.edu.besti.cs1923.(姓名首字母+四位学号) 包中(例如:cn.edu.besti.cs1823.G2301)
    把测试代码放test包中,重新编译,运行代码,提交编译,运行的截图(IDEA,命令行两种)
  • 3.参考http://www.cnblogs.com/maybe2030/p/4715035.html ,学习各种查找算法并在Searching中补充查找算法并测试
  • 4.补充实现课上讲过的排序方法:希尔排序,堆排序,二叉树排序等(至少3个)
    测试实现的算法(正常,异常,边界)
  • 5.编写Android程序对实现各种查找与排序算法进行测试

二、实验过程及结果

(一)排序

Sorting

其中包含选择排序、归并排序、冒泡排序、快速排序、基数排序、插入排序、计数排序、堆排序

package cn.edu.besti.cs1923.C2307;
//********************************************************************
//  Sorting.java       Java Foundations
//
//  Contains various sort algorithms that operate on an array of
//  Comparable objects.
//********************************************************************

/**
 * @author Shape Of My Heart
 */
public class Sorting {
    //-----------------------------------------------------------------
    //  Sorts the specified array of integers using the selection
    //  sort algorithm.
    //-----------------------------------------------------------------
    public static void selectionSort(Comparable[] data) {
        int min;

        for (int index = 0; index < data.length - 1; index++) {
            min = index;
            for (int scan = index + 1; scan < data.length; scan++) {
                if (data[scan].compareTo(data[min]) < 0) {
                    min = scan;
                }
            }

            swap(data, min, index);
        }
    }

    //-----------------------------------------------------------------
    //  Swaps two elements in the specified array.
    //-----------------------------------------------------------------
    private static void swap(Comparable[] data, int index1, int index2) {
        Comparable temp = data[index1];
        data[index1] = data[index2];
        data[index2] = temp;
    }

    //-----------------------------------------------------------------
    //  Sorts the specified array of objects using an insertion
    //  sort algorithm.
    //-----------------------------------------------------------------
    public static void insertionSort(Comparable[] data) {
        for (int index = 1; index < data.length; index++) {
            Comparable key = data[index];
            int position = index;

            // Shift larger values to the right
            while (position > 0 && data[position - 1].compareTo(key) > 0) {
                data[position] = data[position - 1];
                position--;
            }

            data[position] = key;
        }
    }

    //-----------------------------------------------------------------
    //  Sorts the specified array of objects using a bubble sort
    //  algorithm.
    //-----------------------------------------------------------------
    public static void bubbleSort(Comparable[] data) {
        int position, scan;

        for (position = data.length - 1; position >= 0; position--) {
            for (scan = 0; scan <= position - 1; scan++) {
                if (data[scan].compareTo(data[scan + 1]) > 0) {
                    swap(data, scan, scan + 1);
                }
            }
        }
    }

    //-----------------------------------------------------------------
    //  Sorts the specified array of objects using the quick sort
    //  algorithm.
    //-----------------------------------------------------------------
    public static void quickSort(Comparable[] data, int min, int max) {
        int pivot;

        if (min < max) {
            pivot = partition(data, min, max);  // make partitions
            quickSort(data, min, pivot - 1);  // sort left partition
            quickSort(data, pivot + 1, max);  // sort right partition
        }
    }

    //-----------------------------------------------------------------
    //  Creates the partitions needed for quick sort.
    //-----------------------------------------------------------------
    private static int partition(Comparable[] data, int min, int max) {
        // Use first element as the partition value
        Comparable partitionValue = data[min];

        int left = min;
        int right = max;

        while (left < right) {
            // Search for an element that is > the partition element
            while (data[left].compareTo(partitionValue) <= 0 && left < right) {
                left++;
            }

            // Search for an element that is < the partitionelement
            while (data[right].compareTo(partitionValue) > 0) {
                right--;
            }

            if (left < right) {
                swap(data, left, right);
            }
        }

        // Move the partition element to its final position
        swap(data, min, right);

        return right;
    }

    //-----------------------------------------------------------------
    //  Sorts the specified array of objects using the merge sort
    //  algorithm.
    //-----------------------------------------------------------------
    public static void mergeSort(Comparable[] data, int min, int max) {
        if (min < max) {
            int mid = (min + max) / 2;
            mergeSort(data, min, mid);
            mergeSort(data, mid + 1, max);
            merge(data, min, mid, max);
        }
    }

    //-----------------------------------------------------------------
    //  Sorts the specified array of objects using the merge sort
    //  algorithm.
    //-----------------------------------------------------------------
    public static void merge(Comparable[] data, int first, int mid,
                             int last) {
        Comparable[] temp = new Comparable[data.length];

        int first1 = first, last1 = mid;     // endpoints of first subarray
        int first2 = mid + 1, last2 = last;  // endpoints of second subarray
        int index = first1;                  // next index open in temp array

        //  Copy smaller item from each subarray into temp until one
        //  of the subarrays is exhausted
        while (first1 <= last1 && first2 <= last2) {
            if (data[first1].compareTo(data[first2]) < 0) {
                temp[index] = data[first1];
                first1++;
            } else {
                temp[index] = data[first2];
                first2++;
            }
            index++;
        }

        //  Copy remaining elements from first subarray, if any
        while (first1 <= last1) {
            temp[index] = data[first1];
            first1++;
            index++;
        }

        //  Copy remaining elements from second subarray, if any
        while (first2 <= last2) {
            temp[index] = data[first2];
            first2++;
            index++;
        }

        //  Copy merged data into original array
        for (index = first; index <= last; index++) {
            data[index] = temp[index];
        }
    }
}

希尔排序

package Sorting_Alogorithm; /**
 * \* Created with IntelliJ IDEA.
 * \* User: Shape Of My Heart
 * \* Date: 2020/11/23
 * \* Time: 16:39
 * \* Besides,some of the best things in life are total mistakes.
 * \* Description:
 * \
 **/

import java.util.Arrays;

/**
 * 希尔排序
 */
public class ShellSort extends Sort implements IArraySort {

    @Override
    public int[] sort(int[] sourceArray) throws Exception {
        // 对 arr 进行拷贝,不改变参数内容
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        int gap = 1;
        while (gap < arr.length) {
            gap = gap * 3 + 1;
        }

        while (gap > 0) {
            for (int i = gap; i < arr.length; i++) {
                int tmp = arr[i];
                int j = i - gap;
                while (j >= 0 && arr[j] > tmp) {
                    arr[j + gap] = arr[j];
                    j -= gap;
                }
                arr[j + gap] = tmp;
            }
            gap = (int) Math.floor(gap / 3);
        }

        return arr;
    }
}

快速排序

package Sorting_Alogorithm; /**
 * \* Created with IntelliJ IDEA.
 * \* User: Shape Of My Heart
 * \* Date: 2020/11/23
 * \* Time: 16:37
 * \* Besides,some of the best things in life are total mistakes.
 * \* Description:
 * \
 **/
import java.util.Arrays;

/**
 * 快速排序
 */
public class QuickSort extends Sort implements IArraySort {

    @Override
    public int[] sort(int[] sourceArray) throws Exception {
        // 对 arr 进行拷贝,不改变参数内容
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        return quickSort(arr, 0, arr.length - 1);
    }

    private int[] quickSort(int[] arr, int left, int right) {
        if (left < right) {
            int partitionIndex = partition(arr, left, right);
            quickSort(arr, left, partitionIndex - 1);
            quickSort(arr, partitionIndex + 1, right);
        }
        return arr;
    }

    private int partition(int[] arr, int left, int right) {
        // 设定基准值(pivot)
        int pivot = left;
        int index = pivot + 1;
        for (int i = index; i <= right; i++) {
            if (arr[i] < arr[pivot]) {
                swap(arr, i, index);
                index++;
            }
        }
        swap(arr, pivot, index - 1);
        return index - 1;
    }

    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}

基数排序

package Sorting_Alogorithm; /**
 * \* Created with IntelliJ IDEA.
 * \* User: Shape Of My Heart
 * \* Date: 2020/11/23
 * \* Time: 16:38
 * \* Besides,some of the best things in life are total mistakes.
 * \* Description:
 * \
 **/

import java.util.Arrays;

/**
 * 基数排序
 * <p>
 * 考虑负数的情况还可以参考: https://code.i-harness.com/zh-CN/q/e98fa9
 */
public class RadixSort extends Sort implements IArraySort {

    @Override
    public int[] sort(int[] sourceArray) throws Exception {
        // 对 arr 进行拷贝,不改变参数内容
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        int maxDigit = getMaxDigit(arr);
        return radixSort(arr, maxDigit);
    }

    /**
     * 获取最高位数
     */
    private int getMaxDigit(int[] arr) {
        int maxValue = getMaxValue(arr);
        return getNumLenght(maxValue);
    }

    private int getMaxValue(int[] arr) {
        int maxValue = arr[0];
        for (int value : arr) {
            if (maxValue < value) {
                maxValue = value;
            }
        }
        return maxValue;
    }

    protected int getNumLenght(long num) {
        if (num == 0) {
            return 1;
        }
        int lenght = 0;
        for (long temp = num; temp != 0; temp /= 10) {
            lenght++;
        }
        return lenght;
    }

    private int[] radixSort(int[] arr, int maxDigit) {
        int mod = 10;
        int dev = 1;

        for (int i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {
            // 考虑负数的情况,这里扩展一倍队列数,其中 [0-9]对应负数,[10-19]对应正数 (bucket + 10)
            int[][] counter = new int[mod * 2][0];

            for (int j = 0; j < arr.length; j++) {
                int bucket = ((arr[j] % mod) / dev) + mod;
                counter[bucket] = arrayAppend(counter[bucket], arr[j]);
            }

            int pos = 0;
            for (int[] bucket : counter) {
                for (int value : bucket) {
                    arr[pos++] = value;
                }
            }
        }

        return arr;
    }

    /**
     * 自动扩容,并保存数据
     *
     * @param arr
     * @param value
     */
    private int[] arrayAppend(int[] arr, int value) {
        arr = Arrays.copyOf(arr, arr.length + 1);
        arr[arr.length - 1] = value;
        return arr;
    }
}

堆排序

package Sorting_Alogorithm; /**
 * \* Created with IntelliJ IDEA.
 * \* User: Shape Of My Heart
 * \* Date: 2020/11/23
 * \* Time: 16:36
 * \* Besides,some of the best things in life are total mistakes.
 * \* Description:
 * \
 **/
import java.util.Arrays;

public class HeapSort extends Sort implements IArraySort {
    @Override
    public int[] sort(int[] sourceArray) throws Exception {
        // 对 arr 进行拷贝,不改变参数内容
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        int len = arr.length;

        buildMaxHeap(arr, len);

        for (int i = len - 1; i > 0; i--) {
            swap(arr, 0, i);
            len--;
            heapify(arr, 0, len);
        }
        return arr;
    }

    private void buildMaxHeap(int[] arr, int len) {
        for (int i = (int) Math.floor(len / 2); i >= 0; i--) {
            heapify(arr, i, len);
        }
    }

    private void heapify(int[] arr, int i, int len) {
        int left = 2 * i + 1;
        int right = 2 * i + 2;
        int largest = i;
        if (left < len && arr[left] > arr[largest]) {
            largest = left;
        }
        if (right < len && arr[right] > arr[largest]) {
            largest = right;
        }
        if (largest != i) {
            swap(arr, i, largest);
            heapify(arr, largest, len);
        }
    }
    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

计数排序

package Sorting_Alogorithm;
/*
  \* Created with IntelliJ IDEA.
  \* User: Shape Of My Heart
  \* Date: 2020/11/23
  \* Time: 16:36
  \* Besides,some of the best things in life are total mistakes.
  \* Description:
  \
 */
import java.util.Arrays;
public class CountingSort extends Sort implements IArraySort {
    @Override
    public int[] sort(int[] sourceArray) throws Exception {
        // 对 arr 进行拷贝,不改变参数内容
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        int maxValue = getMaxValue(arr);

        return countingSort(arr, maxValue);
    }

    private int[] countingSort(int[] arr, int maxValue) {
        int bucketLen = maxValue + 1;
        int[] bucket = new int[bucketLen];

        for (int value : arr) {
            bucket[value]++;
        }

        int sortedIndex = 0;
        for (int j = 0; j < bucketLen; j++) {
            while (bucket[j] > 0) {
                arr[sortedIndex++] = j;
                bucket[j]--;
            }
        }
        return arr;
    }

    private int getMaxValue(int[] arr) {
        int maxValue = arr[0];
        for (int value : arr) {
            if (maxValue < value) {
                maxValue = value;
            }
        }
        return maxValue;
    }

}

(二)查找

Searching

其中包含线性查找、二分查找、二分查找的递归版本、插值查找、二分查找树查找

package cn.edu.besti.cs1923.C2307;
/**
 * \* Created with IntelliJ IDEA.
 * \* User: Shape Of My Heart
 * \* Date: 2020/11/17
 * \* Time: 14:06
 * \* Besides,some of the best things in life are total mistakes.
 * \* Description:
 * \
 *
 * @author Shape Of My Heart
 */

public class Searching {
    public static Comparable linearSearch(Comparable[] date, Comparable target) {
        Comparable result = null;
        int index = 0;

        while (result == null && index < date.length) {
            if (date[index].compareTo(target) == 0) {
                result = date[index];
            }
            index++;
        }

        return result;
    }

    public static Comparable binarySearch(Comparable[] date, Comparable target) {
        Comparable result = null;
        int first = 0, last = date.length - 1, mid;

        while (result == null && first <= last) {
            mid = (first + last) / 2;
            if (date[mid].compareTo(target) == 0) {
                result = date[mid];
            } else {
                if (date[mid].compareTo(target) > 0) {
                    last = mid - 1;
                } else {
                    first = mid + 1;
                }
            }
        }
        return result;
    }
// 二分查找的递归版本
    public static int binarySearch4(int[] array, int value) {
        int low = 0;
        int high = array.length - 1;
        return searchmy(array, low, high, value);
    }

    private static int searchmy(int array[], int low, int high, int value) {
        if (low > high) {
            return -1;
        }
        int mid = low + ((high - low) >> 1);
        if (value == array[mid]) {
            return mid;
        }
        if (value < array[mid]) {
            return searchmy(array, low, mid - 1, value);
        }
        return searchmy(array, mid + 1, high, value);
    }

    public static int insertValueSearch(int[] arr, int left, int right, int value) {
        //找不到或者越界判定
        if (left > right || arr[left] > value || arr[right] < value) {
            return -1;
        }
        //中间数选择
        int mid = left + (right - left) * (value - arr[left]) / (arr[right] - arr[left]);
        int midValue = arr[mid];
        //判断
        //查找值小于中间值
        if (value < midValue) {
            return insertValueSearch(arr, left, mid - 1, value);
        } else if (value > midValue) {
            //查找值大于中间值
            return insertValueSearch(arr, mid + 1, right, value);
        } else {
            return mid;//找到了
        }
    }
//    public static Comparable insertionSearch(Comparable a[], Comparable value, Comparable low, Comparable high) {
//
//        int mid = low + (value - a[low]) / (a[high] - a[low]) * (high - low);
//        if (a[mid] == value) {
//            return mid;
//        }
//        if (a[mid].compareTo(value) > 0) {
//            return insertionSearch(a, value, low, mid - 1);
//        }
//        if (a[mid].compareTo(value) < 0) {
//            return insertionSearch(a, value, mid + 1, high);
//        }
//
//    }

}

斐波那契查找

package cn.edu.besti.cs1923.C2307;/*
\* Created with IntelliJ IDEA. 
\* User: Shape Of My Heart 
\* Date: 2020/11/23 
\* Time: 21:37 
\* Besides,some of the best things in life are total mistakes.
\* Description:
\**/
import java.util.Arrays;
public class FibonacciSearch {
    public static int FLENGTH = 20;

    public static void main(String[] args) {
        int[] arr = {1, 8, 10, 89, 100, 134};
        int target = 89;
        System.out.println("目标元素在数组中位置是:" + fibSearch(arr, target));
    }

    public static int[] fib() {
        int[] f = new int[FLENGTH];
        f[0] = 1;
        f[1] = 1;
        for (int i = 2; i < FLENGTH; i++) {
            f[i] = f[i - 1] + f[i - 2];
        }
        return f;
    }

    public static int fibSearch(int[] arr, int target) {
        int low = 0;
        int high = arr.length - 1;
        int k = 0;
        int mid = 0;
        int f[] = fib();
        /*获取最相邻的斐波那契数组中元素的值,该值略大于数组的长度*/
        while (high > f[k] - 1) {
            k++;
        }
        /*因为 f[k]值可能大于arr的长度。如果大于时,需要构造一个新的数组temp[],将arr数组中的元素拷贝过去,不足的部分会使用0填充*/
        int[] temp = Arrays.copyOf(arr, f[k]);
        /*然后将temp后面填充的0,替换为最后一位数字
         *如将temp数组由{1,8,10,89,100,134,0,0}变换为{1,8,10,89,100,134,134,134}*/
        for (int i = high + 1; i < temp.length; i++) {
            temp[i] = arr[high];
        }

        while (low <= high) {
            mid = low + f[k - 1] - 1;
            if (target < temp[mid]) {
                high = mid - 1;
                /*因为f[k]=f[k-1]+f[k-2],所以k--就相当于取temp数组的左边部分*/
                k--;
            } else if (target > temp[mid]) {
                low = mid + 1;
                /*同理,f[k]=f[k-1]+f[k-2],k -= 2就相当于取temp数组的右边部分*/
                k -= 2;
            } else {
                /*原arr数组中的值*/
                if (mid <= high) {
                    return mid;
                    /*在temp中,扩展出来的高位的值*/
                } else {
                    return high;
                }
            }
        }
        return -1;
    }
}

二分搜索树查找

package DataStructure.binarySearch;
public class BinarySearchTreeSearch<Key extends Comparable<Key>, Value> {
    // 根节点
    private Node root;
    // 树种的节点个数
    private int count;

    // 构造函数, 默认构造一棵空二分搜索树
    public BinarySearchTreeSearch() {
        root = null;
        count = 0;
    }

    // 返回二分搜索树的节点个数
    public int size() {
        return count;
    }

    // 返回二分搜索树是否为空
    public boolean isEmpty() {
        return count == 0;
    }

    // 向二分搜索树中插入一个新的(key, value)数据对
    public void insert(Key key, Value value) {
        root = insert(root, key, value);
    }

    // 查看二分搜索树中是否存在键key
    public boolean contain(Key key) {
        return contain(root, key);
    }

    // 在二分搜索树中搜索键key所对应的值。如果这个值不存在, 则返回null
    public Value search(Key key) {
        return search(root, key);
    }

    // 向以node为根的二分搜索树中, 插入节点(key, value), 使用递归算法
    // 返回插入新节点后的二分搜索树的根
    private Node insert(Node node, Key key, Value value) {

        if (node == null) {
            count++;
            return new Node(key, value);
        }

        if (key.compareTo(node.key) == 0) {
            node.value = value;
        } else if (key.compareTo(node.key) < 0) {
            node.left = insert(node.left, key, value);
        } else    // key > node->key
        {
            node.right = insert(node.right, key, value);
        }

        return node;
    }


    //********************
    //* 二分搜索树的辅助函数
    //********************

    // 查看以node为根的二分搜索树中是否包含键值为key的节点, 使用递归算法
    private boolean contain(Node node, Key key) {

        if (node == null) {
            return false;
        }

        if (key.compareTo(node.key) == 0) {
            return true;
        } else if (key.compareTo(node.key) < 0) {
            return contain(node.left, key);
        } else // key > node->key
        {
            return contain(node.right, key);
        }
    }

    // 在以node为根的二分搜索树中查找key所对应的value, 递归算法
    // 若value不存在, 则返回NULL
    private Value search(Node node, Key key) {

        if (node == null) {
            return null;
        }

        if (key.compareTo(node.key) == 0) {
            return node.value;
        } else if (key.compareTo(node.key) < 0) {
            return search(node.left, key);
        } else // key > node->key
        {
            return search(node.right, key);
        }
    }

    // 树中的节点为私有的类, 外界不需要了解二分搜索树节点的具体实现
    private class Node {
        private Key key;
        private Value value;
        private Node left, right;

        public Node(Key key, Value value) {
            this.key = key;
            this.value = value;
            left = right = null;
        }
    }
}

(三)测试代码

排序测试代码

package cn.edu.besti.cs1923.C2307;
import Sorting_Alogorithm.*;
import java.util.Arrays;

public class SortTest {
    public static void main(String[] args) {
        BubbleSort test1 = new BubbleSort();
        BucketSort test2 = new BucketSort();
        CountingSort test3 = new CountingSort();
        HeapSort test4 = new HeapSort();
        InsertSort test5 = new InsertSort();
        MergeSort test6 = new MergeSort();
        QuickSort test7 = new QuickSort();
        RadixSort test8 = new RadixSort();
        SelectionSort test9 = new SelectionSort();
        ShellSort test10 = new ShellSort();

        int[] array = {98, 25, 33, 16, 25, 68, 2307, 18, 36, 20, 12};

        System.out.println("\n\n下面进行冒泡排序:");
        try {
            System.out.println(Arrays.toString(test1.sort(array)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("\n\n下面进行桶排序:");
        try {
            System.out.println(Arrays.toString(test2.sort(array)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("\n\n下面进行计数排序:");
        try {
            System.out.println(Arrays.toString(test3.sort(array)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("\n\n下面进行堆排序:");
        try {
            System.out.println(Arrays.toString(test4.sort(array)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("\n\n下面进行插入排序:");
        try {
            System.out.println(Arrays.toString(test5.sort(array)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("\n\n下面进行归并排序:");
        try {
            System.out.println(Arrays.toString(test6.sort(array)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("\n\n下面进行快速排序:");
        try {
            System.out.println(Arrays.toString(test7.sort(array)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("\n\n下面进行基数排序:");
        try {
            System.out.println(Arrays.toString(test8.sort(array)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("\n\n下面进行选择排序:");
        try {
            System.out.println(Arrays.toString(test9.sort(array)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("\n\n下面进行希尔排序:");
        try {
            System.out.println(Arrays.toString(test10.sort(array)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

查找测试代码

package cn.edu.besti.cs1923.C2307;
import Sorting_Alogorithm.BubbleSort;
public class SearchTest {
    public static void main(String[] args) {
        Comparable[] x = new Comparable[11];
        x[0] = 25;
        x[1] = 98;
        x[2] = 25;
        x[3] = 33;
        x[4] = 12;
        x[5] = 16;
        x[6] = 68;
        x[7] = 18;
        x[8] = 36;
        x[9] = 20;
        x[10] = 2307;
        System.out.println("Original array:");
        for (Comparable player : x) {

            System.out.print(player + " ");
        }

        System.out.println("\n下面进行选择排序:");
        Sorting.selectionSort(x);
        System.out.println("positive sequence:");
        for (Comparable player : x) {
            System.out.print(player + " ");
        }

        System.out.println("\n\n下面进行线性查找");
        int target1 = 2307;
        Comparable found1 = Searching.linearSearch(x, target1);
        if (found1 == null) {
            System.out.println(target1 + " was not found.");
        } else {
            System.out.println("Found: " + found1);
        }

        System.out.println("\n\n下面进行二分查找");
        int target2 = 19;
        Comparable found2 = Searching.binarySearch(x, target2);

        if (found2 == null) {
            System.out.println(target2 + " 未找到此数字.");
        } else {
            System.out.println("Found: " + found2);
        }

        int[] array = {25, 98, 25, 33, 12, 16, 68, 18, 36, 20, 2307};

        System.out.println("\n\n下面进行二分查找(递归版本)");
        int target3 = 20;
        BubbleSort test1 = new BubbleSort();
        int found3 = 0;
        try {
            found3 = Searching.binarySearch4(test1.sort(array), target3);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (found3 == -1) {
            System.out.println(target3 + " 未找到此数字.");
        } else {
            System.out.println(target3 + " 目标元素在数组中位置是:" + + found3);
        }
        System.out.println("\n\n下面进行插值查找:");
        int target4 = 2307;
        int index = Searching.insertValueSearch(array, 6, 10, target4);
        if (index == -1) {
            System.out.println("未找到此数字");
        } else {
            System.out.println(target4 + " 目标元素在数组中位置是:" + index);
        }
        System.out.println("\n\n下面进行斐波那契查找:");
        int[] arr = {1, 8, 10, 89, 100, 134};
        int target = 89;
        System.out.println(target + " 目标元素在数组中位置是:" + FibonacciSearch.fibSearch(arr, target));
    }
}

(四)运行结果截图











(五)码云仓库地址

我的码云仓库地址

三、心得体会

  • 在这次实验过程中,我遇到了许多问题,其中既有知识上的漏洞,也有不细心导致的马虎,这一切都补充,完善,丰富,扩展了我的计算机知识体系。在不断修复问题的过程中,我使用了很多方式去查询资料,例如:《数据结构与面向对象程序设计》,博客园平台,CSDN平台,码云平台,知乎app,等。进一步熟悉了Android studio这个平台的使用与运行方式,提高了自己自主学习的能力,为我接下来学习数据结构以及JAVA语言程序设计打下了坚实的基础,并在不断探索的过程中逐步提升了自己。

四、参考资料

posted @ 2020-11-28 21:06  20192307常万里  阅读(88)  评论(0编辑  收藏  举报