20182320 2019-2020-1「オブジェクト指向プログラミングとデータ構造」実験報告7

20182320 2019-2020-1「オブジェクト指向プログラミングとデータ構造」実験報告7

コース:「プログラミングとデータ構造」

クラス:1823

名前:鄭李元

学生ID:20182320

王志強:教師実験

実験日:2019年11月1日

必修/選択科目:必修

1.実験の内容

1.1

検索とソートは、クラスを定義し、実装linearSearch、選択ソートクラスメソッド、そして最後にテストを完了すること。
試験デザイン提出テストケース(正常、異常、境界、正のシーケンス、逆)、10以上が必要で、データは、それらの後の実施例の学生番号4に含まれる
図の提出演算結果。

1.2

あなたのコードのリファクタリング
(例えば:cn.edu.besti.cs1823.G2301)cn.edu.besti.cs1823(イニシャル+ 4桁の学生番号)パッケージにSorting.java Searching.javaにする。
テストコードをテストパッケージを入れて
、再コンパイルしたコードを実行し、コンパイルに提出し、実行ショット(IDEA、コマンドライン)

1.3

参考http://www.cnblogs.com/maybe2030/p/4715035.html、検索を追加することで、検索アルゴリズム検索アルゴリズムの多様性を学び、テスト
のスクリーンショット操作する結果を提出

1.4

ヒルソート、ヒープソート、ソート、バイナリツリー(少なくとも3):補足授業の実現について話法のソート
アルゴリズムの(通常、異常、境界)テスト実装
しても、複数のソートアルゴリズムを記述する場合(提出業績ショット3ソートプログラム)必要に応じて、また満点を得ることができ、不備

1.5

Androidアプリを見つけてテストするアルゴリズムをソートの様々な達成するために
提出実行結果のスクリーンショット
コードクラウドへプッシュコードを(行うために選出された、ポイント)

2.実験の手順と結果

2.1

最初のステップ:書き込み検索とソートクラス

検索カテゴリ

public class Searching {
    private int List[];
    private int searchElement;
    private int listSize;
    private int locate;

    public Searching(int[] list, int searchElement) {
        List = list;
        this.searchElement = searchElement;
        listSize= List.length;
    }

    public boolean linearSearch(){
        for (int i=listSize-1;;i--){
            if (searchElement==List[i]){
                locate=i;
                return true;
            }
            else if (i==0){
                return false;
            }
        }
    }
}

クラス分類

public class Sorting {
    private int List[];
    private int size;

    public Sorting(int[] list) {
        List = list;
        size=List.length;
    }

    public void selectionSort(){
        int temp,tempx;
        for (int i=0;i<size-1;i++){
            temp=i;
            for (int j=i+1;j<size;j++){
                if (List[temp]>List[j]){
                    temp=j;
                }
            }
            if (temp == 0) {
                continue;
            }
            else {
                tempx=List[i];
                List[i]=List[temp];
                List[temp]=tempx;
            }
        }
    }
}

2クラス上の自分の書き込みを実行するときに最初の配列を作成し、いくつかのそれを埋める、それが元の配列を変更しますソートクラスの使用に言うことである、それを達成するために、配列を実行することです。

ステップ2:2つのクラスを実行するために、main関数を呼び出して記述したコード

コードを実行します。

public class SearchingTest {
    public static void main(String[] args) {
        int a1[]={1,2,3,4,5,6,7,2320};
        int a2[]={9,8,7,6,5,4,3,2320};
        System.out.println("线性查找:");
        System.out.println(new Searching(a1,4).linearSearch());
        System.out.println(new Searching(a1,6).linearSearch());
        System.out.println(new Searching(a1,1).linearSearch());
        System.out.println(new Searching(a1,2320).linearSearch());
        System.out.println(new Searching(a1,999).linearSearch());

        System.out.println(new Searching(a2,4).linearSearch());
        System.out.println(new Searching(a2,6).linearSearch());
        System.out.println(new Searching(a2,9).linearSearch());
        System.out.println(new Searching(a2,2320).linearSearch());
        System.out.println(new Searching(a2,999).linearSearch());
    }
}

結果:

2.2

最初のステップ:パッケージcn.edu.besti.cs1823.Z2320、および検索とソートクラスを作成し、パッケージにコードを実行します

ステップ2:Windowsのコマンドラインを開き、その後、コードを実行するために実行するコマンドラインを使用し、クラスを生成した3つの.javaファイルをコンパイルして実行するコマンドを使用します


2.3

ステップ:修飾クラスの検索挿入を見つけるためにバイナリサーチを追加、フィボナッチ探索、検索、以下のようにバイナリハッシュルックアップチェーンアドレスが注釈のそれぞれの機能を説明するために、実装されます

