<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ger Moren</title>
    <description>The latest articles on Forem by Ger Moren (@germoren).</description>
    <link>https://forem.com/germoren</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F866827%2F9df96b99-5f00-4467-a6f1-52d62e793d4c.jpeg</url>
      <title>Forem: Ger Moren</title>
      <link>https://forem.com/germoren</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/germoren"/>
    <language>en</language>
    <item>
      <title>BubbleSort en Typescript</title>
      <dc:creator>Ger Moren</dc:creator>
      <pubDate>Tue, 24 May 2022 01:20:20 +0000</pubDate>
      <link>https://forem.com/germoren/bubblesort-en-typescript-30l4</link>
      <guid>https://forem.com/germoren/bubblesort-en-typescript-30l4</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Fir5PtT9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02vav0lo1x6bwkwuo5af.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fir5PtT9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/02vav0lo1x6bwkwuo5af.png" alt="BubbleSort in Javascript" width="846" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NAMESPACES&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Se crea el &lt;code&gt;namespace MathUtils&lt;/code&gt; con los distintos types auxiliares para el método BubbleSort:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4AkuzGFC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7b1s9g11cxk6pix3scx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4AkuzGFC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7b1s9g11cxk6pix3scx.png" alt="Num, Length, Push NTuple and Add types" width="880" height="626"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;En el caso de la resta importa el orden en que se reciben los paramentos, por este motivo el primer parámetro debe ser mayor o igual que el segundo: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6cc2GWO8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4kwss7dvldqb75cncd3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6cc2GWO8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4kwss7dvldqb75cncd3.png" alt="Substract type" width="880" height="604"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Si queremos comparar 2 números en Typescript, los símbolos "&amp;lt;" y&lt;br&gt;
"&amp;gt;" no son validos&lt;br&gt;
&lt;code&gt;type isMinor = 1 &amp;gt; 2 ? true : false;&lt;/code&gt;&lt;br&gt;
por este motivo vamos a crear el &lt;code&gt;type GreaterComparison&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3nlANHIf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbarmaw9gjtmdmjb3r93.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3nlANHIf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbarmaw9gjtmdmjb3r93.png" alt="Type GreatComparison" width="880" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;BUBBLE SORT&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
Ayudándonos del &lt;code&gt;namespace MathUtils&lt;/code&gt; definido anteriormente procedemos a definir el &lt;code&gt;type BubbleSort&lt;/code&gt; el cual recibe un array como primer parámetro y una variable auxiliar "actual".&lt;br&gt;
Si el array tiene un solo elemento se retorna el mismo array.&lt;/p&gt;

&lt;p&gt;Caso contrario extendemos un array en el que se infieren 3 elementos, los números a comparar y lo que queda del resto.&lt;/p&gt;

&lt;p&gt;Se llama recursivamente al &lt;code&gt;type BubbleSort&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eB_cZDPR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q64pnd87cpgq1dwh9fno.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eB_cZDPR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q64pnd87cpgq1dwh9fno.png" alt="Bubble Sort Method" width="880" height="951"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type example1 = BubbleSort&amp;lt;[234, 43, 55, 63, 5, 6, 235, 547]&amp;gt;;
// [ 5, 6, 43, 55, 63, 234, 235, 547]
type example2 = BubbleSort&amp;lt;[5, 2, 4, 6, 1, 3]&amp;gt;;
// [1, 2, 3, 4, 5, 6]
type example3 = BubbleSort&amp;lt;[1]&amp;gt;;
// [1]
type example4 = BubbleSort&amp;lt;[]&amp;gt;;
// []
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>typescript</category>
      <category>sort</category>
      <category>bubblesort</category>
      <category>typescriptadvanced</category>
    </item>
  </channel>
</rss>
