20172332 2017-2018-2 《程序設計與數據結構》實驗三報告

20172332 2017-2018-2 《程序設計與數據結構》實驗三報告

課程:《程序設計與數據結構》
班級: 1723
姓名: 於欣月
學號:20172332
實驗教師:王志強
實驗日期:2018年11月20日
必修/選修: 必修html

1.實驗內容

  • 查找與排序-1
  • 定義一個Searching和Sorting類,並在類中實現linearSearch(教材P162 ),SelectionSort方法(P169),最後完成測試。
    要求很多於10個測試用例,提交測試用例設計狀況(正常,異常,邊界,正序,逆序),用例數據中要包含本身學號的後四位
    提交運行結果圖。
  • 查找與排序-2
  • 重構你的代碼
    把Sorting.java Searching.java放入 cn.edu.besti.cs1723.(姓名首字母+四位學號) 包中(例如:cn.edu.besti.cs1723.G2301)
    把測試代碼放test包中
    從新編譯,運行代碼,提交編譯,運行的截圖(IDEA,命令行兩種)
  • 查找與排序-3
  • 參考http://www.cnblogs.com/maybe2030/p/4715035.html 在Searching中補充查找算法並測試
    提交運行結果截圖
  • 查找與排序-4
  • 補充實現課上講過的排序方法:希爾排序,堆排序,二叉樹排序等(至少3個)
    測試實現的算法(正常,異常,邊界)
    提交運行結果截圖
    (3分,若是編寫多個排序算法,即便其中三個排序程序有瑕疵,也能夠酌情得滿分)
  • 查找與排序-5(選作,加分)
  • 編寫Android程序對各類查找與排序算法進行測試
    提交運行結果截圖
    推送代碼到碼雲
    (加分3,加到實驗中)

2. 實驗過程及結果

前期準備:

  • 1.瞭解多種查找方法與排序方法的原理及實現

過程:

  • 1.實驗一
    • 關鍵代碼:
public static <T> boolean linearSearch(T[] data, int min, int max, T target) {
        int index = min;
        boolean found = false;

        while (!found && index <= max) {
            found = data[index].equals(target);
            index++;
        }

        return found;
    }
//線性查找
public static <T> boolean linearSearch(T[] data, int min, int max, T target) {
        int index = min;
        boolean found = false;

        while (!found && index <= max) {
            found = data[index].equals(target);
            index++;
        }

        return found;
    }
//選擇排序
public static <T extends Comparable<T>>
        String selectionSort(T[] data)
    {
        int min;
        T temp;

        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);
        }
        String res = "";
        for(int i = 0;i<data.length;i++)
            res += data[i]+",";
        return res;
    }
- 實驗結果:

  • 2.實驗二
    • 關鍵代碼:
      與實驗一相同。
    • 實驗結果:java

  • 3.實驗三
    • 關鍵代碼:
