Skip to content

Conversation

@jinvicky
Copy link

버블 정렬 문제 풀고 그림 그렸습니다.
셀 정렬과 퀵 정렬이 유난히 더 어려웠습니다.

퀵 정렬은 피벗이 n개일 수도 있는 건가요?

image

image

image

image

image

image

image

image

image

image

image

image

image

@hannut91
Copy link
Contributor

퀵 정렬은 피벗이 n개일 수도 있는 건가요?

네 피벗을 여러개 쓰는 퀵 정렬 방법도 있습니다. 강의에서는 다루지는 않지만, 피벗을 두개 사용해서 첫 번째 피벗보다 작은 원소들, 두 피벗 사이의 원소들, 두 번째 피벗보다 큰 원소들로 나누는 듀얼 피벗 퀵 정렬이 있습니다.

그 외에도 3way 퀵정렬도 있는데, 이건 5주차에서 배웁니다.

Comment on lines +12 to +19
for (let j = 0; j< (length -i -1); j++) {
if (less(array[j + 1], array[j])) {
exchange (array, j, j+1);
isAligned = false;
}
else isAligned = true; // exchange하지 않았을 경우 정렬된 상태
}
if (isAligned) return array; // j를 한 바퀴 돌 동안 isAligned가 true면 정렬된 배열
Copy link
Contributor

Choose a reason for hiding this comment

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

한 for문을 실행할 때 단 한 번도 교환이 안됐다면, 정렬이 되었다는 것을 확신할 수 있어요. else로 해버리면 마지막에만 교환이 안되어도 모두 정렬되었다고 판단해버릴 수 있어요.

    for (let j = 0; j< (length -i -1); j++) {
      let isAligned = true;
      
      if (less(array[j + 1], array[j])) {
        exchange (array, j, j+1);
        isAligned = false;
      } 
    }
    
    if (isAligned) {
      return array;
    }

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