编程珠玑 总结

第一章 开篇

本章主要提了一个问题

  • 问题:一个最多包含 n 个正整数的文件,每个都小于 n ,其中 n = 10^7。如果在输入文件中有任何整数重复出现就是致命错误。没有其他数据与该整数相关联。

  • 方法:

    • 直接想到的方法应该是外部归并排序。
    • 更好的方法是位图法(伪代码如下):
      /*步骤一:初始化一个长度为 n 的数组为 0 */
      for i = [0, n)
          bit[i] = 0
      
      /*步骤二:凡是出现过的数字 i 令 bit[i] 为 1*/
      for each i in the input file
          bit[i] = 1
      
      /*步骤三:有序打印出所有数字*/
      for i = [0,n)
          if bit[i] == 1
              write i on the output file
      
  • 我的思考(对应习题7部分):

    • 在本章中的问题强调了所有数字只出现一次,若对于数字出现的次数没有规定,使用这种方法可以在步骤二中将:
      bit[i] = 1
      
      改为
      ++bit[i]
      
      则输出时的代码为:
      for i = [0, n)
          if bit[i] != 0
              for j = [0, bit[i])
                  write i on the output file   
      
      即某个数字出现几次则将该数字打印几次。
    • 若出现负数了该怎么办?
      • 首先我们可以直接认为出现的负数为错误数据,按数据异常处理。
      • 其次我认为若是认为负数是合理的,则可以再设置一个数组用来对应出现负数的情况,即使用两个数组分别对应正负数处理。
Code from Programming Pearls Column 1: Programs for sorting integers bitsort.c -- Sort with bit vectors. sortints.cpp -- Sort using C++ STL sets. qsortints.c -- Sort with C library qsort. bitsortgen.c -- Generate random integers for sorting. Column 2: Test and time algorithms rotate.c -- Three ways to rotate the elements of a vector. The next two program are used in a pipeline to compute all anagrams in a dictionary sign.c -- Sign each word by its letters in sorted order. squash.c -- Put each anagram class on a single line. Column 5: Scaffolding for testing and timing search functions search.c -- Linear and binary search. Column 7: Tiny experiment on C run times timemod0.c -- Edit main to time one operation. Column 8: Compute the maximum-sum subsequence in an array maxsum.c -- Time four algs: n3, n2, n log n, n. Column 9: Code tuning programs genbins.c -- Profile this, then try a special-purpose allocator. macfun.c -- Time the cost of macros and functions. The column also uses rotate.c (Column 2), search.c (Column 5) and maxsum.c (Column 8). Column 11: Test and time sorting algorithms sort.cpp -- Mostly C, but also C++ sort function. SortAnim.java -- Animate those sort functions in Java. Column 12: Generate a sorted list of random integers sortedrand.cpp -- Several algorithms for the task. Column 13: Set representations for the problem in Column 12 sets.cpp -- Several data structures for sets. genbins.c (Column 9) implements the bin data structure in C. Column 14: Heaps priqueue.cpp -- Implement and test priority queues. The column also uses sort.c (Column 11) for heapsort. Column 15: Strings wordlist.cpp -- List words in the file, using STL set. wordfreq.cpp -- List words in the file, with counts, using STL map. wordfreq.c -- Same as above, with hash table in C. longdup.c -- Find long repeated strings in input. markov.c -- Generate random text from input. markovhash.c -- Like markov.c, but with hashing. markovlet.c -- Letter-level markov text, simple algorithm. Appendix 3: Cost Models spacemod.cpp -- Space used by various records. timemod.c -- Table of times used by various C constructs. You may use this code for any purpose, as long as you leave the copyright notice and book citation attached.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值