From 3d6499e663ccef8735ed298f0e8a1163d4fed7c9 Mon Sep 17 00:00:00 2001 From: Eric Kim <48634064+hyeonukim@users.noreply.github.com> Date: Wed, 16 Oct 2024 21:40:12 -0700 Subject: [PATCH] update 10-15, upload 10-16 --- _posts/2024-10-15-Selection-Sort.md | 25 +++--- _posts/2024-10-16-Insertion-Sort.md | 127 ++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 11 deletions(-) create mode 100644 _posts/2024-10-16-Insertion-Sort.md diff --git a/_posts/2024-10-15-Selection-Sort.md b/_posts/2024-10-15-Selection-Sort.md index b4b06f9..778e5bc 100644 --- a/_posts/2024-10-15-Selection-Sort.md +++ b/_posts/2024-10-15-Selection-Sort.md @@ -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++] --- @@ -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. @@ -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, @@ -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. \ No newline at end of file diff --git a/_posts/2024-10-16-Insertion-Sort.md b/_posts/2024-10-16-Insertion-Sort.md new file mode 100644 index 0000000..139d4e9 --- /dev/null +++ b/_posts/2024-10-16-Insertion-Sort.md @@ -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 +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. \ No newline at end of file