Skip to content

Commit dcf0065

Browse files
authored
Update sorting-algorithms.md
1 parent 2a84a5f commit dcf0065

File tree

1 file changed

+29
-40
lines changed

1 file changed

+29
-40
lines changed

contrib/ds-algorithms/sorting-algorithms.md

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
In computer science, a sorting algorithm takes a collection of items and arranges them in a specific order. This order is usually determined by comparing the items using a defined rule.
44

5-
## Real Life Example of Sorting
5+
Real Life Example of Sorting
6+
67
- Sorting a deck of cards
78
- Sorting names in alphabetical order
89
- Sorting a list of items, etc.
910

10-
# Some common sorting techniques
11+
Some common sorting techniques:
1112

12-
# 1. Bubble Sort
13+
## 1. Bubble Sort
1314

1415
Bubble sort is a basic sorting technique that iteratively steps through a list, comparing neighboring elements. If elements are out of order, it swaps them. While easy to understand, bubble sort becomes inefficient for large datasets due to its slow execution time.
1516

@@ -21,7 +22,7 @@ Bubble sort is a basic sorting technique that iteratively steps through a list,
2122
- **Repeating Until Sorted:** The algorithm continues making passes through the list until no more swaps are needed. This indicates the entire list is sorted.
2223

2324

24-
## Bubble Sort Code in Python
25+
### Bubble Sort Code in Python
2526

2627
```python
2728
def bubble_sort(arr):
@@ -35,7 +36,8 @@ arr = [5, 3, 8, 1, 2]
3536
bubble_sort(arr)
3637
print("Sorted array:", arr) # Output: [1, 2, 3, 5, 8]
3738
```
38-
## Example with Visualization
39+
40+
### Example with Visualization
3941

4042
Let's sort the list `[5, 3, 8, 1, 2]` using bubble sort.
4143

@@ -53,17 +55,13 @@ Let's sort the list `[5, 3, 8, 1, 2]` using bubble sort.
5355
- Comparing neighbors: `[1, 2, 3, 5, 8]`
5456
- No swapping needed, the list is already sorted.
5557

56-
## Complexity Analysis
58+
### Complexity Analysis
5759

5860
- **Worst Case:** `O(n^2)` comparisons and swaps. This happens when the list is in reverse order, and we need to make maximum swaps.
5961
- **Best Case:** `O(n)` comparisons. This occurs when the list is already sorted, but we still need O(n^2) swaps because of the nested loops.
6062
- **Average Case:** `O(n^2)` comparisons and swaps. This is the expected number of comparisons and swaps over all possible input sequences.
6163

62-
</br>
63-
<hr>
64-
</br>
65-
66-
# 2. Selection Sort
64+
## 2. Selection Sort
6765

6866
Selection sort is a simple sorting algorithm that divides the input list into two parts: a sorted sublist and an unsorted sublist. The algorithm repeatedly finds the smallest (or largest, depending on sorting order) element from the unsorted sublist and moves it to the sorted sublist. It's not efficient for large datasets but performs better than bubble sort due to fewer swaps.
6967

@@ -73,7 +71,7 @@ Selection sort is a simple sorting algorithm that divides the input list into tw
7371
- **Expanding the Sorted Sublist:** As elements are moved to the sorted sublist, it expands until all elements are sorted.
7472
- **Repeating Until Sorted:** The process continues until the entire list is sorted.
7573

76-
## Example with Visualization
74+
### Example with Visualization
7775

7876
Let's sort the list `[5, 3, 8, 1, 2]` using selection sort.
7977

@@ -97,7 +95,7 @@ Let's sort the list `[5, 3, 8, 1, 2]` using selection sort.
9795
- Find the minimum: `5`
9896
- No swapping needed, the list is already sorted.
9997

100-
## Selection Sort Code in Python
98+
### Selection Sort Code in Python
10199

102100
```python
103101
def selection_sort(arr):
@@ -114,15 +112,13 @@ selection_sort(arr)
114112
print("Sorted array:", arr) # Output: [1, 2, 3, 5, 8]
115113
```
116114

117-
## Complexity Analysis
115+
### Complexity Analysis
116+
118117
- **Worst Case**: `O(n^2)` comparisons and O(n) swaps. This occurs when the list is in reverse order, and we need to make maximum comparisons and swaps.
119118
- **Best Case**: `O(n^2)` comparisons and O(n) swaps. This happens when the list is in sorted order, but the algorithm still needs to iterate through all elements for comparisons.
120119
- **Average Case**: `O(n^2)` comparisons and O(n) swaps. This is the expected number of comparisons and swaps over all possible input sequences.
121-
</br>
122-
<hr>
123-
</br>
124120

125-
# 3. Quick Sort
121+
## 3. Quick Sort
126122
Quick sort is a popular divide-and-conquer sorting algorithm known for its efficiency on average. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then recursively sorted.
127123

128124
**Algorithm Overview:**
@@ -131,29 +127,24 @@ Quick sort is a popular divide-and-conquer sorting algorithm known for its effic
131127
- **Recursion:** Apply the above steps recursively to the sub-arrays formed by partitioning until the base case is reached. The base case is usually when the size of the sub-array becomes 0 or 1, indicating it is already sorted.
132128
- **Base Case:** If the sub-array size becomes 0 or 1, it is already sorted.
133129

