Skip to content

Commit 8526a99

Browse files
committedNov 9, 2018
more tests
1 parent 53e9b20 commit 8526a99

File tree

6 files changed

+1743
-0
lines changed

6 files changed

+1743
-0
lines changed
 

‎book/chapters/timsort.adoc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
= tim Sort
2+
3+
tim sort is an efficient recursive sorting algorithm that uses "divide and conquer" paradigm to sort faster. It can be implemented in-place so it doesn't require additonal memory.
4+
5+
In practice timsort outperforms efficient sorting algorithms like <<Merge Sort>>. And, of course, It also outperforms simple sorting algorithms like <<Selection Sort>>, <<Insertion Sort>> and <<Bubble Sort>>.
6+
7+
tim sort basically picks a "pivot" element (preferably random) and move all the elements that are smaller than the pivot to the right and the ones that are bigger to the left. It does this recursively until all the array is sorted.
8+
9+
== tim Sort Implementation
10+
11+
tim sort implementation uses the divide-and-conquer in the following way:
12+
13+
.tim Sort Algorithm
14+
. Pick a "pivot" element (at random)
15+
. Move everything that is lower than the pivot to the left and everything that is bigger than the pivot to the right.
16+
. Recursively repeat step 1 and 2, the sub-arrays on the left and on the right WITHOUT including the pivot.
17+
18+
Let's convert these words into code!
19+
20+
.tim Sort implementation in JavaScript (timSort)
21+
[source, javascript]
22+
----
23+
include::{codedir}/algorithms/sorting/tim-sort.js[tag=timSort, indent=0]
24+
----
25+
<1> Partition: picks a pivot and find the index where the pivot will be when the array is sorted.
26+
<2> Do the partition of the sub-array at the left of the pivot.
27+
<3> Do the partition of the sub-array at the right of the pivot.
28+
<4> Only do the partition when there's something to divide.
29+
30+
The real heavy-lifting is don in the partion function. Let's implement that:
31+
32+
.tim Sort implementation in JavaScript (partition)
33+
[source, javascript]
34+
----
35+
include::{codedir}/algorithms/sorting/tim-sort.js[tag=partition, indent=0]
36+
----
37+
<1> Make the rightmost element as the pivot.
38+
<2> This is the place holder for the final pivot index. We start in low and as we move all the lower elements to the left we will get the final place where the pivot should be.
39+
<3> Move one element at a time comparing it to the pivot value.
40+
<4> If the current element value is less than the pivot, then increment pivot index (pivot should be place after all the lower values). We also swap the value before incrementing because current element that is lower than the pivot to be at its left side.
41+
42+
Merge sort has a _O(n log n)_ running time. For more details about the how to extract the runtime go to <<Linearithmic>>.
43+
44+
== tim Sort Properties
45+
46+
- Time Complexity: [big]#✅# <<Linearithmic>> _O(n log n)_
47+
- Space Complexity: [big]#⚠️# <<Linear>> _O(n)_
48+
- <<Stable>>: [big]#✅# Yes
49+
- <<In-place>>: [big]#✅# Yes
50+
- <<Adaptive>>: [big]#️️️️️️️⛔️️️️️# No, mostly sorted array takes the same time O(n log n).
51+
- <<Online>>: [big]#️️️️️️️⛔️️️️️# No, the pivot element can be choose at random.
52+
- Recursive: Yes
53+
54+
55+
// Resources:
56+
// https://twitter.com/mathias/status/1036626116654637057?lang=en
57+
// http://cr.openjdk.java.net/~martin/webrevs/openjdk7/timsort/raw_files/new/src/share/classes/java/util/TimSort.java

‎book/images/quicksort.gif

90.8 KB
Loading

‎lab/timsort.java

Lines changed: 928 additions & 0 deletions
Large diffs are not rendered by default.

‎lab/timsort.py

Lines changed: 679 additions & 0 deletions
Large diffs are not rendered by default.

‎src/algorithms/sorting/quick-sort.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const { swap } = require('./sorting-common');
2+
3+
// tag::partition[]
4+
/**
5+
* Linear-time Partitioning
6+
* Chooses a pivot and re-arrage the array that
7+
* everything on the left is <= pivot and
8+
* everything on the right is > pivot
9+
*
10+
* Runtime: O(n)
11+
* @param {*} array
12+
* @param {*} low start index
13+
* @param {*} high end index
14+
* @returns {integer} pivot index
15+
*/
16+
function partition(array, low, high) {
17+
const pivotInitialIndex = high; // <1>
18+
let pivotIndex = low; // <2>
19+
20+
for (let current = low; current < high; current += 1) { // <3>
21+
if (array[current] <= array[pivotInitialIndex]) { // <4>
22+
swap(array, current, pivotIndex);
23+
pivotIndex += 1;
24+
}
25+
}
26+
27+
swap(array, pivotInitialIndex, pivotIndex);
28+
return pivotIndex;
29+
}
30+
// end::partition[]
31+
32+
33+
// tag::quickSort[]
34+
/**
35+
* QuickSort - Efficient in-place recursive sorting algorithm.
36+
* Avg. Runtime: O(n log n) | Worst: O(n^2)
37+
* @param {*} array
38+
* @param {*} low
39+
* @param {*} high
40+
*/
41+
function quickSort(array, low = 0, high = array.length - 1) {
42+
if (low < high) { // <4>
43+
const partitionIndex = partition(array, low, high); // <1>
44+
quickSort(array, low, partitionIndex - 1); // <2>
45+
quickSort(array, partitionIndex + 1, high); // <3>
46+
}
47+
return array;
48+
}
49+
// end::quickSort[]
50+
51+
52+
// tag::sort[]
53+
/**
54+
* Quick sort
55+
* Runtime: O(n log n)
56+
* @param {Array|Set} collection elements to be sorted
57+
*/
58+
function quickSortWrapper(collection) {
59+
const array = Array.from(collection); // <1>
60+
return quickSort(array);
61+
}
62+
// end::sort[]
63+
64+
65+
module.exports = quickSortWrapper;

‎todo

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
3+
Compare content with:
4+
- [] https://adrianmejia.com/blog/2018/04/28/data-structures-time-complexity-for-beginners-arrays-hashmaps-linked-lists-stacks-queues-tutorial/#Stacks
5+
- [] https://medium.freecodecamp.org/time-is-complex-but-priceless-f0abd015063c
6+
- [] https://leetcode.com/explore/learn/
7+
- [] https://github.com/trekhleb/javascript-algorithms
8+
- [] https://www.khanacademy.org/computing/computer-science/algorithms
9+
- [] Compare with: Data Structures and Algorithms.pdf by Lydia Hallie
10+
- [] Cracking code interviews
11+
- [] Grokking Algorithms
12+
- [] CS Distilled
13+
- [] Create poster like: http://bigocheatsheet.com/, http://bigoref.com/,
14+
- [] https://introcs.cs.princeton.edu/java/11cheatsheet/

0 commit comments

Comments
 (0)
Please sign in to comment.