<?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[rotate algorithm]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe versucht std::rotate zu implementieren, der code ist mehr c style as c++...aber egal.<br />
kann man den algo noch verbessern?</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
using namespace std;

void my_rotate(vector&lt;int&gt; &amp;num, int pos) {
	bool rotate_left = false;

	if(pos &lt; 0) {
		rotate_left = true;
	}

	pos = abs(pos);
	pos = pos % num.size();

	if(rotate_left) {
		int index_i = 0;
		int index_j = pos;

		while(index_j &lt; num.size()) {
			swap(num[index_i], num[index_j]);

			index_i++;
			index_j++;
		}		
	}
	else {	
		int last_index = num.size() - 1;
		int index_i = last_index;
		int index_j = last_index - pos;

		while(index_j &gt;= 0) {
			swap(num[index_i], num[index_j]);

			index_i--;
			index_j--;
		}
	}
}

int main() {
	vector&lt;int&gt; vec1 = {1, 1, 4, 7, 10, 13};

	for_each(vec1.begin(), vec1.end(), [](int val){ cout &lt;&lt; val &lt;&lt; ' ';});
	cout &lt;&lt; '\n';

	my_rotate(vec1, -2);

	for_each(vec1.begin(), vec1.end(), [](int val){ cout &lt;&lt; val &lt;&lt; ' ';});

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/325158/rotate-algorithm</link><generator>RSS for Node</generator><lastBuildDate>Mon, 13 Jul 2026 00:03:32 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/325158.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 17 Apr 2014 09:18:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to rotate algorithm on Thu, 17 Apr 2014 09:18:13 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich habe versucht std::rotate zu implementieren, der code ist mehr c style as c++...aber egal.<br />
kann man den algo noch verbessern?</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
using namespace std;

void my_rotate(vector&lt;int&gt; &amp;num, int pos) {
	bool rotate_left = false;

	if(pos &lt; 0) {
		rotate_left = true;
	}

	pos = abs(pos);
	pos = pos % num.size();

	if(rotate_left) {
		int index_i = 0;
		int index_j = pos;

		while(index_j &lt; num.size()) {
			swap(num[index_i], num[index_j]);

			index_i++;
			index_j++;
		}		
	}
	else {	
		int last_index = num.size() - 1;
		int index_i = last_index;
		int index_j = last_index - pos;

		while(index_j &gt;= 0) {
			swap(num[index_i], num[index_j]);

			index_i--;
			index_j--;
		}
	}
}

int main() {
	vector&lt;int&gt; vec1 = {1, 1, 4, 7, 10, 13};

	for_each(vec1.begin(), vec1.end(), [](int val){ cout &lt;&lt; val &lt;&lt; ' ';});
	cout &lt;&lt; '\n';

	my_rotate(vec1, -2);

	for_each(vec1.begin(), vec1.end(), [](int val){ cout &lt;&lt; val &lt;&lt; ' ';});

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2394993</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2394993</guid><dc:creator><![CDATA[mike1]]></dc:creator><pubDate>Thu, 17 Apr 2014 09:18:13 GMT</pubDate></item><item><title><![CDATA[Reply to rotate algorithm on Thu, 17 Apr 2014 09:48:51 GMT]]></title><description><![CDATA[<p>Welches Ziel verfolgst du?</p>
<p>Wenn du einfach mal dich zu Übungszwecken an std::rotate versuche willst, ok, das hast du hiermit getan. Wenn du was besseres willst, dann recherchier mehr/besser.</p>
<p>Willst du einen Algorithmus für den Produktiveinsatz, dann nimm std::rotate oder gib stichhaltige Gründe an, was dagegen spricht.</p>
<p>Your Turn...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2395006</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2395006</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Thu, 17 Apr 2014 09:48:51 GMT</pubDate></item><item><title><![CDATA[Reply to rotate algorithm on Thu, 17 Apr 2014 09:58:05 GMT]]></title><description><![CDATA[<p>Deine Funktion ist nicht vergleichbar mit std::rotate .</p>
<p>du übergibst z.B. einen std::vector, welcher &lt;int&gt; hält. Schau dir mal an was da std::rotate macht. Was für Daten dort übergeben werden und wie damit umgegangen wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2395009</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2395009</guid><dc:creator><![CDATA[ichhoffedasistrichtig]]></dc:creator><pubDate>Thu, 17 Apr 2014 09:58:05 GMT</pubDate></item><item><title><![CDATA[Reply to rotate algorithm on Thu, 17 Apr 2014 10:19:07 GMT]]></title><description><![CDATA[<p>meine frage war, wie man die zeitkomplexitaet des algos verbessert.</p>
<p>ich weiss wohl das rotate iteratoren nimmt...</p>
<p>ist nur ne vorbereitung fuer eine interview...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2395027</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2395027</guid><dc:creator><![CDATA[mike1]]></dc:creator><pubDate>Thu, 17 Apr 2014 10:19:07 GMT</pubDate></item><item><title><![CDATA[Reply to rotate algorithm on Thu, 17 Apr 2014 11:00:43 GMT]]></title><description><![CDATA[<p>Ist Deine Funktion wirklich korrekt?</p>
<p>Ich würde noch in Erwähnung bringen den Zwei-Hände-Trick, den Jon Bentley in Programming Pearls zeigt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2395043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2395043</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 17 Apr 2014 11:00:43 GMT</pubDate></item><item><title><![CDATA[Reply to rotate algorithm on Thu, 17 Apr 2014 11:26:27 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Ich würde noch in Erwähnung bringen den Zwei-Hände-Trick, den Jon Bentley in Programming Pearls zeigt.</p>
</blockquote>
<p>Warum nicht den Block-Swap Algo?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2395053</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2395053</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Thu, 17 Apr 2014 11:26:27 GMT</pubDate></item><item><title><![CDATA[Reply to rotate algorithm on Thu, 17 Apr 2014 11:39:46 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/106">@volkard</a>: meinst du den hier?</p>
<pre><code>Algorithm:

rotate(arr[], d, n)
  reverse(arr[], 1, d) ;
  reverse(arr[], d + 1, n);
  reverse(arr[], l, n);

Let AB are the two parts of the input array where A = arr[0..d-1] and B = arr[d..n-1]. The idea of the algorithm is:
Reverse A to get ArB. /* Ar is reverse of A */
Reverse B to get ArBr. /* Br is reverse of B */
Reverse all to get (ArBr) r = BA.

For arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2 and n = 7

A = [1, 2] and B = [3, 4, 5, 6, 7]

Reverse A, we get ArB = [2, 1, 3, 4, 5, 6, 7]
Reverse B, we get ArBr = [2, 1, 7, 6, 5, 4, 3]
Reverse all, we get (ArBr)r = [3, 4, 5, 6, 7, 1, 2]
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2395057</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2395057</guid><dc:creator><![CDATA[mike1]]></dc:creator><pubDate>Thu, 17 Apr 2014 11:39:46 GMT</pubDate></item><item><title><![CDATA[Reply to rotate algorithm on Thu, 17 Apr 2014 11:43:26 GMT]]></title><description><![CDATA[<p>mike1 schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/106">@volkard</a>: meinst du den hier?</p>
</blockquote>
<p>Ja.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2395059</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2395059</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 17 Apr 2014 11:43:26 GMT</pubDate></item></channel></rss>