logo

Cでの3ウェイマージソート

この記事では、c でソートをマージする 3 つの方法について説明します。マージ ソートでは、配列が再帰的に 2 つの部分に分割され、ソートされ、最後にマージされます。

マージ ソート バリアントは、配列を 2 つの部分に分割するのではなく、3 つの部分に分割する 3 方向のマージ ソートとして扱われます。マージソートは、配列を半分のサイズのサブ配列に再帰的に分割します。同様に、3 方向のマージ ソートは配列を 3 分の 1 のサイズの部分配列に分解します。

マージ ソートでは、配列が再帰的に 2 つの部分に分割され、ソートされ、最後にマージされます。マージ ソートの変形は 3-way マージ ソートと呼ばれ、配列を 2 つの部分に分割する代わりに、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 と呼ばれる他のすべての配列に複製します。次に、配列を 3 つの要素に分割する中点を見つけて配列を型指定し、各配列の型特性を呼び出します。再帰の基本的なケースは、配列のサイズが 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 つの方法について簡単に説明します。マージ ソート バリアントは、配列を 2 つの部分に分割するのではなく、3 つの部分に分割する 3 方向のマージ ソートとして扱われます。このトピックに関連する例も示します。