<?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>DEV Community: Gabriel Mathias</title>
    <description>The latest articles on DEV Community by Gabriel Mathias (@gablemathias).</description>
    <link>https://dev.to/gablemathias</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%2F803562%2F1b902140-7ea5-4d4a-828b-ce863009f6b5.jpeg</url>
      <title>DEV Community: Gabriel Mathias</title>
      <link>https://dev.to/gablemathias</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gablemathias"/>
    <language>en</language>
    <item>
      <title>Bubble Sort Algorithm</title>
      <dc:creator>Gabriel Mathias</dc:creator>
      <pubDate>Sat, 14 Dec 2024 23:48:33 +0000</pubDate>
      <link>https://dev.to/gablemathias/bubble-sort-algorithm-2a7f</link>
      <guid>https://dev.to/gablemathias/bubble-sort-algorithm-2a7f</guid>
      <description>&lt;p&gt;The Bubble Sort is an algorithm that sorts the array from the lowest to the highest value. Learning it is quite useful when starting to learn about algorithms, even though there's not many use cases for it. &lt;br&gt;
My goal here is a full comprehension of the algorithm and be able to explain how this works and how I implemented that. &lt;br&gt;
It has a O(n²) complexity on average &lt;a href="https://www.bigocheatsheet.com/" rel="noopener noreferrer"&gt;Big-O Chart Complexity&lt;/a&gt;, but it is reaaaally fast when you have an Array that is nearly sorted. &lt;/p&gt;
&lt;h2&gt;
  
  
  So how it works?
&lt;/h2&gt;

&lt;p&gt;We loop through the array as many times as it has values inside it, so...&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;One value at a time;&lt;/li&gt;
&lt;li&gt;If the current value is higher than the next value, we swap them. The highest values will come last;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7nh58pubq1lro4lq5g0q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7nh58pubq1lro4lq5g0q.png" alt="Bubble Sort" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0d09e7ydvj5al6z7y9f7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0d09e7ydvj5al6z7y9f7.png" alt="Process" width="508" height="660"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's possible to check that with the first passing through, the number 9 changed its place with the number 1, and all the other values stayed the same.&lt;br&gt;
On the second loop, something like that will happen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[3, 1, 9, 10, 23]
[1, 3, 9, 10, 23]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we're done, our array is sorted. &lt;/p&gt;

&lt;h2&gt;
  
  
  Coding
&lt;/h2&gt;

&lt;p&gt;You remember what I said before that the loop will go through as many times as it has values inside the array, so it is equivalent to the array size. But we also have a wise way of implementing the bubble sort in a way that, with nearly sorted arrays - like ours - the loop will stop as soon as no more items were sorted inside it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;### basic version&lt;/span&gt;
&lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;looping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;# The index starts at 0, so we fix the size for that&lt;/span&gt;

&lt;span class="n"&gt;looping&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;current_loop&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;looping&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;current_loop&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="c1"&gt;# Having nested loops makes it a O(n²)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
      &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;                   &lt;span class="c1"&gt;# Create a temporary variable to hold the value.&lt;/span&gt;
      &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;### optimised version&lt;/span&gt;

&lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;looping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;# The index starts at 0, so we fix the size for that&lt;/span&gt;

&lt;span class="n"&gt;looping&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;current_loop&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="n"&gt;swapped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;

  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;looping&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;current_loop&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="c1"&gt;# Having nested loops makes it a O(n²)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
      &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;                   &lt;span class="c1"&gt;# Create a temporary variable to hold the value&lt;/span&gt;
      &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;
      &lt;span class="n"&gt;swapped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;break&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="n"&gt;swapped&lt;/span&gt; &lt;span class="c1"&gt;# We stop the loop as soon as any changes have been made&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Java version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Arrays&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Integer&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="o"&gt;};&lt;/span&gt;

        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="c1"&gt;// Times that the loop will happen&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="n"&gt;swapped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

            &lt;span class="c1"&gt;// Compare and swap&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt;  &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;//&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
                    &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
                    &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                    &lt;span class="n"&gt;swapped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;swapped&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another useful resource about Sorting Algorithms is this &lt;a href="https://www.toptal.com/developers/sorting-algorithms" rel="noopener noreferrer"&gt;website&lt;/a&gt; that "[...] illustrate how effectively data sets from different starting points can be sorted using different algorithms."&lt;/p&gt;