134-
## Example with Visualization
130+
### Example with Visualization
135131

136132
Let's sort the list `[5, 3, 8, 1, 2]` using quick sort.
137133

138134
1. **Initial Array:** `[5, 3, 8, 1, 2]`
139-
140135
2. **Choose Pivot:** Let's choose the last element, `2`, as the pivot.
141-
142136
3. **Partitioning:**
143137
- We'll partition the array around the pivot `2`. All elements less than `2` will be placed to its left, and all elements greater than `2` will be placed to its right.
144-
145138
- After partitioning, the array becomes `[1, 2, 5, 3, 8]`. The pivot element, `2`, is now in its correct sorted position.
146-
147139
4. **Recursion:**
148140
- Now, we recursively sort the sub-arrays `[1]` and `[5, 3, 8]`.
149141
- For the sub-array `[5, 3, 8]`, we choose `8` as the pivot and partition it.
150142
- After partitioning, the sub-array becomes `[3, 5, 8]`. The pivot element, `8`, is now in its correct sorted position.
151-
152-
153143
5. **Concatenation:**
154144
- Concatenating the sorted sub-arrays `[1]`, `[2]`, `[3, 5, 8]`, we get the final sorted array `[1, 2, 3, 5, 8]`.
155145

156-
## Quick Sort Code in Python (Iterative)
146+
### Quick Sort Code in Python (Iterative)
147+
157148
```python
158149
def partition(arr, low, high):
159150
pivot = arr[high]
@@ -180,7 +171,8 @@ quick_sort_iterative(arr)
180171
print("Sorted array:", arr) # Output: [3, 9, 10, 27, 38, 43, 82]
181172

182173
```
183-
## Quick Sort Code in Python (Recursive)
174+
175+
### Quick Sort Code in Python (Recursive)
184176

185177
```python
186178
def quick_sort(arr):
@@ -196,25 +188,23 @@ arr = [5, 3, 8, 1, 2]
196188
sorted_arr = quick_sort(arr)
197189
print("Sorted array:", sorted_arr) # Output: [1, 2, 3, 5, 8]
198190
```
199-
## Complexity Analysis
191+
192+
### Complexity Analysis
200193

201194
- **Worst Case**: The worst-case time complexity of quick sort is `O(n^2)`. This occurs when the pivot selection consistently results in unbalanced partitioning, such as choosing the smallest or largest element as the pivot.
202195
-**Best Case**: The best-case time complexity is `O(n log n)`. This happens when the pivot selection leads to well-balanced partitioning, halving the array size in each recursive call.
203196
- **Average Case**: The average-case time complexity is `O(n log n)`. This is the expected time complexity when the pivot selection results in reasonably balanced partitioning across recursive calls.
204197
- **Space Complexity**: Quick sort has an `O(log n)` space complexity for the recursion stack, as it recursively sorts sub-arrays.
205-
</br>
206-
<hr>
207-
</br>
208198

209-
# 4. Merge Sort
199+
## 4. Merge Sort
210200

211201
Merge sort is a divide-and-conquer algorithm that recursively divides the input list into smaller sublists until each sublist contains only one element. Then, it repeatedly merges adjacent sublists while maintaining the sorted order until there is only one sublist remaining, which represents the sorted list.
212202

213203
**Algorithm Overview:**
214204
- **Divide:** Split the input list into smaller sublists recursively until each sublist contains only one element.
215205
- **Merge:** Repeatedly merge adjacent sublists while maintaining the sorted order until there is only one sublist remaining, which represents the sorted list.
216206

217-
## Example with Visualization
207+
### Example with Visualization
218208

219209
Let's sort the list `[38, 27, 43, 3, 9, 82, 10]` using merge sort.
220210

@@ -233,7 +223,7 @@ Let's sort the list `[38, 27, 43, 3, 9, 82, 10]` using merge sort.
233223
3. **Final Sorted List:**
234224
- `[3, 9, 10, 27, 38, 43, 82]`
235225

236-
## Merge Sort Code in Python (Iterative)
226+
### Merge Sort Code in Python (Iterative)
237227

238228
```python
239229
def merge_sort_iterative(arr):
@@ -280,7 +270,9 @@ arr = [38, 27, 43, 3, 9, 82, 10]
280270
merge_sort_iterative(arr)
281271
print("Sorted array:", arr) # Output: [3, 9, 10, 27, 38, 43, 82]
282272
```
283-
## Merge Sort Code in Python (Recursive)
273+
274+
### Merge Sort Code in Python (Recursive)
275+
284276
```python
285277
def merge_sort(arr):
286278
if len(arr) > 1:
@@ -320,10 +312,7 @@ arr = [38, 27, 43, 3, 9, 82, 10]
320312
merge_sort(arr)
321313
print("Sorted array:", arr) # Output: [3, 9, 10, 27, 38, 43, 82]
322314
```
323-
## Complexity Analysis
315+
316+
### Complexity Analysis
324317
- **Time Complexity**: `O(n log n)` for all cases. Merge sort always divides the list into halves until each sublist contains only one element, and then merges them back together, resulting in O(n log n) time complexity.
325318
- **Space Complexity**: `O(n)` auxiliary space. In the iterative version, merge sort uses additional space for creating temporary sublists during merging operations.
326-
327-
</br>
328-
<hr>
329-
</br>

0 commit comments

Comments
 (0)