|
1 | | -= Sorting Properties |
| 1 | += Sorting Algorithms |
2 | 2 |
|
3 | | -Sorting implementations with the same time complexity might manipulate the data differently. We want to understand these differences so we can be aware of the side-effects it will have on data or extra resources. For instance, some solutions will need auxiliary memory to store temporary data while sorting while other can do it in place. |
| 3 | +Sorting is one of the most common solutions when we want to extract some insights about a collection of data. |
| 4 | +We can sort to get the maximum or minimun value and many algorithmic problems involves sorting data first. |
4 | 5 |
|
5 | | -== Stable |
| 6 | +.We are going to explore 3 basic sorting algorithms _O(n^2^)_ which have low overhead: |
| 7 | +- <<Bubble Sort>> |
| 8 | +- <<Selection Sort>> |
| 9 | +- <<Insertion Sort>> |
6 | 10 |
|
7 | | -An ((stable sorting)) algorithms keeps the relative order of items with the same comparison criteria. |
| 11 | +.Efficient sorting algorithms _O(n log n)_: |
| 12 | +- <<Merge Sort>> |
| 13 | +- <<Quicksort>> |
8 | 14 |
|
9 | | -This specially useful when you want to sort on multiple phases. |
10 | | - |
11 | | -.Let's say you have the following data: |
12 | | -[source, javascript] |
13 | | ----- |
14 | | -const users = [ |
15 | | - { name: 'Bob', age: 32 }, |
16 | | - { name: 'Alice', age: 30 }, |
17 | | - { name: 'Don', age: 30 }, |
18 | | - { name: 'Charly', age: 32 }, |
19 | | -]; |
20 | | ----- |
21 | | - |
22 | | -.If you sort by `name` you would have: |
23 | | -[source, javascript] |
24 | | ----- |
25 | | -[ |
26 | | - { name: 'Alice', age: 30 }, |
27 | | - { name: 'Bob', age: 32 }, |
28 | | - { name: 'Charly', age: 32 }, |
29 | | - { name: 'Don', age: 30 }, |
30 | | -]; |
31 | | ----- |
32 | | - |
33 | | -Then, here comes the important part, if you sort by `age` you might get two different results. |
34 | | - |
35 | | -.If the sorting algorithm is *stable*, it should keep the items with the same age ordered by `name`: |
36 | | -[source, javascript] |
37 | | ----- |
38 | | -[ |
39 | | - { name: 'Alice', age: 30 }, |
40 | | - { name: 'Don', age: 30 }, |
41 | | - { name: 'Bob', age: 32 }, |
42 | | - { name: 'Charly', age: 32 }, |
43 | | -]; |
44 | | ----- |
45 | | - |
46 | | -.However, if the sorting is *not stable*, then you will lose the relative order of the items and get something like this: |
47 | | -[source, javascript] |
48 | | ----- |
49 | | -[ |
50 | | - { name: 'Don', age: 30 }, |
51 | | - { name: 'Alice', age: 30 }, |
52 | | - { name: 'Charly', age: 32 }, |
53 | | - { name: 'Bob', age: 32 }, |
54 | | -]; |
55 | | ----- |
56 | | - |
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. |
58 | | - |
59 | | -== In-place |
60 | | - |
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. |
62 | | -This is specially useful for memory constraint enviroments like robotics or embedded systems in appliances. |
63 | | - |
64 | | -== Online |
65 | | - |
66 | | -It can sort a list as it receives it. |
67 | | -((Online sorting)) algorithms doesn't have to re-sort the whole collection for every new item added. |
68 | | - |
69 | | -== Adaptive |
70 | | - |
71 | | -Algorithms with ((adaptive sorting)) run faster, close to _O(n)_, on an already sorted (or partially sorted) collection. |
| 15 | +Before we dive into the most well-known sorting algorithms, let's discuess about the sorting properties. |
0 commit comments