public class Searching {
    private int List[];
    private int searchElement;
    private int listSize;
    private int locate;

    public Searching(int[] list, int searchElement) {
        List = list;
        this.searchElement = searchElement;
        listSize= List.length;
    }

//线性查找
    public String linearSearch(){
        for (int i=listSize-1;;i--){
            if (searchElement==List[i]){
                locate=i;
                return "Element found! At List["+locate+"].";
            }
            else if (i==0){
                return "Element not found!";
            }
        }
    }

//二分法查找
    public String binarySearch(){
        int high=listSize-1;
        int low=0;
        int middle=(high+low)/2;

        //调用Sorting方法排序
        Sorting sorting=new Sorting(List);

        while (low<=high){

            if (List[middle]==searchElement){
                locate=middle;
                return "Element found! At List["+locate+"].";
            }
            else {
                if (searchElement<List[middle]){
                    high=middle-1;
                }
                else {
                    low=middle+1;
                }
            }
        }
        return "Element not found!";
    }

//插入查找
    public String insertionSearch(int low,int high){
        //调用Sorting方法排序
        Sorting sorting=new Sorting(List);

        if (low>high){
            return "Element not found!";
        }
        int mid=low+(searchElement-List[low])/(List[high]-List[low])*(high-low);
        if (List[mid]==searchElement){
            locate=mid;
            return "Element found! At List["+locate+"].";
        }
        else if (searchElement>List[mid]){
            low=mid+1;
        }
        else if (searchElement<List[mid]){
            high=mid-1;
        }
        return insertionSearch(low,high);
    }


//斐波那契查找
    //先建立斐波那契数组
    private int maxSize=20;
    public void Fibonacci(int F[]){
        F[0]=0;
        F[1]=1;
        for (int i=2;i<maxSize;i++){
            F[i]=F[i-1]+F[i-2];
        }
    }
    //斐波那契查找方法
    public int fibonacciSearching(){
        int low=0;
        int high=listSize-1;

        //创建斐波那契数组
        int F[]=new int[maxSize];
        Fibonacci(F);

        //计算n位于斐波那契数列的位置
        int k=0;
        while (listSize>F[k]-1){
            k++;
        }

        int temp[]= Arrays.copyOf(List,F[k]);

        for(int i=listSize;i<F[k]-1;++i){
            temp[i]=List[listSize-1];
        }

        while(low<=high) {
            int mid = low + F[k - 1] - 1;
            if (searchElement < temp[mid]) {
                high = mid - 1;
                k -= 1;
            } else if (searchElement > temp[mid]) {
                low = mid + 1;
                k -= 2;
            } else {
                if (mid < listSize)
                    return mid; //若相等则说明mid即为查找到的位置
                else
                    return listSize - 1; //若mid>=n则说明是扩展的数值,返回n-1
            }
        }
        return -1;
    }

//二叉树查找
    public boolean BinaryTreeSearching(){
        //建立二叉树
        BSTNode root=new BSTNode(List[0]);
        BSTree bsTree=new BSTree(root);
        locate=0;
        for (int i=0;i<List.length;i++){
            bsTree.add(List[i],root);
        }

        //开始查找
        return bsTree.search(searchElement,root);
    }

    public void BlockSearch(){
        int[] index = new int[]{10, 20, 30,2321};
        BlockSearch blockSearch=new BlockSearch(index);
        for (int i=0;i<List.length;i++){
            blockSearch.insert(List[i]);
        }
        blockSearch.search(searchElement);
    }

//哈希查找
    public void hash_Chain(){
        int defaultCapacity=100;
        LinearNode<Integer>[] temp;
        temp=new LinearNode[defaultCapacity];

        //把数组用链地址法放入另一个数组中
        for (int i=0;i<List.length;i++){
            int locate=List[i]%11;
            //检测扩容
            if (locate>=defaultCapacity){
                defaultCapacity=defaultCapacity*2;
                LinearNode<Integer>[] larger=new LinearNode[defaultCapacity];
                for (int j=0;i<temp.length;i++){
                    larger[j]=temp[j];
                }
                temp=larger;
            }

            LinearNode<Integer> b;
            b=temp[locate];
            if (b==null){
                LinearNode<Integer> tempx=new LinearNode(List[i]);
                temp[locate]=tempx;
            }
            else {
                while (b!=null){
                    if (b.getNext()==null){
                        break;
                    }
                    b=b.getNext();
                }
                b.setNext(new LinearNode(List[i]));
            }
        }

        //开始查找
        int locate2=searchElement%11;
        LinearNode<Integer>[] temp3=temp;
        while (temp3[locate2]!=null){
            if (temp3[locate2].getElement()==searchElement){
                System.out.println("哈希链式查找:true");
                break;
            }
            temp3[locate2]=temp3[locate2].getNext();
        }
        if (temp[locate2]==null){
            System.out.println("哈希链式查找:false");
        }
    }
}

