20162323周楠 2017-2018-1 《程序设计与数据结构》实验报告三

20162323周楠 2017-2018-1 《程序设计与数据结构》实验报告三

目录预览

实验三 查找与排序-1


查找与排序-1

实验结果

实验过程

public
class SearchAndSortTest {
    public static void main(String[] args) {
        String[] players= new String[]{"8", "6", "4", "46", "57", "89", "76", "20162323"};
        Integer[] numbers=new Integer[]{2323,07,22,18,29,56};
        Integer[] message=new Integer[]{2,3,5,8,10,17,78,69,2323};
        Integer[] data=new Integer[]{5,7,98,101,24,35,58,79,789,1};
        Character[] oddnumber = {  '1', '3', '5', '7', '9', '0'};
        String[] names= new String[]{"周", "楠", "吴", "鹿", "张", "刘昊然"};


        Searching search = new Searching();
        Sorting sort= new Sorting();

        System.out.println(search.linearSearch(players, "20162323"));
        System.out.println(search.linearSearch(message, 69));
        System.out.println(search.linearSearch(data, 35));
        System.out.println(search.linearSearch(oddnumber, '1'));
        System.out.println(search.linearSearch(names, "刘昊然"));

排序


for (int i = 0; i < players.length; i++)
            System.out.print(players[i]+" ");
            System.out.println();
        for (int j = 0; j < numbers.length; j++)
            System.out.print(numbers[j]+" ");
            System.out.println();
        for (int k = 0; k < message.length;k++)
            System.out.print(message[k]+" ");
            System.out.println();


实验三 查找与排序-2


中序先序序列构造二叉树

实验结果

实验过程

package test;

import cn.edu.besti.cs1623.zn20162323.Searching;
import cn.edu.besti.cs1623.zn20162323.Sorting;

public
class SearchAndSortTest {
    public static void main(String[] args) {
        String[] players= new String[]{"8", "6", "4", "46", "57", "89", "76", "20162323"};
        Integer[] numbers=new Integer[]{2323,07,22,18,29,56};
        Integer[] message=new Integer[]{2,3,5,8,10,17,78,69,2323};
        Integer[] data=new Integer[]{5,7,98,101,24,35,58,79,789,1};
        Character[] oddnumber = {  '1', '3', '5', '7', '9', '0'};
        String[] names= new String[]{"周", "楠", "吴", "鹿", "张", "刘昊然"};


        Searching search = new Searching();
        Sorting sort= new Sorting();

        System.out.println(search.linearSearch(players, "20162323"));
        System.out.println(search.linearSearch(message, 69));
        System.out.println(search.linearSearch(data, 35));
        System.out.println(search.linearSearch(oddnumber, '1'));
        System.out.println(search.linearSearch(names, "刘昊然"));


        sort.bubbleSort(players);
        sort.selectionSort(numbers);
        sort.insertionSort(message);

        for (int i = 0; i < players.length; i++)
            System.out.print(players[i]+" ");
            System.out.println();
        for (int j = 0; j < numbers.length; j++)
            System.out.print(numbers[j]+" ");
            System.out.println();
        for (int k = 0; k < message.length;k++)
            System.out.print(message[k]+" ");
            System.out.println();

    }
}


      

把Sorting.java Searching.java放入 cn.edu.besti.cs1623.zn2323 包中

把测试代码放test包中


实验三 查找与排序-3

![](http://images2017.cnblogs.com/blog/1062821/201711/1062821-20171112122256966-321655545.png)

实验结果

实验过程


public class Searching<S> {
    //-----------------------------------------------------------------

    //  Searches the specified array of objects using a linear search

    //  algorithm. Returns null if the target is not found.

    //-----------------------------------------------------------------

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

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

        return result;
    }

    //-----------------------------------------------------------------

    //  Searches the specified array of objects using a binary search

    //  algorithm. Returns null if the target is not found.

    //-----------------------------------------------------------------

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

        while (result == null && first <= last) {
            mid = (first + last) / 2;  // determine midpoint

            if (data[mid].compareTo(target) == 0)
                result = data[mid];
            else if (data[mid].compareTo(target) > 0)
                last = mid - 1;
            else first = mid + 1;
        }

        return result;
    }

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

        while (result == null && first <= last) {
            mid = last + (first - last) * (data[mid].compareTo(target) / (first - last));

            if (data[mid].compareTo(target) == 0) result = data[mid];
            else if (data[mid].compareTo(target) > 0) last = mid - 1;
            else first = mid + 1;
        }
        return result;
    }
}


资料参考


Java二叉树排序算法
[Java实现桶排序]( http://blog.csdn.net/l294265421/article/details/45848677
堆排序原理及Java实现
有序表查找


代码托管


代码托管


PSP(Personal Software Process)时间

步骤 耗时 百分比
需求分析 40min 6%
代码实现 500min 75.8%
测试 90min 13.6%
总结 30min 4.6%

posted on 2017-11-12 12:43  GiggleKV  阅读(214)  评论(1)    收藏  举报

导航