Skip to content

Commit ff488e5

Browse files
authored
Merge pull request #26 from 2G-Afroz/2G-Afroz-Patch-2
Added pseudocode for Selection Sort and Insertion Sort Algorithm.
2 parents 6ae9036 + 652eb6f commit ff488e5

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Insertion Sort
2+
Insertion sort is a simple sorting algorithm that builds the final sorted array one element at a time. It is much less efficient on large lists than more advanced algorithms such as `QuickSort`, `MergeSort`, or `HeapSort`. However, it performs well for small datasets or lists that are nearly sorted.
3+
4+
## Pseudocode
5+
```plaintext
6+
function insertionSort(array):
7+
n = array.length()
8+
for i = 1 to n - 1:
9+
key = array[i]
10+
j = i - 1
11+
12+
# Move elements greater than key to one position ahead
13+
while j >= 0 and array[j] > key:
14+
array[j + 1] = array[j]
15+
j = j - 1
16+
17+
# Place the key at its correct position
18+
array[j + 1] = key
19+
```
20+
21+
## Time & Space Complexity
22+
|Case |Time |Space |
23+
|--------|---------------:|-----:|
24+
|Best |Ω(n) |Ω(1) |
25+
|Average |Θ(n<sup>2</sup>)|Θ(1) |
26+
|Worst |O(n<sup>2</sup>)|O(1) |
27+
28+
## Variants
29+
- Binary Insertion Sort
30+
- Shell Sort
31+
32+
## Use Cases
33+
- Effective for small datasets or nearly sorted lists.
34+
- Well-suited for situations where the input array is already partially sorted.
35+
- Simplicity and low overhead make it suitable for educational purposes.
36+
37+
## Best Practices
38+
- Inefficient for large datasets, opt for more efficient algorithms like `QuickSort` or `MergeSort`.
39+
- Consider using variants like Binary Insertion Sort for slightly better performance.
40+
- Well-suited for scenarios where elements are added to the array sequentially over time.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Selection Sort
2+
Selection sort is a straightforward sorting algorithm that sorts an array by repeatedly finding the minimum element from the unsorted part of the array and putting it at the beginning. The process is iteratively applied to the remaining unsorted elements until the entire array is sorted.
3+
4+
## Pseudocode
5+
```plaintext
6+
function selectionSort(array):
7+
n = array.length()
8+
for i = 0 to n - 1:
9+
# Assume the current index is the minimum
10+
minIndex = i
11+
12+
# Search for the minimum element in the unsorted part
13+
for j = i + 1 to n - 1:
14+
if array[j] < array[minIndex]:
15+
minIndex = j
16+
17+
# Swap the found minimum element with the first element
18+
temp = array[minIndex]
19+
array[minIndex] = array[i]
20+
array[i] = temp
21+
```
22+
23+
## Time & Space Complexity
24+
|Case |Time |Space |
25+
|--------|---------------:|-----:|
26+
|Best |Ω(n<sup>2</sup>)|Ω(1) |
27+
|Average |Θ(n<sup>2</sup>)|Θ(1) |
28+
|Worst |O(n<sup>2</sup>)|O(1) |
29+
30+
## Variants
31+
- Double-Selection Sort
32+
- Stable Selection Sort
33+
- Recursive Selection Sort
34+
- Block-Swap Selection Sort
35+
36+
## Use Cases
37+
- Suitable for sorting small datasets due to its simplicity.
38+
- Applicable in situations with constrained memory where constant space complexity is advantageous.
39+
- Considered when the cost of swapping elements is significantly higher than the cost of comparisons.
40+
41+
## Best Practices
42+
- Inefficient for large datasets, opt more efficient algorithms like `Merge Sort` or `QuickSort`.
43+
- Explore variants or optimizations, such as stable selection sort, based on specific requirements.

0 commit comments

Comments
 (0)