-
Notifications
You must be signed in to change notification settings - Fork 12
[3주차] 정렬 (버블, 선택, 삽입, 셀, 머지, 퀵, 힙) #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8a2eeed
7671210
8441a92
ced3cae
77419a4
21a006a
3545239
b9a9f70
75ca29c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,26 @@ | ||
const exchange = (array, index1, index2) => { | ||
[array[index1], array[index2]] = [array[index2], array[index1]]; | ||
}; | ||
|
||
const isFirstBiggerThanSecond = (a, b) => a > b; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 함수가 하는 일을 굉장히 자세하게 작성하셨네요. 아마도 각 파라미터가 무엇을 의미하는지까지 명시해주고 싶으셨던 것 같습니다. isGreaterThen 정도도 괜찮을 것 같네요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
정확히 제가 의도한 바가 맞습니다. ㅎㅎ |
||
const bubbleSort = (array) => { | ||
const { length } = array; | ||
|
||
for (let pass = 1; pass <= length; pass++) { | ||
let exchangeCount = 0; | ||
|
||
for (let index = 0; index < length - pass; index++) { | ||
if (isFirstBiggerThanSecond(array[index], array[index + 1])) { | ||
exchange(array, index, index + 1); | ||
exchangeCount++; | ||
} | ||
} | ||
|
||
// todo : 정렬이 완료됐다면 loop 를 종료한다. | ||
if (exchangeCount === 0) { | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
test.each([ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이런 일은 swap이라는 말을 더 많이 사용하는 것 같네요.