Notice
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 프로그래머스
- REACT
- 스파르타코딩클럽
- 웹개발종합반
- rn
- ReactNative
- 챗GPT
- 사전준비
- 실전프로젝트
- 달리기반
- D반8조
- Ai
- TDD
- 항해99
- 맥린이
- 알pdf #파일탐색기미리보기안될때
- 7기
- 리액트
- Expo
- 알고리즘기초주차
- NotionAI
- ChatGPT
- Programmers
- typeScript
- TS
- 멍친구
- 필수강의
- 팀워크최고
- 코린이
- 프론트엔드
Archives
- Today
- Total
FrontEnd :-)
Sorting Algorithms_Selection/Insertion Sort 본문
Udemy - 【한글자막】 JavaScript 알고리즘 & 자료구조 마스터클래스
Selection Sort 선택 정렬
Similar to bubble sort, but instead of first placing large values into sorted position, it places small values into sorted position
Selection Sort Pseudocode
- Store the first element as the smallest value you've seen so far.
- Compare this item to the next item in the array until you find a smaller number.
- If a smaller number is found, designate that smaller number to be the new "minimum" and continue until the end of the array.
- If the "minimum" is not the value (index) you initially began with, swap the two values.
- Repeat this with the next element until the array is sorted.
Insertion Sort 삽입 정렬
Builds up the sort by gradually creating a larger left half which is always sorted
Insertion Sort Pseudocode
- Start by picking the second element in the array
- Now compare the second element with the one before it and swap if necessary.
- Continue to the next element and if it is in the incorrect order, iterate through the sorted portion (i.e. the left side) to place the element in the correct place.
- Repeat until the array is sorted.
function insertionSort(arr) {
for(var i = 1; i < arr.length; i++){
let currentVal = arr[i];
for(var j = i - 1; j >= 0 && arr[j] > currentVal; j--){
arr[j+1] = arr[j];
}
arr[j+1] = currentVal;
console.log(arr);
}
return arr;
}
insertionSort([2,1,9,76,4]);
function insertionSort(array) {
for (let i = 1; i < array.length; i++) {
let j = i;
while (j > 0 && array[j] < array[j - 1]) {
[array[j - 1], array[j]] = [array[j], array[j - 1]];
j--;
}
}
return array;
}
삽입 정렬이 유리한 다른 흥미로운 상황은,
온라인 알고리즘이라는 데이터가 있는 경우입니다. 데이터가 들어오는 대로 작동하는 알고리즘으로 새로운 데이터를 수신하므로 전체 배열을 한 번에 정렬할 필요가 없습니다. 어떤 숫자가 입력되더라도 필요한 위치에 놓을 수 있습니다. 라이브, 스트리밍 방식으로 들어온 데이터를 즉시 입력해야 하는 상황에 편리합니다.
Big O of Sorting Algorithms
Algorithm | Time Complexity (Best) | Time Complexity (Average) | Time Complexity (Worst) | Space Complexity |
Bubble Sort | O(n) | O(n^2) | O(n^2) | O(1) |
Insertion Sort | O(n) | O(n^2) | O(n^2) | O(1) |
Selection Sort | O(n^2) | O(n^2) | O(n^2) | O(1) |
Recap
- Sorting is fundamental!
- Bubble sort, selection sort, and insertion sort are all roughly equivalent
- All have average time complexities that are quadratic
- We can do better...but we need more complex algorithms!
'JavaScript > Algorithm' 카테고리의 다른 글
Intermediate Sorting Algorithms_Quick Sort (0) | 2023.04.11 |
---|---|
Intermediate Sorting Algorithms_Merge Sort (0) | 2023.04.08 |
Sorting Algorithms_BubbleSort (0) | 2023.04.03 |
Searching Algorithms (0) | 2023.03.30 |
Recursion (0) | 2023.03.27 |
Comments