<?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[BubbleSort]]></title><description><![CDATA[<p>hi</p>
<p>ich habe eine Funktion für BubbleSort (absteigendes Sortieren)</p>
<pre><code>void bubblesort(vector&lt;pair_saving&gt;&amp; vps)
{
	//Diese Funktion führt eine absteigende Sortierung durch.

	for (unsigned int i=0; i&lt;vps.size(); i++)
	{
		for (unsigned int j=vps.size()-1; j&gt;i; j--)
		{
			if (vps[j].savings &gt; vps[j-1].savings)
			{
				pair_saving tmp;
				tmp = vps[j];
				vps[j] = vps[j-1];
				vps[j-1] = tmp;
			}
		}
	}
}
</code></pre>
<p>das Problem ist das bei 300 Knoten die 2. For schleife schon 1 Mrd mal durchlaufen wird und deswegen der Zeitaufwand zu groß ist. Hat jemand einen Tip, wie ich die Sortierung schneller machen könnte ?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/322537/bubblesort</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Jul 2026 03:21:45 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/322537.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 26 Dec 2013 18:29:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to BubbleSort on Thu, 26 Dec 2013 18:29:05 GMT]]></title><description><![CDATA[<p>hi</p>
<p>ich habe eine Funktion für BubbleSort (absteigendes Sortieren)</p>
<pre><code>void bubblesort(vector&lt;pair_saving&gt;&amp; vps)
{
	//Diese Funktion führt eine absteigende Sortierung durch.

	for (unsigned int i=0; i&lt;vps.size(); i++)
	{
		for (unsigned int j=vps.size()-1; j&gt;i; j--)
		{
			if (vps[j].savings &gt; vps[j-1].savings)
			{
				pair_saving tmp;
				tmp = vps[j];
				vps[j] = vps[j-1];
				vps[j-1] = tmp;
			}
		}
	}
}
</code></pre>
<p>das Problem ist das bei 300 Knoten die 2. For schleife schon 1 Mrd mal durchlaufen wird und deswegen der Zeitaufwand zu groß ist. Hat jemand einen Tip, wie ich die Sortierung schneller machen könnte ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373767</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373767</guid><dc:creator><![CDATA[ide]]></dc:creator><pubDate>Thu, 26 Dec 2013 18:29:05 GMT</pubDate></item><item><title><![CDATA[Reply to BubbleSort on Thu, 26 Dec 2013 18:38:36 GMT]]></title><description><![CDATA[<blockquote>
<p>Hat jemand einen Tip, wie ich die Sortierung schneller machen könnte ?</p>
</blockquote>
<p>Ganz einfach: Indem du nicht Bubblesort benutzt. Das ist eher eine Art Scherzsortierfunktion von Informatiklehrern, damit man da dran demonstrieren kann, wie man es <em>nicht</em> macht.</p>
<pre><code>#include &lt;algorithm&gt;
#include &lt;functional&gt;

// ...

std::sort(daten.begin(), daten.end(), std::greater&lt;datentyp&gt;());
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2373769</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373769</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 26 Dec 2013 18:38:36 GMT</pubDate></item><item><title><![CDATA[Reply to BubbleSort on Thu, 26 Dec 2013 18:42:14 GMT]]></title><description><![CDATA[<p>Vielen Dank. Hatte ich mir schon fast gedacht. Ich habe mir jetzt sort angeschaut. Wenn ich es richtig verstehe sortiert der die Werte dann aber aufsteigend ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373772</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373772</guid><dc:creator><![CDATA[ide]]></dc:creator><pubDate>Thu, 26 Dec 2013 18:42:14 GMT</pubDate></item><item><title><![CDATA[Reply to BubbleSort on Thu, 26 Dec 2013 18:45:34 GMT]]></title><description><![CDATA[<p>ide schrieb:</p>
<blockquote>
<p>Vielen Dank. Hatte ich mir schon fast gedacht. Ich habe mir jetzt sort angeschaut. Wenn ich es richtig verstehe sortiert der die Werte dann aber aufsteigend ?</p>
</blockquote>
<p>gibt ja einen comparator den du selbst bestimmen kannst. (siehe seppj's code)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373773</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373773</guid><dc:creator><![CDATA[Fytch]]></dc:creator><pubDate>Thu, 26 Dec 2013 18:45:34 GMT</pubDate></item><item><title><![CDATA[Reply to BubbleSort on Thu, 26 Dec 2013 19:05:04 GMT]]></title><description><![CDATA[<p>ich habe in dem Vector vps eine Structur verwendet:</p>
<pre><code>struct pair_saving
{
	int o;
	int d;
	double savings;
};
</code></pre>
<p>und nach diesem savings wert soll sortiert werden. Kann es sein das es nicht funktioniert mit vps.savings.begin() etc. ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373775</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373775</guid><dc:creator><![CDATA[ide]]></dc:creator><pubDate>Thu, 26 Dec 2013 19:05:04 GMT</pubDate></item><item><title><![CDATA[Reply to BubbleSort on Thu, 26 Dec 2013 19:10:23 GMT]]></title><description><![CDATA[<p>ide schrieb:</p>
<blockquote>
<p>ich habe in dem Vector vps eine Structur verwendet:</p>
<pre><code>struct pair_saving
{
	int o;
	int d;
	double savings;
};
</code></pre>
<p>und nach diesem savings wert soll sortiert werden. Kann es sein das es nicht funktioniert mit vps.savings.begin() etc. ?</p>
</blockquote>
<p>überleg dir halt mal ob instanzen des types double wirklich eine methode &quot;begin()&quot;. ich bezweifle es. vps hat jedoch sehr wohl eine solche. wie du das lösen kannst? schreib einen comparator der die member &quot;savings&quot; der vektor-einträge (bzw. der iterator-pointees) vergleicht. oder du bietest operatoren für deinen typen an und verwendest dann den naiven ansatz von seppj.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373776</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373776</guid><dc:creator><![CDATA[Fytch]]></dc:creator><pubDate>Thu, 26 Dec 2013 19:10:23 GMT</pubDate></item><item><title><![CDATA[Reply to BubbleSort on Thu, 26 Dec 2013 19:13:12 GMT]]></title><description><![CDATA[<p>Doch, std::sort funktioniert mit allem. Du musst bloß eine passende Vergleichsfunktion angeben. Zum Beispiel so:</p>
<pre><code>bool comp_pair_saving_greater (const pair_saving&amp; lhs, const pair_saving&amp; rhs) 
{
  return lhs.savings &gt; rhs.savings;
}

// ...

std::vector&lt;pair_saving&gt; data;

// ...

std::sort(data.begin(), data.end(), comp_pair_saving_greater);
</code></pre>
<p>Oder</p>
<pre><code>bool operator&gt;(const pair_saving&amp; lhs, const pair_saving&amp; rhs) 
{
  return lhs.savings &gt; rhs.savings;
}

// ...

std::sort(data.begin(), data.end(), std::greater&lt;pair_saving&gt;());
</code></pre>
<p>Oder</p>
<pre><code>std::sort(data.begin(), data.end(), [](const pair_saving&amp; lhs, const pair_saving&amp; rhs) { return lhs.savings &gt; rhs.savings;});
</code></pre>
<p>(Alle ungetestet, mögliche Tipp-/Syntaxfehler dienen der Übung des Lesers <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> )</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373777</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373777</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 26 Dec 2013 19:13:12 GMT</pubDate></item><item><title><![CDATA[Reply to BubbleSort on Thu, 26 Dec 2013 19:16:26 GMT]]></title><description><![CDATA[<p>vielen vielen dank.</p>
<p>Ich werde mich morgen mal dran versuchen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2373778</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2373778</guid><dc:creator><![CDATA[ide]]></dc:creator><pubDate>Thu, 26 Dec 2013 19:16:26 GMT</pubDate></item></channel></rss>