Skip to content

김승우 3주차 과제 구현 #2

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

kyh196201
Copy link

우선 구현된 bubble-sort 코드를 참고하지 않고, 혼자 bubble 정렬을 구현해봤습니다.

생각보다 구현하는 것이 어려워서 생각한 내용을 주석으로 적어 놨습니다.

추가 문제랑 다음 과제도 얼른 풀이 올리겠습니다 😃

Comment on lines +8 to +20
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
exchangeCount += 1;

[array[j + 1], array[j]] = [array[j], array[j + 1]];
}
}

if (i === 0 && exchangeCount === 0) {
return;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

바깥 for문과 안쪽 for문이 있는데, 안쪽 for문에서 한 번도 교환이 일어나지 않았다면, 정렬되어있다는 것을 확신할 수 있습니다.

  for (let i = 0; i < array.length; i++) {
    let exchangeCount = 0;

    for (let j = 0; j < array.length - 1 - i; j++) {
      if (array[j] > array[j + 1]) {
        exchangeCount += 1;

        [array[j + 1], array[j]] = [array[j], array[j + 1]];
      }
    }

    if (exchangeCount === 0) {
      return;
    }
  }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다!! 굳이 i === 0을 할 필요가 없었네요!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants