JAVA

算法4习题 2.1.12

HuGuoDong
算法4习题 2.1.12 问题 Instrument shellsort to print the number of compares divided by the array size for each increment. Write a test client that tests the hypothesis that this number is a small constant, by sorting arrays of random Double values, using array sizes that are increasing powers of 10, starting at 100. 打印希尔排序中每个增量带来的比较次数和数组大小的比值。 验证该值是一个小常数。 思路 统计每个增量对应的比较次数。 private static boolean less(Comparable v, Comparable w) { count ++ ; return v.compareTo(w) < 0; } 解答 Extensive experiments suggest that the average number of compares per increment might be N^(1/5) 大量实验表明每个增量的比较次数约为N的1/5次方。

算法4 chp 1-1

HuGuoDong
1.1.1 7, 200.0000002, true 1.1.2 a. double 1.618 b. double 10.0 c. boolean true d. String “33” 执行Java 采用输入命令的形式: 目录: E:\GDUT\Dropbox\Alg4\algs4\target\classes 执行: java edu.princeton.cs.myalg.u1.Ex_1_1_3 1 1 2 $ab=bc$ $$ab=cd$$ [ x = {-b \pm \sqrt{b^2-4ac} \over 2a} \]

Priority Queues

HuGuoDong

[TOC]

本篇文章主要介绍优先队列(Priority Queue)和嵌套类(nested class)的基本用法。 接下来的文章会介绍在实现例如Greedy Best First Search和AStar搜索算法中使用priority queue。