算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-002如何改进算法
2016-04-20 16:41
696 查看
1.
The answer to this question is that we have discussed and used two classic algorithms,
mergesort and binary search, have introduced the facts that the mergesort is linearith-
mic and binary search is logarithmic.
2.
3.
4.
package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /****************************************************************************** * Compilation: javac TwoSum.java * Execution: java TwoSum input.txt * Dependencies: StdOut.java In.java Stopwatch.java * Data files: http://algs4.cs.princeton.edu/14analysis/1Kints.txt * http://algs4.cs.princeton.edu/14analysis/2Kints.txt * http://algs4.cs.princeton.edu/14analysis/4Kints.txt * http://algs4.cs.princeton.edu/14analysis/8Kints.txt * http://algs4.cs.princeton.edu/14analysis/16Kints.txt * http://algs4.cs.princeton.edu/14analysis/32Kints.txt * http://algs4.cs.princeton.edu/14analysis/1Mints.txt * * A program with N^2 running time. Read in N integers * and counts the number of pairs that sum to exactly 0. * * * Limitations * ----------- * - we ignore integer overflow * * * % java TwoSum 2Kints.txt * 2 * * % java TwoSum 1Kints.txt * 1 * * % java TwoSum 2Kints.txt * 2 * * % java TwoSum 4Kints.txt * 3 * * % java TwoSum 8Kints.txt * 19 * * % java TwoSum 16Kints.txt * 66 * * % java TwoSum 32Kints.txt * 273 * ******************************************************************************/ public class TwoSum { // print distinct pairs (i, j) such that a[i] + a[j] = 0 public static void printAll(int[] a) { int N = a.length; for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j++) { if (a[i] + a[j] == 0) { StdOut.println(a[i] + " " + a[j]); } } } } // return number of distinct triples (i, j) such that a[i] + a[j] = 0 public static int count(int[] a) { int N = a.length; int cnt = 0; for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j++) { if (a[i] + a[j] == 0) { cnt++; } } } return cnt; } public static void main(String[] args) { In in = new In(args[0]); int[] a = in.readAllInts(); Stopwatch timer = new Stopwatch(); int cnt = count(a); StdOut.println("elapsed time = " + timer.elapsedTime()); StdOut.println(cnt); } }
The answer to this question is that we have discussed and used two classic algorithms,
mergesort and binary search, have introduced the facts that the mergesort is linearith-
mic and binary search is logarithmic.
2.
package algorithms.analysis14; /****************************************************************************** * Compilation: javac TwoSumFast.java * Execution: java TwoSumFast input.txt * Dependencies: In.java Stopwatch.java * Data files: http://algs4.cs.princeton.edu/14analysis/1Kints.txt * http://algs4.cs.princeton.edu/14analysis/2Kints.txt * http://algs4.cs.princeton.edu/14analysis/4Kints.txt * http://algs4.cs.princeton.edu/14analysis/8Kints.txt * http://algs4.cs.princeton.edu/14analysis/16Kints.txt * http://algs4.cs.princeton.edu/14analysis/32Kints.txt * http://algs4.cs.princeton.edu/14analysis/1Mints.txt * * A program with N log N running time. Read in N integers * and counts the number of pairs that sum to exactly 0. * * Limitations * ----------- * - we ignore integer overflow * * * % java TwoSumFast 2Kints.txt * 2 * * % java TwoSumFast 1Kints.txt * 1 * * % java TwoSumFast 2Kints.txt * 2 * * % java TwoSumFast 4Kints.txt * 3 * * % java TwoSumFast 8Kints.txt * 19 * * % java TwoSumFast 16Kints.txt * 66 * * % java TwoSumFast 32Kints.txt * 273 * ******************************************************************************/ import java.util.Arrays; import algorithms.util.In; import algorithms.util.StdOut; public class TwoSumFast { // print distinct pairs (i, j) such that a[i] + a[j] = 0 public static void printAll(int[] a) { int N = a.length; Arrays.sort(a); for (int i = 0; i < N; i++) { int j = Arrays.binarySearch(a, -a[i]); if (j > i) StdOut.println(a[i] + " " + a[j]); } } // return number of distinct pairs (i, j) such that a[i] + a[j] = 0 public static int count(int[] a) { int N = a.length; Arrays.sort(a); int cnt = 0; for (int i = 0; i < N; i++) { int j = Arrays.binarySearch(a, -a[i]); if (j > i) cnt++; } return cnt; } public static void main(String[] args) { In in = new In(args[0]); int[] a = in.readAllInts(); int cnt = count(a); StdOut.println(cnt); } }
3.
package algorithms.analysis14; import algorithms.util.In; import algorithms.util.StdOut; /****************************************************************************** * Compilation: javac ThreeSum.java * Execution: java ThreeSum input.txt * Dependencies: In.java StdOut.java Stopwatch.java * Data files: http://algs4.cs.princeton.edu/14analysis/1Kints.txt * http://algs4.cs.princeton.edu/14analysis/2Kints.txt * http://algs4.cs.princeton.edu/14analysis/4Kints.txt * http://algs4.cs.princeton.edu/14analysis/8Kints.txt * http://algs4.cs.princeton.edu/14analysis/16Kints.txt * http://algs4.cs.princeton.edu/14analysis/32Kints.txt * http://algs4.cs.princeton.edu/14analysis/1Mints.txt * * A program with cubic running time. Read in N integers * and counts the number of triples that sum to exactly 0 * (ignoring integer overflow). * * % java ThreeSum 1Kints.txt * 70 * * % java ThreeSum 2Kints.txt * 528 * * % java ThreeSum 4Kints.txt * 4039 * ******************************************************************************/ /** * The <tt>ThreeSum</tt> class provides static methods for counting * and printing the number of triples in an array of integers that sum to 0 * (ignoring integer overflow). * <p> * This implementation uses a triply nested loop and takes proportional to N^3, * where N is the number of integers. * <p> * For additional documentation, see <a href="http://algs4.cs.princeton.edu/14analysis">Section 1.4</a> of * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. * * @author Robert Sedgewick * @author Kevin Wayne */ public class ThreeSum { // Do not instantiate. private ThreeSum() { } /** * Prints to standard output the (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0. * @param a the array of integers */ public static void printAll(int[] a) { int N = a.length; for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j++) { for (int k = j+1; k < N; k++) { if (a[i] + a[j] + a[k] == 0) { StdOut.println(a[i] + " " + a[j] + " " + a[k]); } } } } } /** * Returns the number of triples (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0. * @param a the array of integers * @return the number of triples (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0 */ public static int count(int[] a) { int N = a.length; int cnt = 0; for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j++) { for (int k = j+1; k < N; k++) { if (a[i] + a[j] + a[k] == 0) { cnt++; } } } } return cnt; } /** * Reads in a sequence of integers from a file, specified as a command-line argument; * counts the number of triples sum to exactly zero; prints out the time to perform * the computation. */ public static void main(String[] args) { In in = new In(args[0]); int[] a = in.readAllInts(); Stopwatch timer = new Stopwatch(); int cnt = count(a); StdOut.println("elapsed time = " + timer.elapsedTime()); StdOut.println(cnt); } }
4.
package algorithms.analysis14; /****************************************************************************** * Compilation: javac ThreeSumFast.java * Execution: java ThreeSumFast input.txt * Dependencies: StdOut.java In.java Stopwatch.java * Data files: http://algs4.cs.princeton.edu/14analysis/1Kints.txt * http://algs4.cs.princeton.edu/14analysis/2Kints.txt * http://algs4.cs.princeton.edu/14analysis/4Kints.txt * http://algs4.cs.princeton.edu/14analysis/8Kints.txt * http://algs4.cs.princeton.edu/14analysis/16Kints.txt * http://algs4.cs.princeton.edu/14analysis/32Kints.txt * http://algs4.cs.princeton.edu/14analysis/1Mints.txt * * A program with N^2 log N running time. Read in N integers * and counts the number of triples that sum to exactly 0. * * Limitations * ----------- * - we ignore integer overflow * - doesn't handle case when input has duplicates * * * % java ThreeSumFast 1Kints.txt * 70 * * % java ThreeSumFast 2Kints.txt * 528 * * % java ThreeSumFast 4Kints.txt * 4039 * * % java ThreeSumFast 8Kints.txt * 32074 * * % java ThreeSumFast 16Kints.txt * 255181 * * % java ThreeSumFast 32Kints.txt * 2052358 * ******************************************************************************/ import java.util.Arrays; import algorithms.util.In; import algorithms.util.StdOut; /** * The <tt>ThreeSumFast</tt> class provides static methods for counting * and printing the number of triples in an array of distinct integers that * sum to 0 (ignoring integer overflow). * <p> * This implementation uses sorting and binary search and takes time * proportional to N^2 log N, where N is the number of integers. * <p> * For additional documentation, see <a href="http://algs4.cs.princeton.edu/14analysis">Section 1.4</a> of * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. * * @author Robert Sedgewick * @author Kevin Wayne */ public class ThreeSumFast { // Do not instantiate. private ThreeSumFast() { } // returns true if the sorted array a[] contains any duplicated integers private static boolean containsDuplicates(int[] a) { for (int i = 1; i < a.length; i++) if (a[i] == a[i-1]) return true; return false; } /** * Prints to standard output the (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0. * @param a the array of integers * @throws IllegalArgumentException if the array contains duplicate integers */ public static void printAll(int[] a) { int N = a.length; Arrays.sort(a); if (containsDuplicates(a)) throw new IllegalArgumentException("array contains duplicate integers"); for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j++) { int k = Arrays.binarySearch(a, -(a[i] + a[j])); if (k > j) StdOut.println(a[i] + " " + a[j] + " " + a[k]); } } } /** * Returns the number of triples (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0. * @param a the array of integers * @return the number of triples (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0 */ public static int count(int[] a) { int N = a.length; Arrays.sort(a); if (containsDuplicates(a)) throw new IllegalArgumentException("array contains duplicate integers"); int cnt = 0; for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j++) { int k = Arrays.binarySearch(a, -(a[i] + a[j])); if (k > j) cnt++; } } return cnt; } /** * Reads in a sequence of distinct integers from a file, specified as a command-line argument; * counts the number of triples sum to exactly zero; prints out the time to perform * the computation. */ public static void main(String[] args) { In in = new In(args[0]); int[] a = in.readAllInts(); int cnt = count(a); StdOut.println(cnt); } }
相关文章推荐
- GOF设计模式阅读笔记之行为模型(上)
- Django 异常解决
- go--Ubuntu下安装GO LANG,测试输入hello world
- 机器学习系列(8)_读《Nature》论文,看AlphaGo养成
- Windows下visual studio code搭建golang开发环境
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-001分析步骤
- Django之站内搜索-Solr,Haystack
- ubuntu14.04 RockMongo的配置
- Django filter符合表示"不等于"
- Django笔记 富文本编辑器 课题
- Django笔记 二级子域名配置
- Django笔记 如何扩展User表的字段
- Django之信息聚合
- 谷歌“安全浏览”检查出Google.com“存在危险”
- 谷歌“安全浏览”检查出Google.com“存在危险”
- Django之sitemap
- Go-Pholcus爬人民网新闻规则
- Go语言解析JSON
- golang入门-- import包与包内init方法的执行时机
- django的session保存策略(时间)