&lt;p&gt;Feel free to add any suggestions and comments. &lt;br&gt;
Thank you 👋&lt;/p&gt;

</description>
      <category>algorithms</category>
    </item>
    <item>
      <title>Black Box Testing</title>
      <dc:creator>Gabriel Mathias</dc:creator>
      <pubDate>Thu, 05 Dec 2024 16:58:05 +0000</pubDate>
      <link>https://dev.to/gablemathias/black-box-testing-53fd</link>
      <guid>https://dev.to/gablemathias/black-box-testing-53fd</guid>
      <description>&lt;p&gt;Software Testing is not about - just - finding bugs. Certainly it is important to catch them. But, at the end of the day, the most important aspect is to have a useful software for your end-user. Business is about having customers consuming your product, right?&lt;br&gt;
So, always keep in mind that when you are testing (or building) software, some functionally can look obvious to you or to how you use it, but not to the end-user.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is black box?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;It is a system that produces results without the user being able to see or understand how it works. - Cambridge Dictionary&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In a testing perspective, it serves us to check how an app works from a non-technical end-user point of view, so basically, how a user is actually USING it or how it was SUPPOSED to be used.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2no7rg9yu9fq1452ml2s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2no7rg9yu9fq1452ml2s.png" alt="input-blackbox-output" width="569" height="184"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The tester checks the software functionalities without any concerns of internal details or implementation strategies. They simply provide the input (by initiating different user actions) and observe the output (response time, usability, reliability) generated by the system.&lt;/p&gt;

&lt;p&gt;That's why it's important for the tester to know the end-user, maybe have a talk with them. A solid knowledge about the end-user is key.&lt;br&gt;
The tester doesn't need to be part of the development team if he knows what are the system specifications and requirements of the app to work. &lt;br&gt;
This test can cover a lot of aspects but the primary intention is to check the&lt;br&gt;
outputs without considering what's happening after the inputs. &lt;br&gt;
I highly recommend someone that at least knows what is going on with the product or have a certain experience using it, just knowing the specifications may be harmful in some situations when the system is an ERP, for example, and even the customer uses just a few functionalities inside it in a day-to-day basis. &lt;br&gt;
From my perspective, not knowing anything and going completely isn't ideal, since perceived errors are prone to happen when actually it wasn't an error after all, "Hey I am not sure if clicking here was supposed to open this window instead of that one". &lt;/p&gt;

&lt;h2&gt;
  
  
  When to use it?
&lt;/h2&gt;

&lt;p&gt;A black-box test incorporates different forms, The most common ones are testing its functionalities, how is the user interface in a variety of platforms, with user-eyes and focusing on its usefulness... so, functional, UI, usability and ad-hoc tests, technically speaking. &lt;br&gt;
They help building a complete coverage and eliminating possible risks on the app, checking boundaries and simulating real-world scenarios.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Techniques
&lt;/h2&gt;

&lt;p&gt;Black-box testing employs some techniques to have a great test coverage. Mentioning a few:&lt;/p&gt;

&lt;h3&gt;
  
  
  Equivalence partitioning
&lt;/h3&gt;

&lt;p&gt;Input is categorized into partitions of valid and invalid values. &lt;br&gt;
Example: When checking if a user is older than 18, it is enough to test using a birth date that's under that, and another older, covering both scenarios. &lt;/p&gt;

&lt;h3&gt;
  
  
  Boundary value analysis
&lt;/h3&gt;

&lt;p&gt;Check the boundaries of input ranges. &lt;br&gt;
Example: When selecting a product, the quantity should be between 1 and 100. So the test cases should revolve around values inside the range and outside it, such as negative values or greater/equal than 100, and of course valid inputs inside the range.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3vs6r1e3p5gzb5kmkrpm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3vs6r1e3p5gzb5kmkrpm.png" alt="boundary value analysis example" width="800" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Decision table testing
&lt;/h3&gt;

&lt;p&gt;A technique that captures various input data combinations and their corresponding behaviors in a tabular format. &lt;br&gt;
Let's consider authorization of user when entering e-mail and password. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Correct e-mail&lt;/th&gt;
&lt;th&gt;Correct password&lt;/th&gt;
&lt;th&gt;Message&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Success&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Incorrect password&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Incorrect e-mail&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Incorrect credentials&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This is a conditions table, which helps covering all the possible cases.&lt;/p&gt;

