logo

Ordinamento di unione a 3 vie in c

Questo articolo discute 3 modi per unire l'ordinamento in c. Nel merge sort, l'array viene diviso ricorsivamente in due parti, ordinato e infine unito.

La variante dell'ordinamento di unione viene considerata come un ordinamento di unione a 3 vie che divide l'array in tre parti invece di dividerlo in due parti. L'ordinamento di unione divide ricorsivamente un array in sottoarray di dimensioni dimezzate. Allo stesso modo, un merge sort a tre vie decompone un array in sottoarray di un terzo della dimensione.

Nel merge sort, l'array viene diviso ricorsivamente in due parti, ordinato e infine unito. Una variante del merge sort è chiamata 3-way merge sort, dove invece di dividere l'array in due parti, lo divide in tre parti.

Esempi di ordinamento di unione: L'esempio di merge sort è riportato di seguito:

 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 

La complessità temporale di un merge sort a tre vie è nlog3n.

Esempio 1: Qui, diamo un esempio di 3 modi per unire l'ordinamento in c. L'esempio è riportato di seguito:

 #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)>

Come funziona il codice sopra?

Qui innanzitutto replichiamo il contenuto dell'array delle statistiche in ogni altro array chiamato fArr. Quindi digitare l'array individuando il punto medio che divide l'array in tre elementi e richiama la caratteristica del tipo su ogni array. Il caso base di ricorsione si verifica quando un array ha dimensione 1 e viene restituito da una funzione. Quindi inizia la fusione dell'array e, infine, l'array ordinato è in fArr e copiato in gArr.

La complessità temporale del merge sort:

L'equazione del merge sort a tre vie è: T(n) = 2T(n/2) + O(n)

Allo stesso modo, per un merge sort a tre vie, abbiamo: T( n) = 3T(n/3) + O(n)

Risolvendo con il metodo master, la sua complessità è O(n log 3n).

La complessità temporale sembra inferiore a quella di un ordinamento di unione bidirezionale, ma maggiori sono i confronti nella funzione di unione, maggiore è il tempo necessario nella pratica.

Quindi, in questo articolo, discutiamo brevemente 3 modi per unire l'ordinamento in c. La variante dell'ordinamento di unione viene considerata come un ordinamento di unione a 3 vie che divide l'array in tre parti invece di dividerlo in due parti. Forniamo anche un esempio relativo a questo argomento.