diff --git a/.gitignore b/.gitignore index f6cbc5f..0c676a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # Node rules: ## Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt - +## ## Dependency directory ## Commenting this out is preferred by some people, see ## https://docs.npmjs.com/misc/faq#should-i-check-my-node_modules-folder-into-git diff --git a/1.bubbleSort.md b/1.bubbleSort.md index 78c3b83..9b1385a 100644 --- a/1.bubbleSort.md +++ b/1.bubbleSort.md @@ -1,45 +1,45 @@ -# 冒泡排序 +# Bubble Sort -冒泡排序(Bubble Sort)也是一种简单直观的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。 +Bubble Sort is also a simple and intuitive sorting algorithm. It repeatedly walks through the sequence to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. The work of visiting the sequence is repeated until no more exchanges are needed, that is, the sequence has been sorted. The name of this algorithm comes from the fact that the smaller elements are slowly "floated" to the top of the sequence by swapping. -作为最简单的排序算法之一,冒泡排序给我的感觉就像 Abandon 在单词书里出现的感觉一样,每次都在第一页第一位,所以最熟悉。冒泡排序还有一种优化算法,就是立一个 flag,当在一趟序列遍历中元素没有发生交换,则证明该序列已经有序。但这种改进对于提升性能来说并没有什么太大作用。 +As one of the simplest sorting algorithms, bubble sort gives me the same feeling that Abandon appears in a word book, every time it is first on the first page, so it is the most familiar. There is also an optimization algorithm for bubble sort, which is to set up a flag. When the elements are not exchanged during a sequence traversal, it is proved that the sequence is already in order. But this improvement doesn't do much to improve performance. -## 1. 算法步骤 +## 1. Algorithm steps -1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。 +1. Compare adjacent elements. If the first is bigger than the second, swap the two of them. -2. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。 +2. Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. After this step, the last element will be the largest number. -3. 针对所有的元素重复以上的步骤,除了最后一个。 +3. Repeat the above steps for all elements except the last one. -4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。 +4. Continue to repeat the above steps for fewer and fewer elements each time, until there are no pairs of numbers to compare. -## 2. 动图演示 +## 2. Animated demo -![动图演示](res/bubbleSort.gif) +![Animation demo](res/bubbleSort.gif) -## 3. 什么时候最快 +## 3. When is the fastest -当输入的数据已经是正序时(都已经是正序了,我还要你冒泡排序有何用啊)。 +When the input data is already in positive order (it is already in positive order, what is the use of bubble sort I want you to do). -## 4. 什么时候最慢 +## 4. When is the slowest -当输入的数据是反序时(写一个 for 循环反序输出数据不就行了,干嘛要用你冒泡排序呢,我是闲的吗)。 +When the input data is in reverse order (write a for loop to output data in reverse order, why use your bubble sort, am I free). -## 5. JavaScript 代码实现 +## 5. JavaScript code implementation -```js +````js function bubbleSort(arr) { var len = arr.length; for (var i = 0; i < len - 1; i++) { for (var j = 0; j < len - 1 - i; j++) { - if (arr[j] > arr[j+1]) { // 相邻元素两两对比 - var temp = arr[j+1]; // 元素交换 + if (arr[j] > arr[j+1]) { // pairwise comparison of adjacent elements + var temp = arr[j+1]; // element swap arr[j+1] = arr[j]; arr[j] = temp; } @@ -47,49 +47,49 @@ function bubbleSort(arr) { } return arr; } -``` +```` -## 6. Python 代码实现 +## 6. Python code implementation -```python +````python def bubbleSort(arr): for i in range(1, len(arr)): for j in range(0, len(arr)-i): if arr[j] > arr[j+1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr -``` +```` -## 7. Go 代码实现 +## 7. Go code implementation -```go +````go func bubbleSort(arr []int) []int { - length := len(arr) - for i := 0; i < length; i++ { - for j := 0; j < length-1-i; j++ { - if arr[j] > arr[j+1] { - arr[j], arr[j+1] = arr[j+1], arr[j] - } - } - } - return arr +length := len(arr) +for i := 0; i < length; i++ { +for j := 0; j < length-1-i; j++ { +if arr[j] > arr[j+1] { +arr[j], arr[j+1] = arr[j+1], arr[j] } -``` +} +} +return arr +} +```` -## 8. Java 代码实现 +## 8. Java code implementation -```java +````java public class BubbleSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { - // 对 arr 进行拷贝,不改变参数内容 + // Copy arr without changing the parameter content int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); for (int i = 1; i < arr.length; i++) { - // 设定一个标记,若为true,则表示此次循环没有进行交换,也就是待排序列已经有序,排序已经完成。 + // Set a flag, if it is true, it means that there is no exchange in this loop, that is, the sequence to be sorted is already in order, and the sorting has been completed. boolean flag = true; for (int j = 0; j < arr.length - i; j++) { @@ -111,7 +111,7 @@ public class BubbleSort implements IArraySort { } ``` -## 9. PHP 代码实现 +## 9. PHP Code ```php function bubbleSort($arr) diff --git a/10.radixSort.md b/10.radixSort.md index 2da94b8..02aaaf5 100644 --- a/10.radixSort.md +++ b/10.radixSort.md @@ -1,25 +1,25 @@ -# 基数排序 +# radix sort -基数排序是一种非比较型整数排序算法,其原理是将整数按位数切割成不同的数字,然后按每个位数分别比较。由于整数也可以表达字符串(比如名字或日期)和特定格式的浮点数,所以基数排序也不是只能使用于整数。 +Radix sort is a non-comparative integer sorting algorithm. Since integers can also express strings (such as names or dates) and floating-point numbers in a specific format, radix sort is not limited to integers. -## 1. 基数排序 vs 计数排序 vs 桶排序 +## 1. Radix Sort vs Count Sort vs Bucket Sort -基数排序有两种方法: +There are two methods for radix sort: -这三种排序算法都利用了桶的概念,但对桶的使用方法上有明显差异案例看大家发的: +These three sorting algorithms all use the concept of buckets, but there are obvious differences in the use of buckets. - - 基数排序:根据键值的每位数字来分配桶; - - 计数排序:每个桶只存储单一键值; - - 桶排序:每个桶存储一定范围的数值; + - Radix sort: allocate buckets according to each digit of the key value; + - Counting sort: each bucket stores only a single key value; + - Bucket sorting: each bucket stores a certain range of values; -## 2. LSD 基数排序动图演示 +## 2. LSD radix sort animation demonstration -![动图演示](res/radixSort.gif) +![Animation demo](res/radixSort.gif) -## 3. JavaScript 代码实现 +## 3. JavaScript Code ```js //LSD Radix Sort @@ -50,7 +50,7 @@ function radixSort(arr, maxDigit) { ``` -## 4. python 代码实现 +## 4. python Code ```python def radix(arr): @@ -58,14 +58,14 @@ def radix(arr): digit = 0 max_digit = 1 max_value = max(arr) - #找出列表中最大的位数 + #find the largest number of digits in the list while 10**max_digit < max_value: max_digit = max_digit + 1 while digit < max_digit: temp = [[] for i in range(10)] for i in arr: - #求出每一个元素的个、十、百位的值 + # Find the value of the units, tens, and hundreds of each element t = int((i/10**digit)%10) temp[t].append(i) @@ -81,18 +81,18 @@ def radix(arr): ``` -## 5. Java 代码实现 +## 5. Java code implementation -```java +````java /** - * 基数排序 - * 考虑负数的情况还可以参考: https://code.i-harness.com/zh-CN/q/e98fa9 + * Radix sort + * You can also refer to the case of considering negative numbers: https://code.i-harness.com/zh-CN/q/e98fa9 */ public class RadixSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { - // 对 arr 进行拷贝,不改变参数内容 + // Copy arr without changing the parameter content int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); int maxDigit = getMaxDigit(arr); @@ -100,7 +100,7 @@ public class RadixSort implements IArraySort { } /** - * 获取最高位数 + * Get the highest digit */ private int getMaxDigit(int[] arr) { int maxValue = getMaxValue(arr); @@ -133,7 +133,7 @@ public class RadixSort implements IArraySort { int dev = 1; for (int i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) { - // 考虑负数的情况,这里扩展一倍队列数,其中 [0-9]对应负数,[10-19]对应正数 (bucket + 10) + // Consider the case of negative numbers, double the number of queues here, where [0-9] corresponds to negative numbers, and [10-19] corresponds to positive numbers (bucket + 10) int[][] counter = new int[mod * 2][0]; for (int j = 0; j < arr.length; j++) { @@ -153,7 +153,7 @@ public class RadixSort implements IArraySort { } /** - * 自动扩容,并保存数据 + * Automatically expand and save data * * @param arr * @param value @@ -164,9 +164,9 @@ public class RadixSort implements IArraySort { return arr; } } -``` +```` -## 6. PHP 代码实现 +## 6. PHP code implementation ```php function radixSort($arr, $maxDigit = null) diff --git a/2.selectionSort.md b/2.selectionSort.md index 103830e..036a1da 100644 --- a/2.selectionSort.md +++ b/2.selectionSort.md @@ -1,23 +1,23 @@ -# 选择排序 +# Selection sort -选择排序是一种简单直观的排序算法,无论什么数据进去都是 O(n²) 的时间复杂度。所以用到它的时候,数据规模越小越好。唯一的好处可能就是不占用额外的内存空间了吧。 +Selection sort is a simple and intuitive sorting algorithm, and no matter what data goes in, it takes O(n²) time complexity. So when using it, the smaller the data size, the better. The only benefit may be that it doesn't take up extra memory space. -## 1. 算法步骤 +## 1. Algorithm steps -1. 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置 +1. First find the smallest (largest) element in the unsorted sequence and store it at the beginning of the sorted sequence -2. 再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。 +2. Continue to find the smallest (largest) element from the remaining unsorted elements and place it at the end of the sorted sequence. -3. 重复第二步,直到所有元素均排序完毕。 +3. Repeat step 2 until all elements are sorted. -## 2. 动图演示 +## 2. Animated demo -![动图演示](res/selectionSort.gif) +![Animation demo](res/selectionSort.gif) -## 3. JavaScript 代码实现 +## 3. JavaScript code implementation ```js function selectionSort(arr) { @@ -26,8 +26,8 @@ function selectionSort(arr) { for (var i = 0; i < len - 1; i++) { minIndex = i; for (var j = i + 1; j < len; j++) { - if (arr[j] < arr[minIndex]) { // 寻找最小的数 - minIndex = j; // 将最小数的索引保存 + if (arr[j] < arr[minIndex]) { // find the smallest number + minIndex = j; // save the index of the smallest number } } temp = arr[i]; @@ -36,25 +36,25 @@ function selectionSort(arr) { } return arr; } -``` +```` -## 4. Python 代码实现 +## 4. Python code implementation -```python +````python def selectionSort(arr): for i in range(len(arr) - 1): - # 记录最小数的索引 + # record the index of the smallest number minIndex = i for j in range(i + 1, len(arr)): if arr[j] < arr[minIndex]: minIndex = j - # i 不是最小数时,将 i 和最小数进行交换 + # When i is not the smallest number, swap i and the smallest number if i != minIndex: arr[i], arr[minIndex] = arr[minIndex], arr[i] return arr -``` +```` -## 5. Go 代码实现 +## 5. Go code implementation ```go func selectionSort(arr []int) []int { @@ -72,28 +72,28 @@ func selectionSort(arr []int) []int { } ``` -## 6. Java 代码实现 +## 6. Java code implementation -```java +````java public class SelectionSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); - // 总共要经过 N-1 轮比较 + // A total of N-1 rounds of comparisons are required for (int i = 0; i < arr.length - 1; i++) { int min = i; - // 每轮需要比较的次数 N-i + // The number of times to compare each round N-i for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[min]) { - // 记录目前能找到的最小值元素的下标 + // Record the index of the smallest element that can be found so far min = j; } } - // 将找到的最小值和i位置所在的值进行交换 + // Swap the min value found with the value at position i if (i != min) { int tmp = arr[i]; arr[i] = arr[min]; @@ -104,9 +104,9 @@ public class SelectionSort implements IArraySort { return arr; } } -``` +```` -## 7. PHP 代码实现 +## 7. PHP code implementation ```php function selectionSort($arr) diff --git a/3.insertionSort.md b/3.insertionSort.md index c828cb3..f6855c3 100644 --- a/3.insertionSort.md +++ b/3.insertionSort.md @@ -1,23 +1,23 @@ -# 插入排序 +# Insertion sort -插入排序的代码实现虽然没有冒泡排序和选择排序那么简单粗暴,但它的原理应该是最容易理解的了,因为只要打过扑克牌的人都应该能够秒懂。插入排序是一种最简单直观的排序算法,它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。 +Although the code implementation of insertion sort is not as simple and rude as bubble sort and selection sort, its principle should be the easiest to understand, because anyone who has played poker should be able to understand it in seconds. Insertion sort is one of the simplest and most intuitive sorting algorithms. Its working principle is to construct an ordered sequence. For unsorted data, scan from back to front in the sorted sequence, find the corresponding position and insert it. -插入排序和冒泡排序一样,也有一种优化算法,叫做拆半插入。 +Insertion sort, like bubble sort, also has an optimization algorithm called split-half insertion. -## 1. 算法步骤 +## 1. Algorithm steps -1. 将第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列。 +1. Treat the first element of the first to-be-sorted sequence as an ordered sequence, and treat the second to last element as an unsorted sequence. -2. 从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置。(如果待插入的元素与有序序列中的某个元素相等,则将待插入元素插入到相等元素的后面。) +2. Scan the unsorted sequence from the beginning to the end, and insert each element scanned into the appropriate position of the sorted sequence. (If the element to be inserted is equal to an element in the ordered sequence, the element to be inserted is inserted after the equal element.) -## 2. 动图演示 +## 2. Animated demo -![动图演示](res/insertionSort.gif) +![Animation demo](res/insertionSort.gif) -## 3. JavaScript 代码实现 +## 3. JavaScript code implementation ```js function insertionSort(arr) { @@ -36,7 +36,7 @@ function insertionSort(arr) { } ``` -## 4. Python 代码实现 +## 4. Python Code ```python def insertionSort(arr): @@ -50,7 +50,7 @@ def insertionSort(arr):    return arr ``` -## 5. Go 代码实现 +## 5. Go Code ```go func insertionSort(arr []int) []int { for i := range arr { @@ -66,30 +66,30 @@ func insertionSort(arr []int) []int { } ``` -## 6. Java 代码实现 +## 6. Java code implementation -```java +````java public class InsertSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { - // 对 arr 进行拷贝,不改变参数内容 + // Copy arr without changing the parameter content int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); - // 从下标为1的元素开始选择合适的位置插入,因为下标为0的只有一个元素,默认是有序的 + // Select the appropriate position to insert from the element with subscript 1, because there is only one element with subscript 0, the default is ordered for (int i = 1; i < arr.length; i++) { - // 记录要插入的数据 + // record the data to be inserted int tmp = arr[i]; - // 从已经排序的序列最右边的开始比较,找到比其小的数 + // Compare from the rightmost of the sorted sequence and find the number smaller than it int j = i; while (j > 0 && tmp < arr[j - 1]) { arr[j] = arr[j - 1]; j--; } - // 存在比其小的数,插入 + // there is a smaller number, insert if (j != i) { arr[j] = tmp; } @@ -98,9 +98,9 @@ public class InsertSort implements IArraySort { return arr; } } -``` +```` -## 7. PHP 代码实现 +## 7. PHP code implementation ```php function insertionSort($arr) diff --git a/4.shellSort.md b/4.shellSort.md index e6d5f9c..c067d18 100644 --- a/4.shellSort.md +++ b/4.shellSort.md @@ -1,32 +1,32 @@ -# 希尔排序 +# Hill sort -希尔排序,也称递减增量排序算法,是插入排序的一种更高效的改进版本。但希尔排序是非稳定排序算法。 +Hill sort, also known as the decreasing-increment sorting algorithm, is a more efficient and improved version of insertion sort. But Hill sort is a non-stable sorting algorithm. -希尔排序是基于插入排序的以下两点性质而提出改进方法的: +Hill sort is an improved method based on the following two properties of insertion sort: - - 插入排序在对几乎已经排好序的数据操作时,效率高,即可以达到线性排序的效率; - - 但插入排序一般来说是低效的,因为插入排序每次只能将数据移动一位; + - Insertion sort has high efficiency when operating on almost sorted data, that is, it can achieve the efficiency of linear sorting; + - But insertion sort is generally inefficient, because insertion sort can only move data one bit at a time; -希尔排序的基本思想是:先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录“基本有序”时,再对全体记录进行依次直接插入排序。 +The basic idea of ​​Hill sorting is to first divide the entire sequence of records to be sorted into several sub-sequences for direct insertion sorting, and when the records in the entire sequence are "basically ordered", perform direct insertion sorting on all records in turn. -## 1. 算法步骤 +## 1. Algorithm steps -1. 选择一个增量序列 t1,t2,……,tk,其中 ti > tj, tk = 1; +1. Choose an incremental sequence t1, t2, ..., tk, where ti > tj, tk = 1; -2. 按增量序列个数 k,对序列进行 k 趟排序; +2. According to the number of incremental sequences k, sort the sequence k times; -3. 每趟排序,根据对应的增量 ti,将待排序列分割成若干长度为 m 的子序列,分别对各子表进行直接插入排序。仅增量因子为 1 时,整个序列作为一个表来处理,表长度即为整个序列的长度。 +3. For each sorting, according to the corresponding increment ti, divide the sequence to be sorted into several subsequences of length m, and perform direct insertion sorting on each subtable. Only when the increment factor is 1, the entire sequence is treated as a table, and the table length is the length of the entire sequence. -## 2. JavaScript 代码实现 +## 2. JavaScript code implementation ```js function shellSort(arr) { var len = arr.length, temp, gap = 1; - while(gap < len/3) { //动态定义间隔序列 + while(gap < len/3) { //dynamically define interval sequence/dynamically define interval sequence gap =gap*3+1; } for (gap; gap > 0; gap = Math.floor(gap/3)) { @@ -42,7 +42,7 @@ function shellSort(arr) { } ``` -## 3. Python 代码实现 +## 3. Python code implementation ```python def shellSort(arr): @@ -63,7 +63,7 @@ def shellSort(arr): } ``` -## 4. Go 代码实现 +## 4. Go code implementation ```go func shellSort(arr []int) []int { @@ -88,14 +88,14 @@ func shellSort(arr []int) []int { } ``` -## 5. Java 代码实现 +## 5. Java code implementation ```java public class ShellSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { - // 对 arr 进行拷贝,不改变参数内容 + // Copy arr without changing the parameter content int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); int gap = 1; @@ -121,7 +121,7 @@ public class ShellSort implements IArraySort { } ``` -## 6. PHP 代码实现 +## 6. PHP code implementation ```php function shellSort($arr) diff --git a/5.mergeSort.md b/5.mergeSort.md index c030d8f..ce25bd6 100644 --- a/5.mergeSort.md +++ b/5.mergeSort.md @@ -1,45 +1,45 @@ -# 归并排序 +# merge sort -归并排序(Merge sort)是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。 +Merge sort (Merge sort) is an efficient sorting algorithm based on the merge operation. This algorithm is a very typical application of Divide and Conquer. -作为一种典型的分而治之思想的算法应用,归并排序的实现由两种方法: - - 自上而下的递归(所有递归的方法都可以用迭代重写,所以就有了第 2 种方法); - - 自下而上的迭代; +As a typical algorithm application of the idea of ​​divide and conquer, the implementation of merge sort consists of two methods: + - Top-down recursion (all recursive methods can be rewritten with iteration, so there is a second method); + - bottom-up iteration; -在《数据结构与算法 JavaScript 描述》中,作者给出了自下而上的迭代方法。但是对于递归法,作者却认为: +In "Description of Data Structures and Algorithms in JavaScript", the author gives a bottom-up iterative approach. But for the recursive method, the author thinks: > However, it is not possible to do so in JavaScript, as the recursion goes too deep for the language to handle. > -> 然而,在 JavaScript 中这种方式不太可行,因为这个算法的递归深度对它来讲太深了。 +> However, this is not feasible in JavaScript because the recursion depth of this algorithm is too deep for it. -说实话,我不太理解这句话。意思是 JavaScript 编译器内存太小,递归太深容易造成内存溢出吗?还望有大神能够指教。 +To be honest, I don't quite understand this sentence. Does it mean that the memory of the JavaScript compiler is too small, and the recursion is too deep to cause memory overflow? I hope there is a god who can teach me. -和选择排序一样,归并排序的性能不受输入数据的影响,但表现比选择排序好的多,因为始终都是 O(nlogn) 的时间复杂度。代价是需要额外的内存空间。 +Like selection sort, the performance of merge sort is not affected by the input data, but it performs much better than selection sort because it is always O(nlogn) time complexity. The tradeoff is that additional memory space is required. -## 2. 算法步骤 +## 1. Algorithm steps -1. 申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列; +1. Apply for space so that its size is the sum of the two sorted sequences, and this space is used to store the merged sequence; -2. 设定两个指针,最初位置分别为两个已经排序序列的起始位置; +2. Set two pointers, the initial positions are the starting positions of the two sorted sequences; -3. 比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置; +3. Compare the elements pointed to by the two pointers, select a relatively small element to put into the merge space, and move the pointer to the next position; -4. 重复步骤 3 直到某一指针达到序列尾; +4. Repeat step 3 until a pointer reaches the end of the sequence; -5. 将另一序列剩下的所有元素直接复制到合并序列尾。 +5. Copy all remaining elements of the other sequence directly to the end of the merged sequence. -## 3. 动图演示 +## 2. Animated demo -![动图演示](res/mergeSort.gif) +![Animation demo](res/mergeSort.gif) -## 4. JavaScript 代码实现 +## 3. JavaScript code implementation ```js -function mergeSort(arr) { // 采用自上而下的递归方法 +function mergeSort(arr) { // use top-down recursion var len = arr.length; if(len < 2) { return arr; @@ -72,7 +72,8 @@ function merge(left, right) } ``` -## 5. Python 代码实现 + +## 4. Python code implementation ```python def mergeSort(arr): @@ -97,7 +98,7 @@ def merge(left,right): return result ``` -## 6. Go 代码实现 +## 5. Go code implementation ```go func mergeSort(arr []int) []int { @@ -137,14 +138,14 @@ func merge(left []int, right []int) []int { } ``` -## 7. Java 代码实现 +## 6. Java code implementation ```java public class MergeSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { - // 对 arr 进行拷贝,不改变参数内容 + // Copy arr without changing the parameter content int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); if (arr.length < 2) { @@ -187,7 +188,7 @@ public class MergeSort implements IArraySort { } ``` -## 8. PHP 代码实现 +## 7. PHP code implementation ```php function mergeSort($arr) @@ -224,7 +225,7 @@ function merge($left, $right) } ``` -## 9. C++ 代码实现 +## 8. C++ code implementation ```cpp void merge(vector& arr, int l, int mid, int r) { diff --git a/6.quickSort.md b/6.quickSort.md index 28ad477..dbfe8ce 100644 --- a/6.quickSort.md +++ b/6.quickSort.md @@ -1,33 +1,33 @@ -# 快速排序 +# quicksort -快速排序是由东尼·霍尔所发展的一种排序算法。在平均状况下,排序 n 个项目要 Ο(nlogn) 次比较。在最坏状况下则需要 Ο(n2) 次比较,但这种状况并不常见。事实上,快速排序通常明显比其他 Ο(nlogn) 算法更快,因为它的内部循环(inner loop)可以在大部分的架构上很有效率地被实现出来。 +Quicksort is a sorting algorithm developed by Tony Hall. On average, sorting n items requires Ο(nlogn) comparisons. In the worst case, Ο(n2) comparisons are required, but this is not common. In fact, quicksort is usually significantly faster than other Ο(nlogn) algorithms because its inner loop can be implemented efficiently on most architectures. -快速排序使用分治法(Divide and conquer)策略来把一个串行(list)分为两个子串行(sub-lists)。 +Quicksort uses a divide and conquer strategy to divide a list into two sub-lists. -快速排序又是一种分而治之思想在排序算法上的典型应用。本质上来看,快速排序应该算是在冒泡排序基础上的递归分治法。 +Quick sort is another typical application of divide and conquer idea in sorting algorithm. In essence, quick sort should be regarded as a recursive divide and conquer method based on bubble sort. -快速排序的名字起的是简单粗暴,因为一听到这个名字你就知道它存在的意义,就是快,而且效率高!它是处理大数据最快的排序算法之一了。虽然 Worst Case 的时间复杂度达到了 O(n²),但是人家就是优秀,在大多数情况下都比平均时间复杂度为 O(n logn) 的排序算法表现要更好,可是这是为什么呢,我也不知道。好在我的强迫症又犯了,查了 N 多资料终于在《算法艺术与信息学竞赛》上找到了满意的答案: +The name of Quick Sort is simple and rude, because as soon as you hear the name, you know the meaning of its existence, that is, it is fast and efficient! It is one of the fastest sorting algorithms for big data. Although the time complexity of Worst Case reaches O(n²), it is excellent. In most cases, it performs better than the sorting algorithm with an average time complexity of O(n logn), but this is why, I do not know either. Fortunately, my obsessive-compulsive disorder was committed again. After checking N many materials, I finally found a satisfactory answer on the "Algorithm Art and Informatics Competition": -> 快速排序的最坏运行情况是 O(n²),比如说顺序数列的快排。但它的平摊期望时间是 O(nlogn),且 O(nlogn) 记号中隐含的常数因子很小,比复杂度稳定等于 O(nlogn) 的归并排序要小很多。所以,对绝大多数顺序性较弱的随机数列而言,快速排序总是优于归并排序。 +> The worst-case operation of quicksort is O(n²), such as quicksort of sequential arrays. But its amortized expected time is O(nlogn), and the constant factor implicit in the O(nlogn) notation is small, much smaller than merge sort whose complexity is stable equal to O(nlogn). Therefore, for most random number sequences with weak order, quick sort is always better than merge sort. -## 1. 算法步骤 +## 1. Algorithm steps -1. 从数列中挑出一个元素,称为 “基准”(pivot); +1. Pick out an element from the sequence, called a "pivot"; -2. 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作; +2. Reorder the sequence. All elements smaller than the reference value are placed in front of the reference, and all elements larger than the reference value are placed at the back of the reference (same numbers can go to either side). After the partition exits, the benchmark is in the middle of the sequence. This is called a partition operation; -3. 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序; +3. Recursively sort the subarrays of elements smaller than the reference value and the subarrays of elements larger than the reference value; -递归的最底部情形,是数列的大小是零或一,也就是永远都已经被排序好了。虽然一直递归下去,但是这个算法总会退出,因为在每次的迭代(iteration)中,它至少会把一个元素摆到它最后的位置去。 +The bottom case of recursion is that the size of the sequence is zero or one, which is always sorted. Although it keeps recursing, the algorithm will always exit, because in each iteration, it will put at least one element to its last position. -## 2. 动图演示 +## 2. Animated demo -![动图演示](res/quickSort.gif) +![Animation demo](res/quickSort.gif) -## 3. JavaScript 代码实现 +## 3. JavaScript code implementation ```js function quickSort(arr, left, right) { @@ -44,8 +44,8 @@ function quickSort(arr, left, right) { return arr; } -function partition(arr, left ,right) { // 分区操作 - var pivot = left, // 设定基准值(pivot) +function partition(arr, left ,right) { // partition operation + var pivot = left, // set the pivot value (pivot) index = pivot + 1; for (var i = index; i <= right; i++) { if (arr[i] < arr[pivot]) { @@ -90,7 +90,7 @@ function quickSort2(arr, low, high) { ``` -## 4. Python 代码实现 +## 4. Python code implementation ```python def quickSort(arr, left=None, right=None): @@ -118,7 +118,7 @@ def swap(arr, i, j): arr[i], arr[j] = arr[j], arr[i] ``` -## 5. Go 代码实现 +## 5. Go code implementation ```go func quickSort(arr []int) []int { @@ -153,11 +153,11 @@ func swap(arr []int, i, j int) { } ``` -## 6. C++版 +## 6. C++ version -```C++ - //严蔚敏《数据结构》标准分割函数 +````C++ + //Yan Weimin's "Data Structure" standard segmentation function Paritition1(int A[], int low, int high) { int pivot = A[low]; while (low < high) { @@ -174,7 +174,7 @@ func swap(arr []int, i, j int) { return low; } - void QuickSort(int A[], int low, int high) //快排母函数 + void QuickSort(int A[], int low, int high) //Quick sort mother function { if (low < high) { int pivot = Paritition1(A, low, high); @@ -184,14 +184,14 @@ func swap(arr []int, i, j int) { } ``` -## 7. Java 代码实现 +## 7. Java code implementation ```java public class QuickSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { - // 对 arr 进行拷贝,不改变参数内容 + // Copy arr without changing the parameter content int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); return quickSort(arr, 0, arr.length - 1); @@ -207,7 +207,7 @@ public class QuickSort implements IArraySort { } private int partition(int[] arr, int left, int right) { - // 设定基准值(pivot) + // Set the reference value(pivot) int pivot = left; int index = pivot + 1; for (int i = index; i <= right; i++) { @@ -229,7 +229,7 @@ public class QuickSort implements IArraySort { } ``` -## 8. PHP 代码实现 +## 8. PHP code implementation ```php function quickSort($arr) diff --git a/7.heapSort.md b/7.heapSort.md index 394d929..777026f 100644 --- a/7.heapSort.md +++ b/7.heapSort.md @@ -1,42 +1,42 @@ -# 堆排序 +# heap sort -堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法。堆积是一个近似完全二叉树的结构,并同时满足堆积的性质:即子结点的键值或索引总是小于(或者大于)它的父节点。堆排序可以说是一种利用堆的概念来排序的选择排序。分为两种方法: +Heapsort (Heapsort) refers to a sorting algorithm designed using the data structure of the heap. Stacking is a structure that approximates a complete binary tree, and satisfies the property of stacking at the same time: that is, the key value or index of a child node is always less than (or greater than) its parent node. Heap sort can be said to be a selection sort that uses the concept of heap to sort. There are two methods: -1. 大顶堆:每个节点的值都大于或等于其子节点的值,在堆排序算法中用于升序排列; -2. 小顶堆:每个节点的值都小于或等于其子节点的值,在堆排序算法中用于降序排列; +1. Big top heap: The value of each node is greater than or equal to the value of its child nodes, which is used for ascending order in the heap sorting algorithm; +2. Small top heap: the value of each node is less than or equal to the value of its child nodes, which is used for descending order in the heap sorting algorithm; -堆排序的平均时间复杂度为 Ο(nlogn)。 +The average time complexity of heapsort is Ο(nlogn). -## 1. 算法步骤 +## 1. Algorithm steps -1. 将待排序序列构建成一个堆 H[0……n-1],根据(升序降序需求)选择大顶堆或小顶堆; +1. Build the sequence to be sorted into a heap H[0...n-1], and select a large top heap or a small top heap according to (ascending and descending order requirements); -2. 把堆首(最大值)和堆尾互换; +2. Swap the heap head (maximum value) and the heap tail; -3. 把堆的尺寸缩小 1,并调用 shift_down(0),目的是把新的数组顶端数据调整到相应位置; +3. Reduce the size of the heap by 1 and call shift_down(0) to adjust the top data of the new array to the corresponding position; -4. 重复步骤 2,直到堆的尺寸为 1。 +4. Repeat step 2 until the size of the heap is 1. -## 2. 动图演示 +## 2. Animated demo -![动图演示](res/heapSort.gif) +![Animation demo](res/heapSort.gif) -## 3. JavaScript 代码实现 +## 3. JavaScript code implementation -```js -var len; // 因为声明的多个函数都需要数据长度,所以把len设置成为全局变量 +````js +var len; // Because multiple functions declared require data length, set len ​​as a global variable -function buildMaxHeap(arr) { // 建立大顶堆 +function buildMaxHeap(arr) { // Build a big top heap len = arr.length; for (var i = Math.floor(len/2); i >= 0; i--) { heapify(arr, i); } } -function heapify(arr, i) { // 堆调整 +function heapify(arr, i) { // heap adjustment var left = 2 * i + 1, right = 2 * i + 2, largest = i; @@ -72,7 +72,7 @@ function heapSort(arr) { return arr; } ``` -## 4. Python 代码实现 +## 4. Python code implementation ```python def buildMaxHeap(arr): @@ -107,7 +107,7 @@ def heapSort(arr):    return arr ``` -## 5. Go 代码实现 +## 5. Go code implementation ```go func heapSort(arr []int) []int { @@ -148,14 +148,14 @@ func swap(arr []int, i, j int) { } ``` -## 6. Java 代码实现 +## 6. Java code implementation ```java public class HeapSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { - // 对 arr 进行拷贝,不改变参数内容 + // Copy arr without changing the parameter content int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); int len = arr.length; @@ -204,7 +204,7 @@ public class HeapSort implements IArraySort { } ``` -## 7. PHP 代码实现 +## 7. PHP code implementation ```php function buildMaxHeap(&$arr) diff --git a/8.countingSort.md b/8.countingSort.md index d0930cf..7c96ccc 100644 --- a/8.countingSort.md +++ b/8.countingSort.md @@ -1,15 +1,15 @@ -# 计数排序 +# Count sort -计数排序的核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。 +The core of counting sorting is to convert the input data values ​​into keys and store them in the additionally opened array space. As a sort of linear time complexity, counting sort requires that the input data must be an integer with a certain range. -## 1. 动图演示 +## 1. Animated demo -![动图演示](res/countingSort.gif) +![Animation demo](res/countingSort.gif) -## 2. JavaScript 代码实现 +## 2. JavaScript code implementation -```js +````js function countingSort(arr, maxValue) { var bucket = new Array(maxValue+1), sortedIndex = 0; @@ -32,12 +32,12 @@ function countingSort(arr, maxValue) { return arr; } -``` +```` -## 3. Python 代码实现 +## 3. Python code implementation -```python +````python def countingSort(arr, maxValue): bucketLen = maxValue+1 bucket = [0]*bucketLen @@ -53,42 +53,41 @@ def countingSort(arr, maxValue): sortedIndex+=1 bucket[j]-=1 return arr -``` +```` -## 4. Go 代码实现 +## 4. Go code implementation -```go +````go func countingSort(arr []int, maxValue int) []int { - bucketLen := maxValue + 1 - bucket := make([]int, bucketLen) // 初始为0的数组 +bucketLen := maxValue + 1 +bucket := make([]int, bucketLen) // array with initial 0 - sortedIndex := 0 - length := len(arr) +sortedIndex := 0 +length := len(arr) - for i := 0; i < length; i++ { - bucket[arr[i]] += 1 - } - - for j := 0; j < bucketLen; j++ { - for bucket[j] > 0 { - arr[sortedIndex] = j - sortedIndex += 1 - bucket[j] -= 1 - } - } +for i := 0; i < length; i++ { +bucket[arr[i]] += 1 +} - return arr +for j := 0; j < bucketLen; j++ { +for bucket[j] > 0 { +arr[sortedIndex] = j +sortedIndex += 1 +bucket[j] -= 1 +} } -``` -## 5. Java 代码实现 +return arr +} +```` +## 5. Java Code ```java public class CountingSort implements IArraySort { @Override public int[] sort(int[] sourceArray) throws Exception { - // 对 arr 进行拷贝,不改变参数内容 + // Copy arr without changing the parameter content int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); int maxValue = getMaxValue(arr); @@ -127,7 +126,7 @@ public class CountingSort implements IArraySort { } ``` -## 6. PHP 代码实现 +## 6. PHP Code ```php function countingSort($arr, $maxValue = null) @@ -158,4 +157,4 @@ function countingSort($arr, $maxValue = null) return $arr; } -``` \ No newline at end of file +``` diff --git a/9.bucketSort.md b/9.bucketSort.md index bd76a63..7c964e6 100644 --- a/9.bucketSort.md +++ b/9.bucketSort.md @@ -1,25 +1,24 @@ -# 桶排序 +# Bucket sort -桶排序是计数排序的升级版。它利用了函数的映射关系,高效与否的关键就在于这个映射函数的确定。为了使桶排序更加高效,我们需要做到这两点: +Bucket sort is an upgraded version of count sort. It uses the mapping relationship of functions, and the key to efficiency lies in the determination of this mapping function. To make bucket sort more efficient, we need to do two things: -1. 在额外空间充足的情况下,尽量增大桶的数量 -2. 使用的映射函数能够将输入的 N 个数据均匀的分配到 K 个桶中 +1. In the case of sufficient extra space, try to increase the number of buckets +2. The mapping function used can evenly distribute the input N data into K buckets -同时,对于桶中元素的排序,选择何种比较排序算法对于性能的影响至关重要。 +At the same time, for the sorting of elements in the bucket, the choice of the comparison sorting algorithm is critical to the performance. -## 1. 什么时候最快 +## 1. When is the fastest -当输入的数据可以均匀的分配到每一个桶中。 +When the input data can be evenly distributed to each bucket. -## 2. 什么时候最慢 +## 2. When is the slowest -当输入的数据被分配到了同一个桶中。 +When the input data is allocated to the same bucket. -## 3. JavaScript 代码实现 - +## 3. JavaScript code implementation ```js function bucketSort(arr, bucketSize) { if (arr.length === 0) { @@ -31,14 +30,14 @@ function bucketSort(arr, bucketSize) { var maxValue = arr[0]; for (i = 1; i < arr.length; i++) { if (arr[i] < minValue) { - minValue = arr[i]; // 输入数据的最小值 + minValue = arr[i]; // minimum value of input data } else if (arr[i] > maxValue) { - maxValue = arr[i]; // 输入数据的最大值 + maxValue = arr[i]; // maximum value of input data } } - //桶的初始化 - var DEFAULT_BUCKET_SIZE = 5; // 设置桶的默认数量为5 + //Initialize the bucket + var DEFAULT_BUCKET_SIZE = 5; // Set the default number of buckets to 5 bucketSize = bucketSize || DEFAULT_BUCKET_SIZE; var bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1; var buckets = new Array(bucketCount); @@ -46,33 +45,33 @@ function bucketSort(arr, bucketSize) { buckets[i] = []; } - //利用映射函数将数据分配到各个桶中 +//Using the mapping function to allocate data to each bucket for (i = 0; i < arr.length; i++) { buckets[Math.floor((arr[i] - minValue) / bucketSize)].push(arr[i]); } arr.length = 0; for (i = 0; i < buckets.length; i++) { - insertionSort(buckets[i]); // 对每个桶进行排序,这里使用了插入排序 + insertionSort(buckets[i]); // Sort each bucket, insertion sort is used here for (var j = 0; j < buckets[i].length; j++) { - arr.push(buckets[i][j]); + arr.push(buckets[i][j]); } } return arr; } -``` +```` -## 4. Java 代码实现 +## 4. Java code implementation -```java +````java public class BucketSort implements IArraySort { private static final InsertSort insertSort = new InsertSort(); @Override public int[] sort(int[] sourceArray) throws Exception { - // 对 arr 进行拷贝,不改变参数内容 +// Copy arr without changing the parameter content int[] arr = Arrays.copyOf(sourceArray, sourceArray.length); return bucketSort(arr, 5); @@ -96,7 +95,7 @@ public class BucketSort implements IArraySort { int bucketCount = (int) Math.floor((maxValue - minValue) / bucketSize) + 1; int[][] buckets = new int[bucketCount][0]; - // 利用映射函数将数据分配到各个桶中 + // Use the mapping function to allocate data to each bucket for (int i = 0; i < arr.length; i++) { int index = (int) Math.floor((arr[i] - minValue) / bucketSize); buckets[index] = arrAppend(buckets[index], arr[i]); @@ -107,7 +106,7 @@ public class BucketSort implements IArraySort { if (bucket.length <= 0) { continue; } - // 对每个桶进行排序,这里使用了插入排序 + // Sort each bucket, insertion sort is used here bucket = insertSort.sort(bucket); for (int value : bucket) { arr[arrIndex++] = value; @@ -118,7 +117,7 @@ public class BucketSort implements IArraySort { } /** - * 自动扩容,并保存数据 + * Automatically expand and save data * * @param arr * @param value @@ -132,7 +131,7 @@ public class BucketSort implements IArraySort { } ``` -## 5. PHP 代码实现 +## 5. PHP Code ```php function bucketSort($arr, $bucketSize = 5) @@ -172,4 +171,4 @@ function bucketSort($arr, $bucketSize = 5) return $arr; } -``` \ No newline at end of file +``` diff --git a/README.md b/README.md index 4c36daa..80cb04a 100644 --- a/README.md +++ b/README.md @@ -1,67 +1,66 @@ -# 十大经典排序算法 +# Top 10 Classic Sorting Algorithms -[![Build Status](https://travis-ci.org/hustcc/JS-Sorting-Algorithm.svg?branch=master)](https://travis-ci.org/hustcc/JS-Sorting-Algorithm) -排序算法是《数据结构与算法》中最基本的算法之一。 +The sorting algorithm is one of the most basic algorithms in Data Structures and Algorithms. -排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存。常见的内部排序算法有:**插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序**等。用一张图概括: +Sorting algorithms can be divided into internal sorting and external sorting. Internal sorting is the sorting of data records in memory, while external sorting is because the data to be sorted is large and cannot accommodate all sorting records at one time, and external memory needs to be accessed during the sorting process. Common internal sorting algorithms are: **insertion sort, Hill sort, selection sort, bubble sort, merge sort, quick sort, heap sort, radix sort** and so on. Summarize with a picture: -![十大经典排序算法 概览截图](res/sort.png) +![Top Ten Classic Sorting Algorithms Overview Screenshot](res/read.png.png) -**关于时间复杂度**: +**About time complexity**: -1. 平方阶 (O(n2)) 排序 - 各类简单排序:直接插入、直接选择和冒泡排序。 -2. 线性对数阶 (O(nlog2n)) 排序 - 快速排序、堆排序和归并排序; -3. O(n1+§)) 排序,§ 是介于 0 和 1 之间的常数。 - 希尔排序 -4. 线性阶 (O(n)) 排序 - 基数排序,此外还有桶、箱排序。 +1. Square order (O(n2)) sorting +Various simple sorts: direct insertion, direct selection, and bubble sort. +2. Linear logarithmic (O(nlog2n)) sorting +Quick sort, heap sort and merge sort; +3. O(n1+§)) ordering, where § is a constant between 0 and 1. + Hill sort +4. Linear order (O(n)) sorting +Radix sort, in addition to bucket and bin sorting. -**关于稳定性**: +**About stability**: -稳定的排序算法:冒泡排序、插入排序、归并排序和基数排序。 +Stable sorting algorithms: bubble sort, insertion sort, merge sort, and radix sort. -不是稳定的排序算法:选择排序、快速排序、希尔排序、堆排序。 +Not a stable sorting algorithm: selection sort, quick sort, hill sort, heap sort. -**名词解释**: +**Glossary**: -**n**:数据规模 +**n**: data size -**k**:“桶”的个数 +**k**: the number of "buckets" -**In-place**:占用常数内存,不占用额外内存 +**In-place**: occupies constant memory, does not occupy additional memory -**Out-place**:占用额外内存 +**Out-place**: takes up extra memory -**稳定性**:排序后 2 个相等键值的顺序和排序之前它们的顺序相同 +**Stability**: 2 equal key values ​​are in the same order after sorting as they were before sorting ---- -**GitBook 内容大纲** +**GitBook Content Outline** -1. [冒泡排序](1.bubbleSort.md) -2. [选择排序](2.selectionSort.md) -3. [插入排序](3.insertionSort.md) -4. [希尔排序](4.shellSort.md) -5. [归并排序](5.mergeSort.md) -6. [快速排序](6.quickSort.md) -7. [堆排序](7.heapSort.md) -8. [计数排序](8.countingSort.md) -9. [桶排序](9.bucketSort.md) -10. [基数排序](10.radixSort.md) +1. [Bubble Sort](1.bubbleSort.md) +2. [SelectionSort](2.selectionSort.md) +3. [Insertion Sort](3.insertionSort.md) +4. [Hill Sort](4.shellSort.md) +5. [Merge Sort](5.mergeSort.md) +6. [Quick Sort](6.quickSort.md) +7. [Heap Sort](7.heapSort.md) +8. [Counting Sort](8.countingSort.md) +9. [Bucket Sort](9.bucketSort.md) +10. [Radix Sort](10.radixSort.md) ---- -本书内容几乎完全来源于网络。 +The content of this book comes almost entirely from the Internet. -开源项目地址:[https://github.com/hustcc/JS-Sorting-Algorithm](https://github.com/hustcc/JS-Sorting-Algorithm),整理人 [hustcc](https://github.com/hustcc)。 +Open source project address: [https://github.com/hustcc/JS-Sorting-Algorithm](https://github.com/hustcc/JS-Sorting-Algorithm), organizer [hustcc](https://github .com/hustcc). -GitBook 在线阅读地址:[https://sort.hust.cc/](https://sort.hust.cc/)。 +GitBook online reading address: [https://sort.hust.cc/](https://sort.hust.cc/). -本项目使用 [lint-md](https://github.com/hustcc/lint-md) 进行中文 Markdown 文件的格式检查,务必在提交 Pr 之前,保证 Markdown 格式正确。 +This project uses [lint-md](https://github.com/hustcc/lint-md) to check the format of Chinese Markdown files. Make sure that the Markdown format is correct before submitting Pr. diff --git a/res/read.png.png b/res/read.png.png new file mode 100644 index 0000000..77c1b67 Binary files /dev/null and b/res/read.png.png differ diff --git a/res/sort.png b/res/sort.png deleted file mode 100644 index 5d44280..0000000 Binary files a/res/sort.png and /dev/null differ