&lt;p&gt;Some others include: State transition testing, exploratory testing and guessing. &lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;This pretty much covers the essential parts of a black-box testing and its implementation. Please feel free to add any comments and correct me if necessary. &lt;/p&gt;

</description>
      <category>testing</category>
      <category>qa</category>
    </item>
    <item>
      <title>Important aspects of a Bug Report - QA</title>
      <dc:creator>Gabriel Mathias</dc:creator>
      <pubDate>Mon, 25 Nov 2024 00:18:17 +0000</pubDate>
      <link>https://dev.to/gablemathias/important-aspects-of-a-bug-report-quality-assurance-pij</link>
      <guid>https://dev.to/gablemathias/important-aspects-of-a-bug-report-quality-assurance-pij</guid>
      <description>&lt;p&gt;Writing a good, concise, and clear bug report can save you a lot of time explaining and reproducing the bug with your developer friend. That’s why I’m here today: to clarify—or at least refresh—your understanding of how to report that not-so-great issue found in a given application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keep in mind:&lt;/strong&gt; Put yourself in the shoes of both the user and the developer. What seems obvious to you might not be clear to them. Sometimes, the best approach is to explain things as if everyone around you were kids. We need to explain things properly (kids always ask “why?”), in a simple way (they’re still learning), and with as few gaps as possible—or ideally none—unless you want them to keep asking “why?” over and over.&lt;/p&gt;

&lt;h2&gt;
  
  
  Most important aspects
&lt;/h2&gt;

&lt;p&gt;Title, Steps to Reproduce and Logs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Title
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;It has to be clear, short and obvious.&lt;/strong&gt;&lt;br&gt;
"Product quantity input should only accept positive numbers" &lt;br&gt;
"Product quantity input should not accept negative numbers"&lt;/p&gt;

&lt;p&gt;Both titles clearly convey what is happening. I would say that the first one is more generic, as the problem could involve negative numbers or floating-point numbers. The second one is more specific, and I would personally choose this one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt; If you can’t make the title completely clear, provide a concise but informative explanation in the summary section of the description.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please don't do that:&lt;/strong&gt;&lt;br&gt;
"Product quantity inputted in field X should not accept negative numbers only positive numbers from 1 to 2......."&lt;br&gt;
It is just a title my friend, as I said, create a summary if necessary, the details go after that.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Steps to Reproduce
&lt;/h3&gt;

&lt;p&gt;I dare to say that this is the most important one, where you communicate exactly how you reached that issue. Think like holding someone arms showing the way.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Enter the link abc.com&lt;/li&gt;
&lt;li&gt;Go to the field X at the left corner of the page&lt;/li&gt;
&lt;li&gt;Insert the value -199&lt;/li&gt;
&lt;li&gt;Click on the button "Add to cart"&lt;/li&gt;
&lt;li&gt;Product was successfully added to the cart&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In this simple example, the issue can be easily reproduced. You talked about where to go, the fields, buttons, where to click and what is expected after the click.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logs
&lt;/h3&gt;

&lt;p&gt;That is where you attach some evidences denoting the place where you found the bug, preferably highlighted. &lt;br&gt;
Everything that can help and show the problem: screenshots of the webpage, server logs, exceptions.&lt;/p&gt;

&lt;h2&gt;
  
  
  What else ?
&lt;/h2&gt;

&lt;p&gt;There are additional elements to include in a bug report, such as the  ID, environment, version, priority, labels, and attributions. These are essential, and I will cover them in greater detail in a separate article about creating a bug report in Jira. For now, I hope this explanation clarifies the core purpose of a bug report and, ideally, saves you from at least one Google/Teams meeting, allowing you to focus on what matters.&lt;/p&gt;

