Java常用算法手册-01算法概述

来源:互联网 发布:局部全局优化算法 编辑:程序博客网 时间:2024/06/08 16:12

Java常用算法手册——01算法概述

1.1  算法的特征

1. 有穷性2. 确切性3. 输入4. 输出5. 可行性

1.2  算法与数据结构的关系

数据结果+算法=程序

1.3  算法的性能评价

一个算法优劣往往通过算法复杂度来衡量,算法复杂度包括时间复杂度和空间复杂度两个方面。1. 时间复杂度    时间复杂度就是算法执行消耗的时间,时间越短,算法越好。一个代码的执行时间只有在  实际计算机中运行才知道,但我们也可以对程序代码进行估计,每条语句执行都需要时间,因  此,简短、精悍的代码往往执行速度越快。       2. 空间复杂度    空间复杂度指的是算法程序在计算机中执行消耗的存储空间。可分为以下两方面:    1. 程序保存所需要的的存储空间,也就是程序的大小。    2. 程序在执行过程中所需要消耗的存储空间资源,例如程序在执行过程中的中间变量等。    一般来说,程序的大小越小,执行过程中消耗的资源越少,这个程序就越好。

1.4  算法的性能评价

1.算法实例-在数组中查找数字。package chapter01;import java.util.Random;import java.util.Scanner;/** * Created by Hoictas on 2017/8/24. */public class Demo01 {    static int MaxLength = 20;    public static void main(String[] args) {        boolean mark = true;        int[] ints = new int[MaxLength];        Random random = new Random();        for (int i = 0; i < ints.length; i++) {            ints[i] = random.nextInt(100);//[0,100)随机生成0到99        }        System.out.print("随机生成的数据序列:\n");        for (int i = 0; i < ints.length; i++) {            System.out.print(ints[i] + "、");        }        System.out.print("\n");        Scanner scanner = new Scanner(System.in);        while (mark) {            boolean flag = false;            int tag = -1;            int nextInt = -1;            System.out.println("输入要查找的数");            nextInt = scanner.nextInt();            if (nextInt < 0 || nextInt >= 100) {                System.out.println("输入数不在范围内,请重新输入");                continue;            } else {                for (int i = 0; i < ints.length; i++) {                    if (nextInt == ints[i]) {                        flag = true;                        tag = i;                        break;                    }                }                if (flag) {                    System.out.println("找到了数据:" + ints[tag] + "是数组中第" + tag + "个元素");                } else {                    System.out.println("您输入的数据不存在!");                }            }            System.out.println("是否还要继续查询?0:不是 1:是");            int nextInt2 = scanner.nextInt();            if (nextInt2 == 0) {                mark = false;            }        }        System.out.println("查询结束!");    }}

更新时间: 2017/8/24 20:00:14 学习笔记chapter01