TimSort 在 JDK10 中的实现
Timsort 是一种稳定高效的排序算法,混合了合并排序和二分插入排序,旨在很好地处理多种真实数据。它由Tim Peters于2002年实施使用在Python编程语言中。该算法查找已经排序的数据的子序列,并使用该知识更有效地对其余部分进行排序。从版本2.3开始,Timsort一直是Python的标准排序算法。如今,Timsort 已是是 Python、 Java、 Android平台 和 GNU Octave 的默认排序算法。
大致思路是:因为二分插入排序处理短序列的效率比较高,所以就把待排序的长序列切分成多个短序列,每个短序列独立执行二分插入排序,最后再对排好序的短序列做归并排序。在归并排序时,通过检测序列中已经排好序的片段,巧妙地把这些片段直接作为排序结果,跳过归并操作,从而减少了归并排序时的迭代次数。
复杂度比较(借用网上的图,原图地址):
jdk/lib/src.zip/java.base/java/util/TimSort.java
package java.util;/** * A stable, adaptive, iterative mergesort tha ...
叶秀山版《西方哲学史》笔记之第二卷:古希腊罗马哲学
绪论
古希腊罗马哲学,始于公元前6世纪初的早期希腊哲学,终于公元529年,东罗马帝国皇帝查士丁尼关闭雅典的柏拉图学园标志着古希腊罗马哲学的终结。
西欧古典文明,是古希腊罗马哲学赖以发展的社会和文化基础,也是西方文化的一大源泉。西欧古典文明发端于公元前2000年的克里特文明,崛起于古埃及文明和两河流域文明之后,终结于公元476年西罗马帝国灭亡。西欧古典文明划分为爱琴文明、希腊古典文明、希腊化文明和罗马文明这四个阶段。
西欧古典文明最早起源于爱琴海地区,在爱琴文明中,各部落历经动荡和曲折,终于形成了后来作为希腊哲学主体的希腊民族,发展出城邦奴隶制文明,也产生了萌发哲学思想的希腊神话,所以爱琴文明是希腊民族与希腊文化的摇篮。
希腊古典文明时期,始于公元前6世纪初,终于公元前4世纪30年代,是西欧古典文明奠立根基和全面鼎盛的时代,也是希腊哲学产生并发展至全盛的时代。雅典民主制的确立使其称为希腊古典文明的中心,希波战争的胜利也使得早期希腊哲学向希腊本土转移而趋于兴盛,所以早期希腊哲学是科学启蒙与民主精神的产物。而后在伯利克里时期,民主政治的革新提供了较大的科学文化自由,希腊古典哲学与文化空前繁荣 ...
tinyhttpd 源码阅读
httpd.c/* J. David's webserver *//* This is a simple webserver. * Created November 1999 by J. David Blackstone. * CSE 4344 (Network concepts), Prof. Zeigler * University of Texas at Arlington *//* This program compiles for Sparc Solaris 2.6. * To compile for Linux: * 1) Comment out the #include <pthread.h> line. * 2) Comment out the line that defines the variable newthread. * 3) Comment out the two lines that run pthread_create(). * 4) Uncomment the line that runs accept_request(). * ...
redis源码阅读(更新中)
1 概述
redis版本:2.2.15
看的很老的版本,因为代码少 v^^7
阅读顺序参考自博文 https://blog.csdn.net/terence1212/article/details/53541908
2 数据结构相关2.1 内存分配config.h#ifndef __CONFIG_H#define __CONFIG_H#ifdef __APPLE__#include <AvailabilityMacros.h>#endif/* Use tcmalloc's malloc_size() when available. * When tcmalloc is used, native OSX malloc_size() may never be used because * this expects a different allocation scheme. Therefore, *exclusively* use * either tcmalloc or OSX's malloc_size()! *///如果系统中存在Google的TC_MALLOC库,re ...
Haskell Programming From First Principles notes (更新中)
1 All You Need is Lambda
A calculus is a method of calculation or reasoning. The lambda calculus is one process for formalizing a method. Lambda calculus is your foundation, because Haskell is a lambda calculus.
The essence of Functional Programming(FP) is that programs are a combination of expressions. Expressions include concrete values, variables and functions. In FP, functions are first-class: they can be used as values or passed as arguments, or inputs, to yet more functions.
FP languages a ...
leetcode (更新中)
1 两数之和class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)-1): for j in range(i+1,len(nums)): if nums[i]+nums[j]==target: return [i,j]
2 两数相加class Solution: def addTwoNumbers(self, l1, l2): a="" b="" while True: a+=str(l1.val) if l1.next: l1=l1.next else: break while True: b+=str(l2.val) ...
区块链技术与应用笔记
1 比特币1.1 密码学原理
加密货币(crypto-currency)用到的密码学技术是哈希和签名。
加密货币用到的哈希函数叫做加密哈希函数(cryptographic hash function), 它有三个特性:
collision resistance:抗碰撞性,即输入空间足够大时,给定一个 $x$ ,没有一种比穷举更高效的方法使我们找到一个 $y\neq x$ 满足 $hash(x)=hash(y)$ 。有此性质可以防篡改,因为几乎不可能人为篡改信息后仍保持哈希值不变。抗碰撞性是不可证明的,只能通过经验大概判断。
hiding:藏匿性,即输入空间足够大时,给定一个哈希值 $hash(x)$ ,没有一种比穷举更高效的方法使我们反推出对应的 $x$ 。结合抗碰撞性与藏匿性可以实现数字承诺(digital commitment),即 seal a value in an envelope and open the envelope later。
puzzle friendly:任何人无法得知哈希函数的映射规则,无法根据给定的哈希值要求构造输入。这是比特币对哈希函数的要求,比特币是一 ...
美语连读规则
1 linking consonant to vowel
when a word ends with a consonant and its next word starts with a vowel, let this consonant be the start of the second word.
“hold on” pronounced as “hol don”(/hoʊl dɑːn/)
“like it” pronounced as “li kit”(/laɪ kɪt/), “e” is swallowed
“deep end” pronounced as “dee pend”(/di pend/)
“get up late” pronounced as “ge tu plate”(/ɡe tʌ pleɪt/)
“picked out” pronounced as “pick dout”(/pɪk taʊt/), “ed” pronounced as “/t/“, “e” is swallowed
“this guy” pronounced as “thi sguy”(/ð ...
English with Lucy note(更新中)
160106 make or do
“make” usually means to create sth. didn’t exist before. “do” usually means to complete tasks that already exist.
make:coffee, tea, offer, suggestion, promise, mistake, complaint, sound, noise, discovery, money, profit, loss, investment, bet, fortune
do:homework, test, experiment, interview, course, shopping, dishes, ironing, well, badly, good, bad, sb’s best/worst
exceptions:do business, make my bed(整理床铺), do drawing, do hair(梳头), do make-up(化妆)
160119 -ed pronunciation
/id/: ...
美语弱读规则
“your/you’re” weakened into “yer”(/jɪr/, sounds like “year”)
“yours” weakened into “yers”(/jɪrs/)
“for” weakened into “fer”(/fər/)
“of” weakened into “a”(/ə/)
“you” weakened into “ya”(/jə/)
“-ing” weakened into “-in”, usually used in verb’s continuous form, $\color{red}{informal}$
“what do you/what are you” weakened into “whaddaya”(/wɑːdəjə/)
“what are you” weakened into “whacha”(/wɑːtʃə/), $\color{red}{informal}$ than “whaddaya”
“what do we/they” weakened into “whadda we/they”
“want to” weaken ...