<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Algorithms on Lin's Blog</title><link>https://coder.tw/en/tags/algorithms/</link><description>Recent content in Algorithms on Lin's Blog</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Sun, 05 Jun 2011 01:00:07 +0000</lastBuildDate><atom:link href="https://coder.tw/en/tags/algorithms/index.xml" rel="self" type="application/rss+xml"/><item><title>Shuffle Algorithm (Swap Method)</title><link>https://coder.tw/en/posts/shuffle-algorithm-swap-method/</link><pubDate>Sun, 05 Jun 2011 01:00:07 +0000</pubDate><guid>https://coder.tw/en/posts/shuffle-algorithm-swap-method/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/06/2011-06-05_005920.jpg"&gt;&lt;img class="size-large wp-image-534 aligncenter" title="2011-06-05_005920" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/06/2011-06-05_005920-540x352.jpg" alt="" width="540" height="352" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The shuffle algorithm is a way to generate random numbers that never repeat. For example, suppose you need to generate ten random numbers, and all ten must be &amp;ldquo;unique.&amp;rdquo; Many people&amp;rsquo;s first instinct is to call the rand function to get a random value, store it in an array, and compare it against the numbers already in the array. That method is simple, but its performance is very poor. The shuffle algorithm introduced here is worlds apart from that approach in terms of performance. The principle behind the shuffle algorithm is actually very simple: first store the numbers in an array, then scramble them.&lt;/p&gt;</description></item><item><title>[C++] Reversing Numbers and Strings (Applications: Type Conversion, String Handling, Loops)</title><link>https://coder.tw/en/posts/c-reverse-number-and-string/</link><pubDate>Sun, 06 Mar 2011 11:18:22 +0000</pubDate><guid>https://coder.tw/en/posts/c-reverse-number-and-string/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/03/2011-03-06_111510.jpg"&gt;&lt;img class="size-large wp-image-342 aligncenter" title="2011-03-06_111510" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/03/2011-03-06_111510-540x382.jpg" alt="" width="540" height="382" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This is a fairly simple problem: it just reverses a number or a string. For example, input "linroex" and the output is "xeornil", or input 123 and get 321. The solution isn't hard. In C++, you can access a string like an array, so we set up a loop whose end value is the length of the input string. On each pass, we move the last remaining character of string X to the first position of string Y. When the loop finishes, the reversal is complete.&lt;/p&gt;
&lt;p&gt;</description></item><item><title>[VB] [C++] Calculating n! (Factorial)</title><link>https://coder.tw/en/posts/vb-c-factorial-n-2/</link><pubDate>Wed, 05 Jan 2011 05:58:10 +0000</pubDate><guid>https://coder.tw/en/posts/vb-c-factorial-n-2/</guid><description>&lt;p&gt;&lt;a href="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/01/2011-01-05_054100.jpg"&gt;&lt;img class="alignnone size-large wp-image-4369" title="2011-01-05_054100" src="https://pub-6c026d2742d14927bfa2d850c31eac91.r2.dev/wp-content/uploads/2011/01/2011-01-05_054100-520x253.jpg" alt="" width="520" height="253" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Calculating the factorial of n is very simple. I wrote this program for permutations and combinations: I wanted a rough idea of how many possible answers there would be, so I wrote an n! function to compute the factorial value. A factorial is 1&lt;em&gt;2&lt;/em&gt;3&amp;hellip;..&lt;em&gt;n. In a permutation of 5 items, the result is at most 5&lt;/em&gt;4&lt;em&gt;3&lt;/em&gt;2*1, which means there are at most 120 possible answers.&lt;/p&gt;
&lt;p&gt;Below, I&amp;rsquo;ve made n! into a Function, with a VB version and a C++ version. People who are just starting to learn loops can use this function to practice how loops work. Also, because I was worried that an integer wouldn&amp;rsquo;t be able to hold the result, I used a string to return the value. When you use this Function, you need to convert the return value to a numeric type first.&lt;/p&gt;
&lt;h2&gt;</description></item></channel></rss>