logo

C의 3방향 병합 정렬

이 기사에서는 c에서 정렬을 병합하는 3가지 방법에 대해 설명합니다. 병합 정렬에서는 배열이 재귀적으로 두 부분으로 분할되어 정렬된 후 최종적으로 병합됩니다.

병합 정렬 변형은 배열을 두 부분으로 분할하는 대신 세 부분으로 분할하는 3방향 병합 정렬로 처리됩니다. 병합 정렬은 배열을 절반 크기의 하위 배열로 재귀적으로 분할합니다. 마찬가지로, 3방향 병합 정렬은 배열을 1/3 크기의 하위 배열로 분해합니다.

병합 정렬에서는 배열이 재귀적으로 두 부분으로 분할되어 정렬된 후 최종적으로 병합됩니다. 병합 정렬의 변형은 3방향 병합 정렬이라고 하는데, 배열을 두 부분으로 나누는 대신 세 부분으로 나눕니다.

병합 정렬의 예: 병합 정렬의 예는 다음과 같습니다.

 Input: 4, 8, -4, -9, 10, 55, 46, 70, -56, 78, 90, 67, 85, 20, 29 Output: -56 -9 -4 4 8 10 20 29 46 55 67 70 78 85 90 Input: 98, -67 Output: -67 98 

3방향 병합 정렬의 시간 복잡도는 nlog3n입니다.

예시 1: 여기에서는 c에서 정렬을 병합하는 3가지 방법의 예를 제공합니다. 그 예는 아래와 같습니다 -

 #include usingnamespacestd; voidmerge1(intgArr1[], intlow1, intmid1,intmid2, inthigh1, intdestArr1[]) { inti = low1, a = mid1, b = mid2, c = low1; while((i <mid1) 2 && (a < mid2) (b high1)) { if(garr1[i] garr1[j]) garr1[b]) destarr1[c++]="gArr1[i++];" } else else{ if(garr1[j] garr1[b]){ while((i mid1) mid2)){ garr1[a]){ while((a high1)){ if(garr1[a] while(i while(a while(b high) voidmergesort3wayrec(intgarr1[], intlow1, inthigh1, intdestarr1[]) if(high1 - low1 2) return; intmid1="low1" + ((high1 low1) 3); intmid2="low1" * 3) 1; mergesort3wayrec(destarr1, low1, mid1, garr1); mid2, high1, merge(destarr1, voidmergesort3way(intgarr1[], intn1){ if(n1="=" 0) intfarr1[n]; for(inti="0;" i n1; i++) farr1[i]="gArr1[i];" mergesort3wayrec(farray1, 0, n, garray1); garr1[i]="fArr1[i];" int main(){ intdata1[]="{4," 8, -4, -9, 10, 55, 46, 70, -56, 78, 90, 67, 85, 20, 29}; mergesort3way(data1,10); cout<< 'the result after the three way of merge sort is: '; 10; data1[i] << ' return0; pre> <p> <strong>Result:</strong> Now we compile the above program, and after successful compilation, we run it. Then the result is given below -</p> <pre> The result after the three way of merge sort is: -56 -9 -4 4 8 10 20 29 46 55 67 70 78 85 90 </pre> <h2>How does the above code work?</h2> <p>Here we first replica the contents of the statistics array into every other array called fArr. Then type the array by locating the midpoint that divides the array into three elements and calls the type characteristic on every array. The basic case of recursion is when an array has size 1 and is returned from a function. Then the array merging starts, and finally, the sorted array is in fArr and copied to gArr.</p> <h2>The time complexity of the merge sort:</h2> <p>Three-way merge sort equation is: T(n) = 2T(n/2) + O(n)</p> <p>Similarly, for a three-way merge sort, we have: T( n) = 3T(n/3) + O(n)</p> <p>Solving with the master method, its complexity is O(n log 3n).</p> <p>Time complexity appears less than a two-way merge sort, but the more comparisons in the merge function, the more time it might take in practice.</p> <p>So, in this article, we briefly discuss 3 ways to merge sort in c. The merge sort variant is treated as a 3-way merge sort that splits the array into three parts instead of splitting it into two parts. We also give an example that is related to this topic.</p> <hr></mid1)>

위 코드는 어떻게 작동하나요?

여기서는 먼저 통계 배열의 내용을 fArr이라는 다른 모든 배열에 복제합니다. 그런 다음 배열을 세 개의 요소로 나누는 중간점을 찾아 배열을 입력하고 모든 배열의 유형 특성을 호출합니다. 재귀의 기본 사례는 배열의 크기가 1이고 함수에서 반환되는 경우입니다. 그런 다음 배열 병합이 시작되고, 마지막으로 정렬된 배열이 fArr에 있고 gArr에 복사됩니다.

병합 정렬의 시간 복잡도:

3방향 병합 정렬 방정식은 다음과 같습니다. T(n) = 2T(n/2) + O(n)

마찬가지로 3방향 병합 정렬의 경우 다음과 같습니다. T(n) = 3T(n/3) + O(n)

마스터 방법으로 풀면 복잡도는 O(n log 3n)입니다.

시간 복잡도는 양방향 병합 정렬보다 적게 나타나지만 병합 기능에서 비교가 많을수록 실제로는 더 많은 시간이 걸릴 수 있습니다.

그래서 이번 글에서는 c에서 정렬을 병합하는 3가지 방법에 대해 간략하게 설명하겠습니다. 병합 정렬 변형은 배열을 두 부분으로 분할하는 대신 세 부분으로 분할하는 3방향 병합 정렬로 처리됩니다. 또한 이 주제와 관련된 예도 제공합니다.