<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[mergesort Problem]]></title><description><![CDATA[<p>Hallo,</p>
<p>fülle ein Array mit Pseudozufallszahlen und übergebe es der funktion mergesort. In der Ausgabe bekommt ich irgendwelche zahlen. Diese zahlen sind weder sortiert noch die erzeugten.</p>
<pre><code class="language-cpp">int merge(int *F1, int *F2, int n){
	int F[n*2];
	int i,k,j,t;
	i=0;
	j=0;
	k=0;
	t=0;
	/*Liste leer?*/
	while(i&lt;n &amp;&amp; j&lt;n){
		if(F1[i] &lt; F2[j]){
			t=F1[i];
			i++;	
		}else{
			t=F2[j];
			j++;			
		}	
		F[k]=t;
		k++;
	}
	/*Rest anhängen*/
	while(i&lt;n){
		F[k]=F1[i];
		k++;i++;			
	}
	while(j&lt;n){
		F[k]=F2[j];
		k++;j++;			
	}
	return *F;	
}

int mergesort(int *F, int n){
	int i,j,k;
	j=0;
	k=0;
	int F1[n/2];
	int F2[n/2]; 
	/*Divide*/
	if(n&gt;1){
		for(i=0;i&lt;n;i++){
			if(i&lt;n/2){
				F1[j]=F[i];
				j++;	
			}else{
				F2[k]=F[i];
				k++;
			}	
		}
		/*Conquer*/
		mergesort(F1,j);
		mergesort(F2,k);
		/*Combine*/
		return merge(F1, F2,n/2);
	}
	return *F;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/148856/mergesort-problem</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 01:46:36 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/148856.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 31 May 2006 08:25:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to mergesort Problem on Wed, 31 May 2006 08:25:06 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>fülle ein Array mit Pseudozufallszahlen und übergebe es der funktion mergesort. In der Ausgabe bekommt ich irgendwelche zahlen. Diese zahlen sind weder sortiert noch die erzeugten.</p>
<pre><code class="language-cpp">int merge(int *F1, int *F2, int n){
	int F[n*2];
	int i,k,j,t;
	i=0;
	j=0;
	k=0;
	t=0;
	/*Liste leer?*/
	while(i&lt;n &amp;&amp; j&lt;n){
		if(F1[i] &lt; F2[j]){
			t=F1[i];
			i++;	
		}else{
			t=F2[j];
			j++;			
		}	
		F[k]=t;
		k++;
	}
	/*Rest anhängen*/
	while(i&lt;n){
		F[k]=F1[i];
		k++;i++;			
	}
	while(j&lt;n){
		F[k]=F2[j];
		k++;j++;			
	}
	return *F;	
}

int mergesort(int *F, int n){
	int i,j,k;
	j=0;
	k=0;
	int F1[n/2];
	int F2[n/2]; 
	/*Divide*/
	if(n&gt;1){
		for(i=0;i&lt;n;i++){
			if(i&lt;n/2){
				F1[j]=F[i];
				j++;	
			}else{
				F2[k]=F[i];
				k++;
			}	
		}
		/*Conquer*/
		mergesort(F1,j);
		mergesort(F2,k);
		/*Combine*/
		return merge(F1, F2,n/2);
	}
	return *F;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1068558</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1068558</guid><dc:creator><![CDATA[booorgy]]></dc:creator><pubDate>Wed, 31 May 2006 08:25:06 GMT</pubDate></item><item><title><![CDATA[Reply to mergesort Problem on Wed, 31 May 2006 08:28:53 GMT]]></title><description><![CDATA[<p>Du mergest die beiden Felder in ein lokales Array (das am Ende der Funktion merge() wieder zerstört wird) und gibst dann das erste Element dieses Arrays wieder zurück. So kann das nicht funktionieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1068561</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1068561</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Wed, 31 May 2006 08:28:53 GMT</pubDate></item><item><title><![CDATA[Reply to mergesort Problem on Wed, 31 May 2006 08:31:49 GMT]]></title><description><![CDATA[<p>kannst du mir da weiterhelfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1068563</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1068563</guid><dc:creator><![CDATA[booorgy]]></dc:creator><pubDate>Wed, 31 May 2006 08:31:49 GMT</pubDate></item><item><title><![CDATA[Reply to mergesort Problem on Wed, 31 May 2006 08:36:44 GMT]]></title><description><![CDATA[<p>Du musst das Zielarray als Parameter an merge übergeben. Außerdem kannst du dich nicht darauf verlassen, dass F1 und F2 gleich groß sind. Eine Größenangabe für beide reicht also nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1068566</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1068566</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Wed, 31 May 2006 08:36:44 GMT</pubDate></item><item><title><![CDATA[Reply to mergesort Problem on Wed, 31 May 2006 09:06:42 GMT]]></title><description><![CDATA[<p>warum muss ich ein zielarray übergegen? müsste doch das array der funktion zurückbekommen.<br />
wie kann ich es sonst machen außer mit der länge? kann man abfragen ob array == leer ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1068589</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1068589</guid><dc:creator><![CDATA[booorgy]]></dc:creator><pubDate>Wed, 31 May 2006 09:06:42 GMT</pubDate></item><item><title><![CDATA[Reply to mergesort Problem on Wed, 31 May 2006 09:07:44 GMT]]></title><description><![CDATA[<p>booorgy schrieb:</p>
<blockquote>
<p>warum muss ich ein zielarray übergegen?</p>
</blockquote>
<p>Weil du kein Array zurückgeben kannst.</p>
<blockquote>
<p>wie kann ich es sonst machen außer mit der länge? kann man abfragen ob array == leer ist?</p>
</blockquote>
<p>Nein. Übergib doch einfach zwei ints.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1068590</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1068590</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Wed, 31 May 2006 09:07:44 GMT</pubDate></item><item><title><![CDATA[Reply to mergesort Problem on Wed, 31 May 2006 11:06:24 GMT]]></title><description><![CDATA[<p>Zudem hat deine Lösung auch noch das Problem, dass es ungrade Zahlen abschneidet. Wenn 7 übergeben wird, werden nur 6 davon behandelt. Das ist wegen dem zweimal n/2 ergibt bei 7 zweimal 3 also 6.</p>
<p>Und so geht es wirklich:</p>
<pre><code class="language-cpp">void Merge(int* LeftArray, int* RightArray, int* ResultArray, int nLeft, int nRight)
{
    int nL = 0, nR = 0, nResult = 0;
    while(nL &lt; nLeft &amp;&amp; nR &lt; nRight)
    {
        if(LeftArray[nL] &gt; RightArray[nR])
        {
            ResultArray[nResult] = RightArray[nR];
            nR++; nResult++;
        }
        else
        {
            ResultArray[nResult] = LeftArray[nL];
            nL++; nResult++;
        }
    }
    while(nL &lt; nLeft)
    {
        ResultArray[nResult] = LeftArray[nL];
        nL++; nResult++;
    }
    while(nR &lt; nRight)
    {
        ResultArray[nResult] = RightArray[nR];
        nR++; nResult++;
    }
}

void MergeSort(int* Array, int nCount)
{
    if (nCount &gt; 1)
    {
        int nLeftCount = (nCount / 2);
        int nRightCount = (nCount - nLeftCount);
        int LeftArray[nLeftCount];
        int RightArray[nRightCount];

        for(int i = 0; i &lt; nLeftCount; i++)
        { LeftArray[i] = Array[i]; }

        for(int i = 0; i &lt;nRightCount; i++)
        { RightArray[i] = pArray[i + nLeftCount]; }

        MergeSort(LeftArray, nLeftCount);
        MergeSort(RightArray, nRightCount);
        Merge(LeftArray, RightArray, Array, nLeftCount, nRightCount);
    }
}
</code></pre>
<p>Code ist nur hier reingeschrieben. Ich gebe keine Garantie, dass es keine Fehler drin hat. Aber in der Art sollte es eigentlich gehen.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1068681</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1068681</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Wed, 31 May 2006 11:06:24 GMT</pubDate></item></channel></rss>