You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MergeSort(A[], p, r){
if(p < r) then {
q <-(p+q)/2'
MergeSort(A,p,q);
MergeSort(A,q+1,r);
Merge(A, q, q, r);
}
}
Merge(A[], p, q, r){
정렬되어 있는 두 배열 A[p...q]와 A[q+1...r]을 합하여
정렬된 하나의 배열 A[p...r]을 만든다.
}
시간복잡도
Quick sort
Divide and Conquer
분할 : 배열에 어떤 pivot을 두고 그 pivot 보다 작은 값과 큰 값으로 분할
정복 : 각 부분을 순환적으로 정렬
합병 : nothing to do
QuickSort(A[], p, r){
if (p<r) then {
q = partition(A, p, r);// 분할
QuickSort(A, p, q-1);// 왼쪽 부분배열 정렬
QuickSort(A, q+1, r);// 오른쪽 부분배열 정렬
}
}
Partition(A[], p, r){
배열 A[p...r]의 원소들을 A[r]을 기준으로 양쪽으로 재배치하고
A[r]이 자리한 위치를 return;
x<-A[r]// Pivot
i<-p-1;
for j<-p to r-1{
if A[j]<=x then
i<-i+1;
exchange A[i] and A[j];
}
exchange A[i+1] and A[r];
return i+1;
}