KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (487)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (432)
      • 백준 (187)
      • Programmers (105)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • 문자열
  • tree
  • 그래프
  • C++
  • LeetCode
  • programmers
  • 제코베 JS 100제
  • Level 0
  • 백준
  • recursion
  • Level1
  • string
  • C
  • 수학
  • js
  • Level 2
  • codeup
  • 정렬
  • Level 1
  • 다이나믹 프로그래밍

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

ALGORITHM/정렬

Algorithm / 정렬 / Selection Sort (선택 정렬)

2022. 8. 17. 18:40

Selection Sort (선택 정렬)

function selection_sort1(arr) {
  console.log(`before selection sorting: ${[...arr]}`);
  let cnt = 0;

  for (let i = 0; i < arr.length; i++) {
    let smaller_idx = i;
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[j] < arr[smaller_idx]) {
        smaller_idx = j;
      }
    }
    // swap
    [arr[i], arr[smaller_idx]] = [arr[smaller_idx], arr[i]];
    cnt++;
  }

  console.log(`after selection sorting: ${[...arr]}`);
  console.log(`Sorting count = ${cnt}`);
}

function selection_sort2(arr) {
  console.log(`before selection sorting: ${[...arr]}`);
  let cnt = 0;

  for (let i = 0; i < arr.length; i++) {
    let smaller_idx = i;
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[j] < arr[smaller_idx]) {
        smaller_idx = j;
      }
    }
    // swap
    // 현재 인덱스가 최솟값의 인덱스일 경우 swap을 하지 않도록
    if (i !== smaller_idx) {
      [arr[i], arr[smaller_idx]] = [arr[smaller_idx], arr[i]];
      cnt++;
    }
  }

  console.log(`after selection sorting: ${[...arr]}`);
  console.log(`Sorting count = ${cnt}`);
}

let arr = [3, 6, 13, 4, 33, 12, 35, 87, 8, 64];
selection_sort1(arr); // Sorting Count = 10
selection_sort2(arr); // Sorting Coutn = 8

시간 복잡도: O(n^2)

  • selection_sort1
    • 반복문을 통해 현재의 값보다 작은 최솟값을 찾은 뒤 위치를 바꿔준다.
  • selection_sort2
    • 만약 현재의 값이 최솟값인 경우 굳이 swap을 수행하지 않아 시간을 조금 줄일 수 있다.
저작자표시 (새창열림)

'ALGORITHM > 정렬' 카테고리의 다른 글

Algorithm / 정렬 / Quick Sort (퀵 정렬)  (0) 2022.08.31
Algorithm / 정렬 / Merge Sort (합병 정렬)  (0) 2022.08.18
Algorithm / 정렬 / Insertion Sort (삽입 정렬)  (0) 2022.08.18
Algorithm / 정렬 / Bubble Sort (버블 정렬)  (0) 2022.08.17
    'ALGORITHM/정렬' 카테고리의 다른 글
    • Algorithm / 정렬 / Quick Sort (퀵 정렬)
    • Algorithm / 정렬 / Merge Sort (합병 정렬)
    • Algorithm / 정렬 / Insertion Sort (삽입 정렬)
    • Algorithm / 정렬 / Bubble Sort (버블 정렬)
    KimMinJun
    KimMinJun

    티스토리툴바