</description>
      <category>qa</category>
      <category>testing</category>
      <category>codequality</category>
      <category>assurance</category>
    </item>
    <item>
      <title>End-to-End Testing with Cypress</title>
      <dc:creator>Gabriel Mathias</dc:creator>
      <pubDate>Sat, 09 Nov 2024 10:41:55 +0000</pubDate>
      <link>https://dev.to/gablemathias/end-to-end-testing-with-cypress-10pl</link>
      <guid>https://dev.to/gablemathias/end-to-end-testing-with-cypress-10pl</guid>
      <description>&lt;p&gt;While searching for tools to learn and apply end-to-end (E2E) automated tests, I found Cypress to be quite impressive! My goal was to find a tool that was easy to set up and had a clean user interface (UI) as a plus. &lt;/p&gt;

&lt;p&gt;For me, the best way to learn is by doing. So, I decided to automate an order on the Pizza Hub website, stopping at the point of payment, and having the opportunity, I also made a more complete approach on another website visiting and filling all the necessary fields to their job application process.   &lt;/p&gt;

&lt;p&gt;The code can be found below. I ensured it's easy to read and follow along. &lt;br&gt;
&lt;a href="https://github.com/gablemathias/pizzahut-cypress/blob/main/cypress/e2e/spec.cy.js" rel="noopener noreferrer"&gt;Github Repo - Ordering on PizzaHut&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7x12dy0rwfa32mq3bv02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7x12dy0rwfa32mq3bv02.png" alt="Cart" width="800" height="505"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the process automated:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Access the website&lt;/li&gt;
&lt;li&gt;Click on "New Order"&lt;/li&gt;
&lt;li&gt;Accept the cookies&lt;/li&gt;
&lt;li&gt;Fill in blank fields (address, etc.)&lt;/li&gt;
&lt;li&gt;Select the pizza&lt;/li&gt;
&lt;li&gt;Modify the dough type&lt;/li&gt;
&lt;li&gt;Add to cart&lt;/li&gt;
&lt;li&gt;Reach the payment page&lt;/li&gt;
&lt;li&gt;Check the cart&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I tested them both on Chromium (v130) and Firefox (v129).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb4v1i5e1u6jsw0vv1lri.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb4v1i5e1u6jsw0vv1lri.png" alt="Firefox Test" width="458" height="598"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqw247wj06042vxp4f5t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqw247wj06042vxp4f5t.png" alt="Chromium Test" width="521" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For the job application process&lt;/strong&gt;, I ensured to include all the necessary information on the repository, with images and videos to follow along! &lt;br&gt;
&lt;a href="https://github.com/gablemathias/init-cypress" rel="noopener noreferrer"&gt;Github Repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There's also some examples of creating and documenting user stories  using &lt;a href="https://www.atlassian.com/software/jira" rel="noopener noreferrer"&gt;Jira&lt;/a&gt; and &lt;a href="https://www.atlassian.com/software/confluence" rel="noopener noreferrer"&gt;Confluence&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Impressions
&lt;/h2&gt;

&lt;p&gt;I've found that the methods &lt;code&gt;cy.get&lt;/code&gt; and &lt;code&gt;cy.should&lt;/code&gt; were my best friends. Cypress has amazing &lt;a href="https://docs.cypress.io/api/table-of-contents" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; and &lt;a href="https://learn.cypress.io/testing-your-first-application" rel="noopener noreferrer"&gt;courses&lt;/a&gt; to get you through everything pretty well and acquire a solid knowledge. I still have a lot to learn!&lt;/p&gt;

&lt;h3&gt;
  
  
  Pros
&lt;/h3&gt;

&lt;p&gt;My first two impressions of this tool were the real-time reloading and "time travel" features. Every time I saved the code I changed, the test automatically reloaded, making it incredibly fast to check for any possible errors and move on. I was able to examine every step made by the program and the web-app's state at each point. I can't say how much time this saves and how it allowed me to focus on what really matters: what I wanted to test and whether it was working properly or not.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cons
&lt;/h3&gt;

&lt;p&gt;Well, It doesn’t support WebKit Browsers Engine (Safari) and mobile testing natively. It’s mainly suited for JavaScript applications and involves huuuuge DOM manipulation, which can be problematic for some applications that manipulate the DOM after the page loads. &lt;br&gt;
I also have the feel that certain complex scenarios might slow down the execution speed of the tests. Using more assertions could potentially help with this, but I am not sure since this particular test wasn’t resource-intensive.&lt;/p&gt;

</description>
      <category>cypress</category>
      <category>e2e</category>
    </item>
  </channel>
</rss>
