<?xml version="1.0" encoding="UTF-8" standalone="yes"?><oembed><version><![CDATA[1.0]]></version><provider_name><![CDATA[The Osmosian Order of Plain English Programmers Welcomes You]]></provider_name><provider_url><![CDATA[http://osmosianplainenglishprogramming.blog]]></provider_url><author_name><![CDATA[gerryrzeppa]]></author_name><author_url><![CDATA[https://osmosianplainenglishprogramming.blog/author/gerryrzeppa/]]></author_url><title><![CDATA[A Simple Merge&nbsp;Sort]]></title><type><![CDATA[link]]></type><html><![CDATA[<header class="entry-header">
<h1 class="entry-title"></h1>
</header>
<div class="entry-content">
<p>The only compound data types native to Osmosian Plain English are records and doubly-linked lists. When we need to sort a list, we use the simple recursive merge sort that I’ll show you below. But first we need something to sort. Let’s sort fruits, and let’s begin with a type definition:</p>
<p><span style="color:#00ccff;">A fruit is a thing with a name.</span></p>
<p>When the word “thing” appears in a type definition, our compiler works a little magic behind the scenes to make things (pun intended) more powerful and flexible. The above definition, for example, actually causes the following data types to be defined:</p>
<p><img loading="lazy" class="alignnone size-full wp-image-312934" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/fruit-internals.jpg" alt="" width="504" height="360" /></p>
<p>So a Fruit is really nothing but a pointer containing the address of a Fruit Record.</p>
<p>And each 16-byte Fruit Record has a hidden 8-byte prefix with two pointers for linking these records into lists, together with the fruit’s name, which is a string. Plain English strings are stored in the Heap and can be any length. So the Name in the Fruit Record is actually just two pointers to the first and last bytes of the string on the Heap, respectively. <em>String </em>memory is managed automatically, but <em>thing </em>memory is managed by the programmer.</p>
<p>The third type generated by the compiler serves as the anchor for lists of Fruit Records. Such lists are simply (and intuitively) called Fruits, the plural of Fruit.</p>
<p>Now let’s add some fruits to a list, in random order, and sort them. Here are the top level sentences in our test program:</p>
<p><span style="color:#00ccff;">To run:</span><br />
<span style="color:#00ccff;"> Start up.</span><br />
<span style="color:#00ccff;"> Create some fruits.</span><br />
<span style="color:#00ccff;"> Write the fruits on the console.</span><br />
<span style="color:#00ccff;"> Skip a line on the console.</span><br />
<span style="color:#00ccff;"> Sort the fruits.</span><br />
<span style="color:#00ccff;"> Write the fruits on the console.</span><br />
<span style="color:#00ccff;"> Destroy the fruits.</span><br />
<span style="color:#00ccff;"> Wait for the escape key.</span><br />
<span style="color:#00ccff;"> Shut down.</span></p>
<p>And here are the routines that will be called to “Create some fruits”:</p>
<p><span style="color:#00ccff;">To create some fruits:</span><br />
<span style="color:#00ccff;"> Add &#8220;banana&#8221; to the fruits.</span><br />
<span style="color:#00ccff;"> Add &#8220;apple&#8221; to the fruits.</span><br />
<span style="color:#00ccff;"> Add &#8220;orange&#8221; to the fruits.</span><br />
<span style="color:#00ccff;"> Add &#8220;bacon&#8221; to the fruits.</span><br />
<span style="color:#00ccff;"> Add &#8220;pineapple&#8221; to the fruits.</span><br />
<span style="color:#00ccff;"> Add &#8220;pomegranate&#8221; to the fruits.</span><br />
<span style="color:#00ccff;"> Add &#8220;tomato&#8221; to the fruits.</span><br />
<span style="color:#00ccff;"> Add &#8220;grape&#8221; to the fruits.</span><br />
<span style="color:#00ccff;"> Add &#8220;fig&#8221; to the fruits.</span><br />
<span style="color:#00ccff;"> Add &#8220;date&#8221; to the fruits.</span></p>
<p><span style="color:#00ccff;">To add a name to some fruits:</span><br />
<span style="color:#00ccff;"> Allocate memory for a fruit.</span><br />
<span style="color:#00ccff;"> Put the name into the fruit&#8217;s name.</span><br />
<span style="color:#00ccff;"> Append the fruit to the fruits.</span></p>
<p>Now we’re ready to sort. This is the sort routine:</p>
<p><span style="color:#00ccff;">To sort some fruits:</span><br />
<span style="color:#00ccff;"> If the fruits&#8217; first is the fruits&#8217; last, exit.</span><br />
<span style="color:#00ccff;"> Split the fruits into some left fruits and some right fruits.</span><br />
<span style="color:#00ccff;"> Sort the left fruits.</span><br />
<span style="color:#00ccff;"> Sort the right fruits.</span><br />
<span style="color:#00ccff;"> Loop.</span><br />
<span style="color:#00ccff;"> Put the left fruits&#8217; first into a left fruit.</span><br />
<span style="color:#00ccff;"> Put the right fruits&#8217; first into a right fruit.</span><br />
<span style="color:#00ccff;"> If the left fruit is nil, append the right fruits to the fruits; exit.</span><br />
<span style="color:#00ccff;"> If the right fruit is nil, append the left fruits to the fruits; exit.</span><br />
<span style="color:#00ccff;"> If the left fruit&#8217;s name is greater than the right fruit&#8217;s name,</span><br />
<span style="color:#00ccff;"> move the right fruit from the right fruits to the fruits; repeat.</span><br />
<span style="color:#00ccff;"> Move the left fruit from the left fruits to the fruits.</span><br />
<span style="color:#00ccff;"> Repeat.</span></p>
<p>When we run this program, the output on the console looks like this:</p>
<p><img loading="lazy" class="alignnone size-full wp-image-312937" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/sort-output-1.jpg" alt="" width="504" height="555" /></p>
<p>But is it fast? Let’s see using this modified test program:</p>
<p><span style="color:#00ccff;">To run:</span><br />
<span style="color:#00ccff;"> Start up.</span><br />
<span style="color:#00ccff;"> Write &#8220;Working&#8230;&#8221; on the console.</span><br />
<span style="color:#00ccff;"> Put 10000 into a count.</span><br />
<span style="color:#00ccff;"> Create some fruits using &#8220;apple&#8221; and the count.</span><br />
<span style="color:#00ccff;"> Start a timer. Sort the fruits. Stop the timer.</span><br />
<span style="color:#00ccff;"> Write the timer then &#8221; milliseconds for &#8221; then the count on the console.</span><br />
<span style="color:#00ccff;"> Destroy the fruits.</span><br />
<span style="color:#00ccff;"> Put 100000 into the count.</span><br />
<span style="color:#00ccff;"> Create the fruits using &#8220;apple&#8221; and the count.</span><br />
<span style="color:#00ccff;"> Start the timer. Sort the fruits. Stop the timer.</span><br />
<span style="color:#00ccff;"> Write the timer then &#8221; milliseconds for &#8221; then the count on the console.</span><br />
<span style="color:#00ccff;"> Destroy the fruits.</span><br />
<span style="color:#00ccff;"> Put 1000000 into the count.</span><br />
<span style="color:#00ccff;"> Create the fruits using &#8220;apple&#8221; and the count.</span><br />
<span style="color:#00ccff;"> Start the timer. Sort the fruits. Stop the timer.</span><br />
<span style="color:#00ccff;"> Write the timer then &#8221; milliseconds for &#8221; then the count on the console.</span><br />
<span style="color:#00ccff;"> Destroy the fruits.</span><br />
<span style="color:#00ccff;"> Wait for the escape key.</span><br />
<span style="color:#00ccff;"> Shut down.</span></p>
<p>The fruits this time, at the start, look like this:</p>
<p><span style="color:#00ccff;">apple 0010000</span><br />
<span style="color:#00ccff;"> apple 0009999</span><br />
<span style="color:#00ccff;"> apple 0009998</span><br />
<span style="color:#00ccff;"> &#8230;</span></p>
<p>And the output on the console looks like this:</p>
<p><img loading="lazy" class="alignnone size-full wp-image-312938" src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/sort-output-2.jpg" alt="" width="504" height="141" /></p>
<p>Not quite linear, I admit. But not exponentially bad, either. Ten times as many records take <em>roughly </em>ten times as long to sort. There’s a technical way of saying this using big “O’s” and little “n’s’ and “logs” and stuff, but Plain English programmers don’t generally think that way. And it’s stable, as a Plain English programmer would expect — records with duplicate sort values retain their original sequence.</p>
<p>Good, simple, useful stuff.</p>
</div>
]]></html><thumbnail_url><![CDATA[https://i2.wp.com/cdncontribute.geeksforgeeks.org/wp-content/uploads/fruit-internals.jpg?fit=440%2C330&ssl=1]]></thumbnail_url><thumbnail_width><![CDATA[440]]></thumbnail_width><thumbnail_height><![CDATA[314]]></thumbnail_height></oembed>