logo

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 

التعقيد الزمني لفرز الدمج الثلاثي هو nlog3n.

مثال 1: هنا، نعطي مثالا على 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.

التعقيد الزمني لفرز الدمج:

معادلة فرز الدمج الثلاثي هي: T(n) = 2T(n/2) + O(n)

وبالمثل، بالنسبة لفرز الدمج الثلاثي، لدينا: T( n) = 3T(n/3) + O(n)

الحل بالطريقة الرئيسية، تعقيدها هو O(n log 3n).

يبدو التعقيد الزمني أقل من نوع الدمج ثنائي الاتجاه، ولكن كلما زادت المقارنات في وظيفة الدمج، زاد الوقت الذي قد يستغرقه الأمر في الممارسة العملية.

لذلك، في هذه المقالة، نناقش بإيجاز 3 طرق لدمج الفرز في ج. يتم التعامل مع متغير فرز الدمج على أنه فرز دمج ثلاثي الاتجاهات يقسم المصفوفة إلى ثلاثة أجزاء بدلاً من تقسيمها إلى جزأين. ونقدم أيضًا مثالًا يتعلق بهذا الموضوع.