1. 程式人生 > 實用技巧 >演算法與資料結構

演算法與資料結構

軟體需求分析與系統設計 https://edu.cnblogs.com/campus/zswxy/2018SE
這個作業要求在哪裡 https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406
這個作業的目標 演算法與資料結構
參考視訊 老九君資料結構

第一題:尋找陣列中第K大是數 考察演算法:排序演算法

1、氣泡排序演算法:

  • 比較相鄰的元素。如果第一個比第二個大,就交換它們兩個;
  • 對每一對相鄰元素作同樣的工作,從開始第一對到結尾的最後一對,這樣在最後的元素是最大的數;
  • 針對所有的元素重複以上的步驟,除了最後一個;
  • 重複步驟1~3,直到排序完成。

2、演算法分析:

  • 時間複雜度
    • 最好情況:O(n)
    • 最壞情況:O(n2)
    • 平均情況:O(n2)
  • 空間複雜度:O(1)
    • 穩定性:穩定

3、思路分析

1、Scanner輸入序列
2、陣列遍歷序列
3、設定標識變數,檢查是否交換
4、迴圈實現輸入次數
5、氣泡排序查詢第K大數,輸出結果

3、執行程式碼

'''java

  package com.company;
  import java.util.Scanner;
/**
 * 給定一個序列,每次詢問序列中第i個數到第r個數中的第K大的數是那個
 *
 *
 */
 public class ArryDemo {

      public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

        System.out.print("序列的長度:");
        int n = input.nextInt();
        System.out.print("序列:");
        int[] arr1 = new int[n + 1];

        for (int i = 1; i <= n; i++) {  //遍歷陣列各元素
            arr1[i] = input.nextInt();

        /*檢查遍歷序列
        System.out.print(arr[i]);

         */
        }
        System.out.print("查詢的次數:");

        int m = input.nextInt();
        int[] arr2 = new int[n + 1];
        int temp; //臨時變數
        boolean flag = false; // 標識變數,表示是否進行過交換


        for (int i = 1; i <= m; i++) {
            System.out.print("\n選擇序列分別為:");

            for (int j = 1; j <= n; j++) {
                arr2[j] = arr1[j];
            }

            int l = input.nextInt();  //序列首
            int r = input.nextInt();  //序列位
            int k = input.nextInt();   // 第K大的數

            //氣泡排序,時間複雜度O(N²)
            for (int a = 1; a <= r; a++) {
                for (int b = a; b <= r; b++) {
                    if (arr2[b] > arr2[a]) {
                        flag = true;
                        temp = arr2[b];
                        arr2[b] = arr2[a];
                        arr2[a] = temp;
                    }

                }

                if (!flag) {  //如果沒有交換,就退出
                    break;
                } else {
                    flag = false; //重置flag,進行下次交換。
                }




            }

            System.out.print("第" + k + "大的數為:" + arr2[1 + k - 1]);
        }



    }
}

'''

4、執行結果

第二題:二叉樹的先、中、後 序遍歷與層級遍歷

1、基本思想

1.前序遍歷:先輸出父節點,在遍歷左子樹和右子樹
2.中序遍歷:先遍歷左子樹,在輸出父節點,再遍歷右子樹
3.後續遍歷:先遍歷左子樹,再遍歷右子樹,最後輸出父節點

2、程式碼實現

'''java

  package com.company;

  import java.util.LinkedList;

   public class Tree {
    public static void main(String[] args) {
        Node root = into();
        // 先序遍歷
        System.out.print(" \n前序遍歷:");
        A(root);
        // 中序遍歷
        System.out.print(" \n中序遍歷:");
        B(root);
        // 後續遍歷
        System.out.print("\n 後續遍歷:");
        C(root);
        // 層級遍歷
        System.out.print(" \n層級遍歷:");
        D(root);

    }

    private static void A(Node root) {
        // TODO 先序遍歷
        if(root == null) return;
        System.out.print(root.data + "\t");
        A(root.l);
        A(root.r);
    }
    private static void B(Node root) {
        // TODO 中序遍歷
        if(root == null) return;
        B(root.l);
        System.out.print(root.data + "\t");
        B(root.r);
    }
    private static void C(Node root) {
        // TODO 後續遍歷
        if(root == null) return;
        C(root.l);
        C(root.r);
        System.out.print(root.data + "\t");
    }

    private static void D(Node root) {
        // TODO 層級遍歷
        if(root == null) return;
        LinkedList<Node> linkedList = new LinkedList<>();
        Node current = null;
        //將根節點入隊
        linkedList.offer(root);
        while (! linkedList.isEmpty()){
            //出隊隊頭元素並訪問
            current = linkedList.poll();
            System.out.print(current.data + "\t");
            //如果當前節點的左節點不為空入隊
            if (current.l != null) {
                linkedList.offer(current.l);
            }
            //如果當前節點的右節點不為空,把右節點入隊
            if (current.r != null){
                linkedList.offer(current.r);
            }
        }
    }

    // 構建一顆樹,返回根節點
    private static Node into(){
        Node root = new Node("A");
        Node node1 = new Node("T");
        Node node2 = new Node("D");
        Node node3 = new Node("N");
        Node node4 = new Node("B");
        Node node5 = new Node("6");
        Node node6 = new Node("5");
        Node node7 = new Node("4");
        Node node8 = new Node("9");
        Node node9 = new Node("1");
        root.l = node1;
        node1.l = node2;
        node2.l = node3;
        node2.r = node6;
        node3.r = node7;
        node7.r = node8;
        node6.l = node9;
        node3.l = node4;
        root.r = node5;
        return root;
    }

    // 節點
    static class Node{
        // 資料
        Object data;
        // 左孩子
        Node l;
        // 右孩子
        Node r;

        public Node(){}

        public Node(Object data) {
            this.data = data;
            this.l = null;
            this.r = null;
        }

        public Node(Object data, Node l, Node r) {
            this.data = data;
            this.l = l;
            this.r = r;
        }
    }
}

'''

3、執行結果