ステップ2:追加のテストコードと実行

2.4

手順:以下のように変更ソートクラスサプリメントヒルは二分法のソート、ヒープソートを、ソート

public class Sorting {
    private static int List[];
    private static int size;
    private static String list="";

    public Sorting(int[] list) {
        List = list;
        size=List.length;
    }

//选择排序
    public String selectionSort(){
        int temp,tempx;
        for (int i=0;i<size-1;i++){
            temp=i;
            for (int j=i+1;j<size;j++){
                if (List[temp]>List[j]){
                    temp=j;
                }
            }
            if (temp == 0) {
                continue;
            }
            else {
                tempx=List[i];
                List[i]=List[temp];
                List[temp]=tempx;
            }
        }
        String string="";
        for (int i=0;i<size;i++){
            string=string+List[i]+" ";
        }
        return string;
    }

//希尔排序
    public String shellSort(){
        int temp=List.length/2;
        int first,last;
        while (temp>0){
            for (int i=0;i+temp<List.length-1;i++){
                first=i;
                last=i+temp;
                if (List[first]>List[last]){
                    int temp2=List[first];
                    List[first]=List[last];
                    List[last]=temp2;
                }
            }
            temp=temp/2;
        }


        String result="";
        for (int i=0;i<List.length;i++){
            result=result+List[i]+" ";
        }
        return result;
    }

//二叉树排序
    public void binarySort() throws Exception {
        BSTNode roots = new BSTNode();
        for (int number : List) {
            roots.add(number);
        }
        System.out.println(roots.values());
    }

//堆排序
    public static void heapify(int[] arrays, int currentRootNode, int size) {
        if (currentRootNode < size) {
            //左子树和右字数的位置
            int left = 2 * currentRootNode + 1;
            int right = 2 * currentRootNode + 2;

            //把当前父节点位置看成是最大的
            int max = currentRootNode;

            if (left < size) {
                //如果比当前根元素要大,记录它的位置
                if (arrays[max] < arrays[left]) {
                    max = left;
                }
            }
            if (right < size) {
                //如果比当前根元素要大,记录它的位置
                if (arrays[max] < arrays[right]) {
                    max = right;
                }
            }
            //如果最大的不是根元素位置,那么就交换
            if (max != currentRootNode) {
                int temp = arrays[max];
                arrays[max] = arrays[currentRootNode];
                arrays[currentRootNode] = temp;

                //继续比较,直到完成一次建堆
                heapify(arrays, max, size);
            }
        }
    }
    public static void maxHeapify(int[] arrays, int size) {

        // 从数组的尾部开始,直到第一个元素(角标为0)
        for (int i = size - 1; i >= 0; i--) {
            heapify(arrays, i, size);
        }

    }
    public static String dui(int[] arrays) {
        for (int i = 0; i < arrays.length; i++) {

            //每次建堆就可以排除一个元素了
            maxHeapify(arrays, arrays.length - i);

            //交换
            int temp = arrays[0];
            arrays[0] = arrays[(arrays.length - 1) - i];
            arrays[(arrays.length - 1) - i] = temp;

        }
        String result="";
        for(int i=0;i<arrays.length;i++)
        {
            result =result+ "  "+arrays[i];
        }
        return  result;
    }
}

ステップ2:変更とテストコードを実行

2.5

最初のステップ:レイアウトを作ります

ステップ2:アイデアは、テストコードに加えて、コードに転送されAndroidStudio

第三段階:MainActivityのレイアウトを実現するには、リスナー、入力および出力試験結果を実現し、ラン


3.実験過程で発生した問題や決済処理

質問1:

実行するには、システムへの完全なコードフィットのアイデアは、現象によってそこにコンパイルされることはありません実験では、7.2。

この問題を解決するために1:

コードをインポートおよびパッケージのステートメントを含んでいることが分かっているので、先生に尋ねることで、転送パッケージの包装に問題がありました。正常に実行するコードのすべてのインポートおよびパッケージのステートメントを削除します。

その他(知覚、思考、など)

私たちは、より高いレベルを必要とするとき、実験方法はそう、異なるロジックと効率性、一般的には、単純な、方法のより良い理解を持って、効率が非常に高いではありません検索し、さまざまな方法でソートの様々な関係しますプログラミング作業は、我々は、論理的思考能力を発揮する必要があるときに、より複雑な習得するが、アルゴリズムのプログラムのパフォーマンスを向上させることができます。

参考資料

おすすめ

転載: www.cnblogs.com/leonzheng/p/11869110.html