You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: book/chapters/sorting-intro.adoc
+11-13
Original file line number
Diff line number
Diff line change
@@ -4,10 +4,11 @@ Sorting implementations with the same time complexity might manipulate the data
4
4
5
5
== Stable
6
6
7
-
An stable sorting algorithms keeps the relative order of items with the same comparison criteria.
7
+
An ((stable sorting)) algorithms keeps the relative order of items with the same comparison criteria.
8
8
9
-
This specially useful when you want to sort on multiple phases. Let's say you have the following data:
9
+
This specially useful when you want to sort on multiple phases.
10
10
11
+
.Let's say you have the following data:
11
12
[source, javascript]
12
13
----
13
14
const users = [
@@ -18,8 +19,7 @@ const users = [
18
19
];
19
20
----
20
21
21
-
If you sort by `name` you would have
22
-
22
+
.If you sort by `name` you would have:
23
23
[source, javascript]
24
24
----
25
25
[
@@ -30,10 +30,9 @@ If you sort by `name` you would have
30
30
];
31
31
----
32
32
33
-
Then, here comes the important part, if you sort by `age` you might two different results.
34
-
35
-
If the sorting algorithm is *stable*, it should keep the items with the same age ordered by `name`:
33
+
Then, here comes the important part, if you sort by `age` you might get two different results.
36
34
35
+
.If the sorting algorithm is *stable*, it should keep the items with the same age ordered by `name`:
37
36
[source, javascript]
38
37
----
39
38
[
@@ -44,8 +43,7 @@ If the sorting algorithm is *stable*, it should keep the items with the same age
44
43
];
45
44
----
46
45
47
-
However, if the sorting is *not stable*, then you will lose the relative order of the items and get something like this:
48
-
46
+
.However, if the sorting is *not stable*, then you will lose the relative order of the items and get something like this:
49
47
[source, javascript]
50
48
----
51
49
[
@@ -56,18 +54,18 @@ However, if the sorting is *not stable*, then you will lose the relative order o
56
54
];
57
55
----
58
56
59
-
Both results are correct, however, having a sorting algorithm is better if you want to do multiple sorting passes.
57
+
Both results are correctly sorted by `age`, however, having a stable sorting is better if you want to keep the relative possition of keys with the same value.
60
58
61
59
== In-place
62
60
63
-
An in-place sorting algorithm would a space complexity of O(1). In other words, it does not use any other auxiliary memory because it moves the items in the collection itself.
61
+
An ((in-place sorting)) algorithm would a _space complexity_ of O(1). In other words, it does not use any other auxiliary memory because it moves the items in the collection itself.
64
62
This is specially useful for memory constraint enviroments like robotics or embedded systems in appliances.
65
63
66
64
== Online
67
65
68
66
It can sort a list as it receives it.
69
-
Online algorithms doesn't have to re-sort the whole collection for every new item added.
67
+
((Online sorting)) algorithms doesn't have to re-sort the whole collection for every new item added.
70
68
71
69
== Adaptive
72
70
73
-
It runs fasteran already sorted or partially sorted collection.
71
+
Algorithms with ((adaptive sorting)) run faster, close to _O(n)_, on an already sorted (or partially sorted) collection.
0 commit comments