2014-10-24 2 views
0

내 병합 정렬 알고리즘은 아래에 있습니다 ...하나의 배열을 사용하여 병합 정렬

이 알고리즘은 추가 메모리가 필요합니다. 하나의 배열로만 정렬 할 수 있습니까? (. 하나의 배열에 정렬)

내 병합 정렬 알고리즘은 다음과 같습니다

//LeftPos = start of left half; 
//RightPos = start of right half 
void Merge(int A[ ], int LeftPos, int RightPos, int RightEnd) 
{ 
    int LeftEnd = RightPos – 1; 
    int TmpPos = 1 
    int NumElements = RightEnd – LeftPos + 1; 
    int TempArray[NumElements]; 
    while(leftPos <= LeftEnd && RightPos <= RightEnd) 
     if(A[LeftPos] <= A[RightPos]) 
      TmpArray[TmpPos++] = A[LeftPos++]; 
     else 
      TmpArray[TmpPos++] = A[RightPos++]; 
    while(LeftPos <= LeftEnd) //Copy rest of first half 
     TmpArray[TmpPos++] = A[LeftPos++]; 
    while(RightPos <= RightEnd) //Copy rest of second half  TmpArray[TmpPos++] = A[RightPos++]; 
    for(int i = 1; i <= NumElements; i++) //Copy TmpArray back 
     A[LeftPos++] = TmpArray[i]; 
} 
+0

[가능한 병합 정렬 알고리즘을 사용하여 정렬 방법] (http://stackoverflow.com/questions/2571049/how-to-sort-in-place-using-the-merge-sort -연산) –

답변

관련 문제