강의

멘토링

커뮤니티

Inflearn Community Q&A

kaehehehe's profile image
kaehehehe

asked

Introduction to Javascript Algorithm Problem Solving (Coding Test Preparation)

1. Selection sort

idx를 선언해줘야 하는 이유..

Written on

·

301

4

안녕하세요?

idx를 선언해줘야 하는 이유가 모르겠어요..

idx를 생략하고 이렇게 코드를 짜면 안되나요?

코테 준비 같이 해요! javascript

Answer 1

4

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

위에 코드 같은 경우 j for문이 돌면서 arr[j]<arr[i]가 참일경우 매번 arr[i]와 arr[j]의 값을 교환합니다.

idx를 쓰는 영상의 방법은 j for문이 끝나고 나서 한 번 두 값의 교환을 합니다. 

즉 idx를 쓰는 이유는 두 값의 교환 횟수를 최소화하기 위해서입니다.

kaehehehe's profile image
kaehehehe

asked

Ask a question