Skip to content
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

[daadaadaah] BubbleSort와 InsertSort 구현 #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

daadaadaah
Copy link
Collaborator

No description provided.

@daadaadaah daadaadaah changed the title [daadaadaah] Implement BubbleSort and InsertSort [daadaadaah] BubbleSort와 InsertSort 구현 Jan 3, 2021
Copy link
Collaborator

@gyim1345 gyim1345 left a comment

Choose a reason for hiding this comment

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

insertSort 가 지금 들어가야 할 위치를 찾은 다음에 하나씩 바꿔 가면서 위치 바꾸는것 맞죠?
getInsertIndex로 들어가야할 위치를 구하시고
getTempInsertSortArray로 구한 위치에 따라 배열의 요소들을 옮기는것 같아요.
혹시 따로따로 하신 이유가 있을까요? 큰 차이는 아니지겠지만 위치를 구하면서 요소들을 동시에 옮겨도 될것 같아서 질문 드립니다!

@daadaadaah
Copy link
Collaborator Author

insertSort 가 지금 들어가야 할 위치를 찾은 다음에 하나씩 바꿔 가면서 위치 바꾸는것 맞죠?
getInsertIndex로 들어가야할 위치를 구하시고
getTempInsertSortArray로 구한 위치에 따라 배열의 요소들을 옮기는것 같아요.
혹시 따로따로 하신 이유가 있을까요? 큰 차이는 아니지겠지만 위치를 구하면서 요소들을 동시에 옮겨도 될것 같아서 질문 드립니다!

특별한 이유는 없어요~ 그냥 insertionSort가 insert할 인덱스를 찾아 Insert하니까 그렇게 구현했어요~ㅎㅎ

혹시, 위치를 구하면서 요소들을 동시에 옮긴다면, 코드가 어떻게 되나용?ㅎㅎ

@gyim1345
Copy link
Collaborator

gyim1345 commented Jan 8, 2021

const getTempInsertSortArray = (array, currentIndex) => {
  const currentElement = array[currentIndex];  
  let insertIndex;
  for(let k = currentIndex; k >= 0; k--) {
    if(currentElement > array[k-1] || array[k-1] === undefined) {
      insertIndex = k;
      break;
    }
    array[k] = array[k-1];
  }

  array[insertIndex] = currentElement;  
  return array;
}


const insertSort = (array) => {
  for (let i = 1; i < array.length; i++) {
    const currentIndex = i;
    array = getTempInsertSortArray(array, currentIndex)
  }

  return array;
}

위치를 구하면서 요소들을 동시에 옮긴 코드 입니다!

@Woomin-Jeon Woomin-Jeon removed their request for review September 6, 2023 02:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants