|
| 1 | +# Sorting |
| 2 | + |
| 3 | +From wikipedia |
| 4 | + |
| 5 | +> Sorting refers to ordering data in an increasing or decreasing manner according to some linear relationship among the |
| 6 | +> data items. Ordering: arranging items in a sequence ordered by some criterion. Categorizing: grouping items with |
| 7 | +> similar properties. |
| 8 | +
|
| 9 | +## 🎨 Design |
| 10 | + |
| 11 | +**Sorting Techniques** |
| 12 | + |
| 13 | +| | Definition | |
| 14 | +|----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 15 | +| Bubble Sort | Repeatedly compares two adjacent elements and swaps them if they are in the wrong order | |
| 16 | +| Selection Sort | Repeatedly finds the minimum element and sort it in order | |
| 17 | +| Insertion Sort | This sorting algorithm maintains a sub-array that is always sorted. Values from the unsorted part of the array are placed at the correct position in the sorted part | |
| 18 | + |
| 19 | +## 📈 Complexity Analysis |
| 20 | + |
| 21 | +| Sort type | Complexity | |
| 22 | +|----------------|------------| |
| 23 | +| Bubble Sort | $O(n^2)$ | |
| 24 | +| Selection Sort | $O(n^2)$ | |
| 25 | +| Insertion Sort | $O(n^2)$ | |
| 26 | + |
| 27 | +## 💻 Implementation in Python |
| 28 | + |
| 29 | +Python has built-in function `sorted`. |
| 30 | + |
| 31 | +```python |
| 32 | +array = [8, 5, 9, 2, 1] # [8, 5, 9, 2, 1] |
| 33 | +sorted(array) # [1, 2, 5, 8, 9] |
| 34 | + |
| 35 | +words = ['banana', 'pie', 'Washington', 'book'] # ['banana', 'pie', 'Washington', 'book'] |
| 36 | +sorted(words, key=len) # ['pie', 'book', 'banana', 'Washington'] |
| 37 | +``` |
| 38 | + |
| 39 | +Implement Bubble Sort in Python |
| 40 | + |
| 41 | +```python |
| 42 | +def bubble_sort(array): |
| 43 | + n = len(array) |
| 44 | + for i in range(n): |
| 45 | + already_sorted = True |
| 46 | + |
| 47 | + for j in range(n - i - 1): |
| 48 | + if array[j] > array[j + 1]: |
| 49 | + array[j], array[j + 1] = array[j + 1], array[j] |
| 50 | + already_sorted = False |
| 51 | + |
| 52 | + if already_sorted: |
| 53 | + break |
| 54 | + return array |
| 55 | + |
| 56 | + |
| 57 | +array = [8, 5, 9, 2, 1] # [8, 5, 9, 2, 1] |
| 58 | +bubble_sort(array) # [1, 2, 5, 8, 9] |
| 59 | +``` |
| 60 | + |
| 61 | +Implement Selection Sort in Python |
| 62 | + |
| 63 | +```python |
| 64 | +def selection_sort(array): |
| 65 | + n = len(array) |
| 66 | + for i in range(n): |
| 67 | + min_idx = i |
| 68 | + |
| 69 | + for j in range(i + 1, n): |
| 70 | + if array[j] < array[min_idx]: |
| 71 | + min_idx = j |
| 72 | + |
| 73 | + array[i], array[min_idx] = array[min_idx], array[i] |
| 74 | + return array |
| 75 | + |
| 76 | + |
| 77 | +array = [8, 5, 9, 2, 1] # [8, 5, 9, 2, 1] |
| 78 | +selection_sort(array) # [1, 2, 5, 8, 9] |
| 79 | +``` |
| 80 | + |
| 81 | +Implement Insertion Sort in Python |
| 82 | + |
| 83 | +```python |
| 84 | +def insertion_sort(array): |
| 85 | + for i in range(1, len(array)): |
| 86 | + key_item = array[i] |
| 87 | + j = i - 1 |
| 88 | + |
| 89 | + while j >= 0 and array[j] > key_item: |
| 90 | + # Shift the value one position to the left |
| 91 | + # and reposition j to point to the next element |
| 92 | + # (from right to left) |
| 93 | + array[j + 1] = array[j] |
| 94 | + j -= 1 |
| 95 | + |
| 96 | + array[j + 1] = key_item |
| 97 | + return array |
| 98 | + |
| 99 | + |
| 100 | +array = [8, 5, 9, 2, 1] # [8, 5, 9, 2, 1] |
| 101 | +insertion_sort(array) # [1, 2, 5, 8, 9] |
| 102 | +``` |
| 103 | + |
| 104 | +## 🔗 Further Reading |
| 105 | + |
| 106 | +* [Sorting](https://en.wikipedia.org/wiki/Sorting), wikipedia |
| 107 | +* [Sorting Algorithms in Python](https://realpython.com/sorting-algorithms-python/), realpython |
| 108 | +* [How to Use sorted() and sort() in Python](https://realpython.com/python-sort/), realpython |
| 109 | +* [Sorting Algorithms in Python](https://www.geeksforgeeks.org/sorting-algorithms-in-python/), geeksforgeeks 2022 |
| 110 | +* ▶️ [Python Sorting Algorithms](https://www.youtube.com/playlist?list=PLj8W7XIvO93rJHSYzkk7CgfiLQRUEC2Sq), Joe James, 2015 |
0 commit comments