/**
     * Searches the specified array of objects using a binary search
     * algorithm.
     *
     * @param data   the array to be searched
     * @param min    the integer representation of the minimum value
     * @param max    the integer representation of the maximum value
     * @param target the element being searched for
     * @return true if the desired element is found
     */
    //二分查找
    public static <T extends Comparable<T>> boolean binarySearch(T[] data, int min, int max, T target) {
        boolean found = false;
        int midpoint = (min + max) / 2;  // determine the midpoint

        if (data[midpoint].compareTo(target) == 0)
            found = true;

        else if (data[midpoint].compareTo(target) > 0) {
            if (min <= midpoint - 1)
                found = binarySearch(data, min, midpoint - 1, target);
        } else if (midpoint + 1 <= max)
            found = binarySearch(data, midpoint + 1, max, target);

        return found;
    }

    //順序查找
    public static int SequenceSearch(int a[], int value, int n) {
        int i;
        for (i = 0; i < n; i++)
            if (a[i] == value)
                return i;
        return -1;
    }

    //二分查找兩個版本
    public static int BinarySearch1(int a[], int value, int n) {
        int low, high, mid;
        low = 0;
        high = n - 1;
        while (low <= high) {
            mid = (low + high) / 2;
            if (a[mid] == value)
                return mid;
            if (a[mid] > value)
                high = mid - 1;
            if (a[mid] < value)
                low = mid + 1;
        }
        return -1;
    }

    //二分查找,遞歸版本
    public static int BinarySearch2(int a[], int value, int low, int high) {
        if (high < low)
            return -1;
        int mid = low + (high - low) / 2;
        if (a[mid] == value)
            return mid;
        else if (a[mid] > value)
            return BinarySearch2(a, value, low, mid - 1);
        else if (a[mid] < value)
            return BinarySearch2(a, value, mid + 1, high);
        else
            return -1;
    }

    //插值查找
    public static int InsertionSearch(int[] data, int target, int min, int max) {
        if (min == max)
            return -1;
        int mid = min + (target - data[min]) / (data[max] - data[min]) * (max - min);
        if (data[mid] == target)
            return mid;
        else if (data[mid] > target)
            return InsertionSearch(data, target, min, mid - 1);
        else if (data[mid] < target)
            return InsertionSearch(data, target, mid + 1, max);
        else
            return -1;
    }

    //斐波那契查找
    private static int[] fibonacci() {
        int[] f = new int[10];
        f[0] = 0;
        f[1] = 1;
        for (int i = 2; i < f.length; i++) {
            f[i] = f[i - 1] + f[i - 2];
        }
        return f;
    }

    public static int fibonacciSearch(int[] data, int key) {
        int low = 0;
        int high = data.length - 1;
        int mid = 0;
        // 斐波那契分割數值下標
        int k = 0;
        // 序列元素個數
        int i = 0;
        // 獲取斐波那契數列
        int[] f = fibonacci();
        // 獲取斐波那契分割數值下標
        while (data.length > f[k] - 1) {
            k++;
        }

        // 建立臨時數組
        int[] temp = new int[f[k] - 1];
        for (int j = 0; j < data.length; j++)
            temp[j] = data[j];

        // 序列補充至f[k]個元素
        // 補充的元素值爲最後一個元素的值
        for (i = data.length; i < f[k] - 1; i++) {
            temp[i] = temp[high];
        }

        while (low <= high) {
            // low:起始位置
            // 前半部分有f[k-1]個元素,因爲下標從0開始
            // 則-1 獲取 黃金分割位置元素的下標
            mid = low + f[k - 1] - 1;

            if (temp[mid] > key) {
                // 查找前半部分,高位指針移動
                // (所有元素) = (前半部分)+(後半部分)
                // f[k] = f[k-1] + f[k-1]
                // 由於前半部分有f[k-1]個元素,因此 k = k-1
                high = mid - 1;
                k = k - 1;

            } else if (temp[mid] < key) {
                // 查找後半部分,高位指針移動
                // (所有元素) = (前半部分)+(後半部分)
                // f[k] = f[k-1] + f[k-1]
                // 由於後半部分有f[k-1]個元素,因此 k = k-2
                low = mid + 1;
                k = k - 2;
            } else {
                // 若是爲真則找到相應的位置
                if (mid <= high) {
                    return mid;
                } else {
                    // 出現這種狀況是查找到補充的元素
                    // 而補充的元素與high位置的元素同樣
                    return high;
                }
            }
        }
        return -1;
    }

    public static int treeSearch(int[] arr, int key) {
        LinkedBinarySearchTree tree = new LinkedBinarySearchTree();
        for (int a = 0; a < arr.length; a++) {
            tree.addElement(arr[a]);
        }
        if (tree.find(key) != null)
            return (int) tree.find(key);
        else
            throw new ElementNotFoundException();
    }

    //分塊查找
    //index表明索引數組,st2表明待查找數組,keytype表明要查找的元素,m表明每塊大小
    public static int blocksearch(int[] index, int[] st2, int keytype, int m) {
        int i = linearSearch(index, 0, st2.length - 1, keytype);    //shunxunsearch函數返回值爲帶查找元素在第幾塊
        if (i >= 0) {
            int j = m * i;   //j爲第i塊的第一個元素下標
            int curlen = (i + 1) * m;
            while (j < curlen) {
                if (st2[j] == keytype)
                    return j;
                j++;
            }
        }
        return -1;
    }

    public static <T> int linearSearch(int[] data, int min, int max, int target) {
        int index = min;

        if (data[index] >= target)
            return 0;
        int i=1;
        while(i<data.length) {
            if((data[i-1]<target)&&(data[i]>target))
                return i;
            else
                i++;
        }
        return -1;
    }

    //哈希查找
    public static int hashSearch(int data[], int target) {
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < data.length; i++)
            hashMap.put(Integer.hashCode(data[i]), data[i]);

        int key = Integer.hashCode(target);
        if (hashMap.containsKey(key))
            return hashMap.get(key);

        return -1;//找不到返回-1
    }
- 實驗結果:

  • 實驗四
    • 關鍵代碼:
public static <T extends Comparable<T>>
        String selectionSort(T[] data)
    {
        int min;
        T temp;

        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);
        }
        String res = "";
        for(int i = 0;i<data.length;i++)
            res += data[i]+",";
        return res;
    }

    /**
     * Swaps to elements in an array. Used by various sorting algorithms.
     *
     * @param data   the array in which the elements are swapped
     * @param index1 the index of the first element to be swapped
     * @param index2 the index of the second element to be swapped
     */
    private static <T extends Comparable<T>> void swap(T[] data, int index1, int index2)
    {
        T temp = data[index1];
        data[index1] = data[index2];
        data[index2] = temp;
    }
    
    /**
     * Sorts the specified array of objects using an insertion
     * sort algorithm.
     *
     * @param data the array to be sorted
     */
    public static <T extends Comparable<T>> void insertionSort(T[] data)
    {
        for (int index = 1; index < data.length; index++)
        {
            T 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.
     *
     * @param data the array to be sorted
     */
    public static <T extends Comparable<T>> void bubbleSort(T[] data)
    {
        int position, scan;
        T temp;

        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 merge sort
     * algorithm.
     *
     * @param data the array to be sorted
     */
    public static <T extends Comparable<T>> void mergeSort(T[] data)
    {
        mergeSort(data, 0, data.length - 1);
    }

    /**
     * Recursively sorts a range of objects in the specified array using the
     * merge sort algorithm.
     *
     * @param data the array to be sorted
     * @param min  the index of the first element
     * @param max  the index of the last element
     */
    private static <T extends Comparable<T>> void mergeSort(T[] 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);
        }
    }

    /**
     * Merges two sorted subarrays of the specified array.
     *
     * @param data the array to be sorted
     * @param first the beginning index of the first subarray
     * @param mid the ending index fo the first subarray
     * @param last the ending index of the second subarray
     */
    @SuppressWarnings("unchecked")
    private static <T extends Comparable<T>> void merge(T[] data, int first, int mid, int last)
    {
        T[] temp = (T[])(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];
   }

    /**
     * Sorts the specified array of objects using the quick sort algorithm.
     *
     * @param data the array to be sorted
     */
    public static <T extends Comparable<T>> void quickSort(T[] data)
    {
        quickSort(data, 0, data.length - 1);
    }

    /**
     * Recursively sorts a range of objects in the specified array using the
     * quick sort algorithm.
     *
     * @param data the array to be sorted
     * @param min  the minimum index in the range to be sorted
     * @param max  the maximum index in the range to be sorted
     */
    private static <T extends Comparable<T>> void quickSort(T[] data, int min, int max)
    {
        if (min < max)
        {
            // create partitions
            int indexofpartition = partition(data, min, max);

            // sort the left partition (lower values)
            quickSort(data, min, indexofpartition - 1);

            // sort the right partition (higher values)
            quickSort(data, indexofpartition + 1, max);
        }
    }

    /**
     * Used by the quick sort algorithm to find the partition.
     *
     * @param data the array to be sorted
     * @param min  the minimum index in the range to be sorted
     * @param max  the maximum index in the range to be sorted
     */
    private static <T extends Comparable<T>> int partition(T[] data, int min, int max)
    {
        T partitionelement;
        int left, right;
        int middle = (min + max) / 2;

        // use the middle data value as the partition element
        partitionelement = data[middle];
        // move it out of the way for now
        swap(data, middle, min);

        left = min;
        right = max;

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

            // search for an element that is < the partition element
            while (data[right].compareTo(partitionelement) > 0)
                right--;

            // swap the elements
            if (left < right)
                swap(data, left, right);
        }

        // move the partition element into place
        swap(data, min, right);

        return right;
    }
    public static String Xiersort(int[] arrays){
        String res = "";
        if(arrays == null || arrays.length <= 1){
            return null;
        }
        //增量
        int incrementNum = arrays.length/2;
        while(incrementNum >=1){
            for(int i=0;i<arrays.length;i++){
                //進行插入排序
                for(int j=i;j<arrays.length-incrementNum;j=j+incrementNum){
                    if(arrays[j]>arrays[j+incrementNum]){
                        int temple = arrays[j];
                        arrays[j] = arrays[j+incrementNum];
                        arrays[j+incrementNum] = temple;
                    }
                }
            }
            //設置新的增量
            incrementNum = incrementNum/2;
        }
        for (int i = 0;i<arrays.length;i++)
            res+=arrays[i]+" ";
        return res;
    }
public void heapSort() {
        int[] a = {2,4,1,9,3,23,32};
        assertEquals("1 2 3 4 9 23 32 ", HeapSort.HeapSort(a));
    }
public static void main(String[] args) {
        int[] num = {5,1,0,9,7,36,24,15,23,32};
        LinkedBinarySearchTree a = new LinkedBinarySearchTree(num[0]);
        for(int i = 1;i<num.length;i++)
            a.addElement(num[i]);
        a.inOrder(a.root);
        System.out.println();
        Integer[] nu ={5,1,0,9,7,36,24,15,23,32};
        Sorting.quickSort(nu);
        for (int o = 0;o<nu.length;o++)
            System.out.print(nu[o]+" ");
        Sorting.insertionSort(nu);
        for (int o = 0;o<nu.length;o++)
            System.out.print(nu[o]+" ");
        Sorting.selectionSort(nu);
        for (int o = 0;o<nu.length;o++)
            System.out.print(nu[o]+" ");
        Sorting.bubbleSort(nu);
        for (int o = 0;o<nu.length;o++)
            System.out.print(nu[o]+" ");
        Sorting.mergeSort(nu);
        for (int o = 0;o<nu.length;o++)
            System.out.print(nu[o]+" ");
    }
- 實驗結果:








  • 實驗五
    • 關鍵代碼:
      基本與idea中的沒變,只是加了佈局文件。
    • 實驗結果:算法
















3. 實驗過程當中遇到的問題和解決過程

  • 問題1:數組

  • 問題1解決方案:T不兼容Comparable型,因此須要改成Integer型的數組。數據結構

其餘(感悟、思考等)

  • 我以爲此次的實驗由於學完的過久了,因此有些方法不記得原理了,就又翻了一遍書,正好溫習了一下。可是實驗總體的難度並不大,原理都清楚就是代碼實現的問題,須要花一些時間。

參考資料

相關文章
相關標籤/搜索