数据结构Java版例程练习

昨日下午于图书馆借到一本蔡明志编的数据结构(Java版)。大概翻了下看起来写的还可以,借一本去实验室看吧。

这书第一章用各种废话粗略介绍了下神马事算法,并举了一小堆例子。第二章讲数组。虽然没看懂第二章想要说明什么问题,不过第二章最后的生命游戏倒是很有趣,虽然过去有玩过,但原理并未完全理解。那么今天上午的计划就是把这书的前两章看完,顺便做个笔记神马的……


第一章 算法分析

在这里,算法的定义是解决问题的有限步骤过程,也可以说是在解答过程中的一步步过程。对于简单的问题,很容易看出解决问题的步骤,对于复杂的问题却不然。

性能分析(Performance Analysis)的常用方法有Time Complexity AnalysisTime Complexity Analysis

算法复杂度的分析可以用O,Ω和Θ来确定一个程序的时间复杂度。定义如下:

f(n) = O(g(n)),当且仅当唯一存在正整数c即n0,使得f(n) <= cg(n), 对所有得n, n >=n0

f(n)=Ω(g(n))当且仅当存在正整数c和n0,使得f(n) >= c*g(n),对所有的n,n >= 0

f(n)=Θ(g(n)),当且仅当存在正整数c1,c2和n,使得c1*g(n)<=f(n)<=c2*g(n),对所有的n, n >= 0



哦对了,第一页还有四个例子的说。虽然很简单,还是要把写的代码贴上以作纪念= =

#1

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.firedom.www;

import fire.*;

/**
 *
 * @author firedom
 *
 *
 *
 */
public class OBJECT extends iostr {

    /**
     * @param args the command line arguments
     */


    public static void main(String[] args) {
        int a[][] = new int[12][11];
        for (int i = 0; i < a.length; i++) { //获得行长度
            for (int j = 0; j < a[0].length; j++) { // 获得列长度
                a[i][j] = ((i * 10) + j); // 
                // 以这种方式递增得到的最大的数是120,而不是12*11
                // 每次循环,个位数为零的数会出现两次。
            }
        }
        sum(a);
    }

    public static void sum(int array[][]) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[0].length; j++) {
                output(array[i][j]);
            }
        }
    }
}


    static int count = 0;
    public static void main(String[] args) {
        int a[][] = new int[12][11];
        for (int i = 0; i < a.length; i++) { //获得行长度
            for (int j = 0; j < a[0].length; j++) { // 获得列长度
                a[i][j] = count; // 这样写才ok-
                count++;
            }
        }
        sum(a);
    }

#2 内部矩阵相加 & 查找 & 相乘

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.firedom.www;

import fire.*;

/**
 *
 * @author firedom
 *
 *
 *
 */
class setArray {

    private int a[][] = new int[5][5];
    private int b[][] = new int[5][5];
    private int c[][] = new int[5][5];

    setArray() {
        int count = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[0].length; j++) {
                a[i][j] = count;
                b[i][j] = count;
                count++;
            }
        }
    }

    public int getaryA(int i, int j) {
        return (a[i][j]);
    }

    public int getaryB(int i, int j) {
        return (b[i][j]);
    }

    public void sumAdd() {
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[0].length; j++) {
                c[i][j] = (getaryA(i, j) + getaryB(i, j));
            }
        }
    }

    public int srarchTarget(int a[], int target) { // search the first target,nember
        // start to 0

        for (int i = 0; i < a.length; i++) {
            if (target == a[i]) {
                return (i);
            }
        }
        return (-1);

    }

    public void mult() {
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[0].length; j++) {
                sum = 0;
                for (int k = 0; k < a.length; k++) {
                    sum += (a[i][k] * b[k][j]);
                    c[i][j] = sum;

                }

            }
        }

    }

    public void printMultary() {
        mult();
        for (int i = 0; i < c.length; i++) {
            for (int j = 0; j < c[0].length; j++) {
                System.out.print(c[i][j] + " ");
            }
            System.out.println(" ");
        }
    }

    public void printary() {
        sumAdd();
        for (int i = 0; i < c.length; i++) {
            for (int j = 0; j < c[0].length; j++) {
                System.out.print(c[i][j] + " ");
            }
            System.out.println(" ");
        }
    }
}

public class OBJECT extends iostr {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        setArray a = new setArray();
        a.printary();

        int[] num = {2, 3, 1, 4, 3, 5, 4, 6, 34, 2};
        int target = 5;
        output(a.srarchTarget(num, target));

        System.out.println(" ");
        System.out.println(" ");
        System.out.println(" ");
        System.out.println(" ");
        System.out.println(" ");

        a.printMultary();

    }
}

使用二分法查找已经排序完成的数列的复杂度是O(log2N),函数图像大概像这个样子:

数据结构Java版例程练习_第1张图片

差点把代码忘了,按照二分法的思路写出的代码是这样的:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package execcount;

/**
 *
 * @author firedom
 */
class searchAlog {

    public void binarysearch(int source[], int findNumber) {
        int low = 0;
        int upper = source.length;
        int middle;
        while (low <= upper) {
            middle = (upper + low) / 2;
            if (source[middle] > findNumber) {
                upper = middle - 1;
            } else if (source[middle] < findNumber) {
                low = middle + 1;
            } else {
                System.out.println(middle);
                break;
            }
        }
    }
}

public class Execcount {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int[] data = new int[1000000];
        int count = 0;
        for (int i = 0; i < data.length; i++) {
            data[i] = count;
            count++;
        }
        searchAlog b = new searchAlog();
        b.binarysearch(data, 8475);
    }
}
==看这样子以后还是直接贴关键代码吧,不然的话太占地方了。

饿看样子今天只能到这里了,发烧好难受的说。

你可能感兴趣的:(Java相关)