✍️
TIL
  • TIL
  • react
    • react 16.3 이후 라이프 싸이클
    • 함수형 컴포넌트 vs 클래스 컴포넌트
    • React의 장점
    • 기본문법
    • Flow Diagram
    • redux-saga
    • nextjs
    • mobx
    • 리액트는 어떻게 동작할까?
  • data-structure
    • 이진 검색 트리(binary search tree)
    • HashTable
    • Tree
    • 트라이(Trie)
    • 선형 구조 vs 비선형 구조
    • 연결 리스트(linked-list)
    • Queue
    • Graph
    • Heap
    • Stack
  • web
    • 웹 브라우저의 동작 원리
    • Basic
    • Webpack이란?
    • rendering
    • npm
    • babel
  • graphQL
    • Query
    • Mutation
    • Introduction
  • algorithm
    • big-o
    • 버블 정렬(bubble sort)
    • 힙 정렬(heap sort)
    • 선택 정렬(selection sort)
    • 퀵 정렬(quick sort)
    • 백트래킹 알고리즘
    • 삽입 정렬(insertion sort)
    • 계수 정렬(counting sort)
    • 다엑스트라 알고리즘
    • 이진 탐색
    • 합병 정렬(merge-sort)
    • 동적 계획법(Dynamic programming)
  • web-security
    • XSS(Cross Site Scripting)
    • CSRF(Cross-site request forgery)
    • Tabnabbing
  • javaScript
    • dom
    • 자바스크립트 성능 최적화
    • Event Loop
    • Snippets
    • javaScript
  • programming-paradigm
    • Object Oriented Programming
    • 함수형 프로그래밍
    • 구조적 프로그래밍
  • computer-science
    • Process vs Thread
    • 비트 연산자
    • 그레이 코드
  • vue
    • Vue
  • design-pattern
    • MVP pattern
    • Flux
    • 아토믹 디자인
    • MVVM pattern
    • MVC pattern
  • css
    • css
    • Grid
    • css-methodologies
    • FlexBox
  • html
    • html
  • regExp
    • regExp
  • git
    • Git-flow
Powered by GitBook
On this page

Was this helpful?

  1. algorithm

계수 정렬(counting sort)

먼저 배열을 생성한뒤 생성한 배열에 정렬할 배열의 원소중 제일 큰 수 만큼 0을 넣습니다

배열을 순회하며 각 원소가 몇번 등장했는지 갯수를 생성한 배열에 저장합니다

갯수를 저장한 것을 누적합으로 바꿔줍니다

누적합을 바탕으로 값을 결과에 넣어줍니다

function countingSort(array) {
  const max = Math.max(...array)
  const count = new Array(max + 1).fill(0)
  const result = []
  array.forEach(val => {
    count[val]++
  })
  for (let i = 0; i < max; i++) {
    // 누적합을 구합니다.
    count[i + 1] += count[i]
  }
  array.forEach(val => {
    // 누적합이 가리키는 인덱스를 바탕으로 결과에 숫자를  집어넣습니다.
    result[count[val] - 1] = val
    count[val]--
  })
  return result
}

countingSort([1, 3, 2, 4, 11, 2, 6, 4, 11, 1, 7])
Previous삽입 정렬(insertion sort)Next다엑스트라 알고리즘

Last updated 5 years ago

Was this helpful?