Skip to content

Commit

Permalink
update 10-15, upload 10-16
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonukim committed Oct 17, 2024
1 parent c4a931c commit 3d6499e
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 11 deletions.
25 changes: 14 additions & 11 deletions _posts/2024-10-15-Selection-Sort.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Algorithm Analysis (1) - Selection Sort
date: 2024-10-15
categories: [Algorithm Analysis]
categories: [Algorithm Analysis, Sorting]
tags: [Algorithm Analysis, Selection Sort, Sorting, Study, Python, C#, C++]
---

Expand All @@ -10,9 +10,11 @@ tags: [Algorithm Analysis, Selection Sort, Sorting, Study, Python, C#, C++]

[- Coding Selection Sort](#coding-selection-sort)

[- Selection Sort's Time Complexity](#selection-sorts-time-complexity)

[- Selection Sort's characteristics](#selection-sorts-characteristics)

[- Selection Sort's Time Complexity](#selection-sorts-time-complexity)


## Understanding Selection Sort
Selection Sort is one of many sorting algorithms that can sort data. Say that we want to sort array in **ascending order**, then key idea with Selection Sort is that we're looking for **minimum value**, and gradually **swapping them with the i-th element of the array**. Here's a simple array and we're going to use Selection Sort to sort this array.
Expand Down Expand Up @@ -136,15 +138,6 @@ void SelectionSort(int[] array, int n) {
}
```

## Selection Sort's characteristics

**Advantage**
- No additional temporary storage is required other than the original list.
- Easy to implement.

**Disadvantage**
- Poor efficiency regarding time complexity when dealing with big arrays.

## Selection Sort's Time Complexity
To calculate the Time Complexity of Selection Sort,

Expand All @@ -161,3 +154,13 @@ To calculate the Time Complexity of Selection Sort,
Thus, we have

**T(n) = (n-1) + (n-2) + ... + 2 + 1 = n(n-1)/2 = O(n^2)**


## Selection Sort's characteristics

**Advantage**
- No additional temporary storage is required other than the original list.
- Easy to implement.

**Disadvantage**
- Poor efficiency regarding time complexity when dealing with big arrays.
127 changes: 127 additions & 0 deletions _posts/2024-10-16-Insertion-Sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
---
title: Algorithm Analysis (2) - Insertion Sort
date: 2024-10-16
categories: [Algorithm Analysis, Sorting]
tags: [Algorithm Analysis, Insertion Sort, Sorting, Study, Python, C#, C++]
---

## Goal
[- Understanding Insertion Sort](#understanding-insertion-sort)

[- Coding Insertion Sort](#coding-insertion-sort)

[- Insertion Sort's Time Complexity](#insertion-sorts-time-complexity)

[- Insertion Sort's characteristics](#insertion-sorts-characteristics)

## Understanding Insertion Sort

This is another way of sorting data structure, and key idea is to **insert the element to appropriate place**. It is a bit more complicated to implement compared to [Selection Sort](https://hyeonukim.github.io/devblog/posts/Selection-Sort/), but Insertion Sort is a better method as time complexity to sort is more efficient.

>Why is it more efficient? Since Insertion Sort only compares data when needed, it is even more efficient when array is already somewhat sorted.
Let's see an example of how Insertion Sort works.

Beginning of Array:
| 5 | 7 | 8 | 3 | 9 |

Step 1: We start at 2nd element of array, and we check if it should go to left of 5 or right of 5
and since 7 is bigger than 5, we should leave 7 as it is.
| 5 | 7 | 8 | 3 | 9 |
^

Step 2: Again, here we don't need to swap since 8 should be coming after 7
| 5 | 7 | 8 | 3 | 9 |
^

Step 3: Here, we are checking where 3 should be placed from left of 5, 7, 8, right of 8 and since 3 is
smaller than 5, it should be at left of 5
| 5 | 7 | 8 | 3 | 9 |
^

Step 4: No insertion is needed here as 9 is bigger than 8
| 3 | 5 | 7 | 8 | 9 |
^

Thus, we have a sorted array.


## Coding Insertion Sort

### Python
```python
#array : array that we're trying to sort
#n : length of array
def InsertionSort(array, n):
for i in range(1, n):
for j in range(i, 0, -1):
if array[j] < array[j - 1]:
# Swap elements
array[j], array[j - 1] = array[j - 1], array[j]
else: # Element is at its correct position
break
```

### C++
```c++
#include <iostream>
using namespace std;

//array : array that we're trying to sort
//n : length of array
void InsertionSort(int array[], int n) {
for (int i = 1; i < n; i++){
for (int j = i; j > 0; j--){
if (array[j] < array[j - 1]){
// Swap elements
int temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
} else{
// Element is at its correct position
break;
}
}
}
}
```
### C#
```c#
void InsertionSort(int[] array, int n){
for (int i = 1; i < n; i++){
for (int j = i; j > 0; j--){
if (array[j] < array[j - 1]){
// Swap elements
int temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
} else {
// Element is at its correct position
break;
}
}
}
}
```

## Insertion Sort's Time Complexity

To calculate the Time Complexity of Insertion Sort,

Just like Selection Sort, it takes O(n^2) as in worst case, it has to swap

**T(n) = (n-1) + (n-2) + ... + 2 + 1 = n(n-1)/2 = O(n^2)**

However, in best case scenario, we don't have to iterate the second for loop.

Thus, in best case: **T(n) = 1 + 1 + ... + 1 + 1 = O(n)**

## Insertion Sort's characteristics

**Advantage**
- No additional temporary storage is required other than the original list.
- In best canse the time complexity is O(n).

**Disadvantage**
- In a worst case, It still has poor time complexity.

0 comments on commit 3d6499e

Please sign in to comment.