Tiffany R. White Blog

Just my little corner of the internet.

Exploring Algorithms

Tiffany White

What Is Meant by an Algorithm?

Algorithms are simple: they are a set of instructions broken down into simple steps in order to complete a larger task.

For example, if I want to make a recipe for stuffed peppers, I would usually follow steps such as:

  1. Preheat oven to 350°F.
  2. Cut out stem ends of bell peppers.
  3. Discard stems.
  4. Scoop out seeds.
  5. Bring 8 cups water to a boil in a large pot and blanch the peppers until tender-crisp, about 1 minute.
  6. Drain.
  7. Cool under cold running water.
  8. Set aside…
And so on.

This is a really simple example of an algorithm.

Using Algorithms in Java

In Computer Science, there are well established methods given to students to help them master algorithms. One of them is the speeding ticket scenario, where you are making a text-based system to calculate fines for speeding, speeding in a construction zone, DUI, and being under 21 with a DUI, etc.

https://www.youtube.com/embed/XSLj46Lfpfk

Here, we step through getting the data with a few conditionals:

public static void main (String args[])
	{
		// Create a scanner to read from keyboard
		Scanner kbd = new Scanner (System.in);
   <span class="&quot;c1&quot;">// declare and assign values to variables that we can use later</span>
	<span class="&quot;n&quot;">String</span> <span class="&quot;n&quot;">firstName</span><span class="&quot;o&quot;">=</span><span class="&quot;s&quot;">"N/A"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">lastName</span><span class="&quot;o&quot;">=</span><span class="&quot;s&quot;">"N/A"</span><span class="&quot;o&quot;">;</span>
	<span class="&quot;kt&quot;">int</span> <span class="&quot;n&quot;">age</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">,</span> <span class="&quot;n&quot;">speedLimit</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">,</span> <span class="&quot;n&quot;">actualSpeed</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">,</span> <span class="&quot;n&quot;">mphOverLimit</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">;</span>
	<span class="&quot;kt&quot;">int</span> <span class="&quot;n&quot;">baseFine</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">,</span> <span class="&quot;n&quot;">underAgeFine</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">,</span> <span class="&quot;n&quot;">zoneFine</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">,</span> <span class="&quot;n&quot;">totalFine</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">;</span>

//################ Input ###################### // Here, the person calculating the fine would enter the data which is // read in by the scanner object System.out.println(“Enter your first name and last name. > ”); firstName = kbd.next(); lastName = kbd.next(); System.out.println(“Enter your age. > ”); age = kbd.nextInt(); System.out.println(“Enter the speed limit. > ”); speedLimit = kbd.nextInt(); System.out.println(“Enter your actual speed. > ”); actualSpeed = kbd.nextInt(); System.out.println(“Did the violation occur in a construction zone? Enter yes or no. > ”); String inConstructionZone = kbd.next();

//########### Speed Limit Fines ######### // Algorithm, step by step, giving the computer instructions on how to calculate // the fine using conditionals if (actualSpeed > speedLimit) { mphOverLimit = actualSpeed - speedLimit; } else { mphOverLimit = 0; } if (mphOverLimit < 5) { baseFine = 0; } else if (mphOverLimit <= 20) { baseFine = mphOverLimit / 5 30; } else if (mphOverLimit > 20) { baseFine = mphOverLimit / 5 50; }

 <span class="&quot;c1&quot;">//########### Zone Fines ############</span>

if (mphOverLimit < 5) { zoneFine = 0; } else if (inConstructionZone.equals(“yes”) || inConstructionZone.equals(“y”) || inConstructionZone.equals(“Yes”) || inConstructionZone.equals(“YES”)) { zoneFine = baseFine * baseFine; } else if (inConstructionZone.equals(“no”) || inConstructionZone.equals(“n”) || inConstructionZone.equals(“No”) || inConstructionZone.equals(“NO”)) { zoneFine = 0; }

 <span class="&quot;c1&quot;">//############## Age Fines #############</span>

 <span class="&quot;k&quot;">if</span> <span class="&quot;o&quot;">(</span><span class="&quot;n&quot;">age</span> <span class="&quot;o&quot;">&lt;</span> <span class="&quot;mi&quot;">21</span> <span class="&quot;o&quot;">&amp;&amp;</span> <span class="&quot;n&quot;">mphOverLimit</span> <span class="&quot;o&quot;">&gt;</span> <span class="&quot;mi&quot;">20</span><span class="&quot;o&quot;">)</span>
 <span class="&quot;o&quot;">{</span>
	 <span class="&quot;n&quot;">underAgeFine</span> <span class="&quot;o&quot;">=</span> <span class="&quot;mi&quot;">300</span><span class="&quot;o&quot;">;</span>
 <span class="&quot;o&quot;">}</span>
 <span class="&quot;k&quot;">else</span>
 <span class="&quot;o&quot;">{</span>
	 <span class="&quot;n&quot;">underAgeFine</span> <span class="&quot;o&quot;">=</span> <span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">;</span>
 <span class="&quot;o&quot;">}</span>

//############# Total Charges ##############

 <span class="&quot;n&quot;">totalFine</span> <span class="&quot;o&quot;">=</span> <span class="&quot;n&quot;">baseFine</span> <span class="&quot;o&quot;">+</span> <span class="&quot;n&quot;">zoneFine</span> <span class="&quot;o&quot;">+</span> <span class="&quot;n&quot;">underAgeFine</span><span class="&quot;o&quot;">;</span>
   <span class="&quot;c1&quot;">// Telling the JVM to print out each formatted scanner input on a new line</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">println</span><span class="&quot;o&quot;">();</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"name: %s, %sn"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">lastName</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">firstName</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"age: %d yrs.n"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">age</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"actual speed: %d mph.n"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">actualSpeed</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"speed limit: %d mph.n"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">speedLimit</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"mph over limit: %d mph.n"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">mphOverLimit</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"base fine: $%dn"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">baseFine</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"zone fine: $%dn"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">zoneFine</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"under age fine: $%dn"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">underAgeFine</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"total fine: $%dn"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">totalFine</span> <span class="&quot;o&quot;">);</span>
<span class="&quot;o&quot;">}</span> <span class="&quot;c1&quot;">// END MAIN</span>

} // END CLASS

Some More Complex Algorithms

There are more complex, established algorithms that use data structures like arrays, dictionaries, and linked lists. Most algorithms are judged on their complexity and effieciency, where efficiency does not necessarily mean speed.

For example, the Bubble Sort algorithm sorts the values in an array from smallest to largest by assessing the first index and comparing it with the next. If the first value is larger than the second, the bubble sort algorithm swaps the two values.

Here is a version of the bubble sort algorithm in pseudocode:

procedure bubbleSort( list : array of items )

loop = list.count;

for i = 0 to loop-1 do: swapped = false

  </span><span class="&quot;k&quot;">for </span>j <span class="&quot;o&quot;">=</span> 0 to loop-1 <span class="&quot;k&quot;">do</span>:

     // compare the adjacent elements
     <span class="&quot;k&quot;">if </span>list[j] &gt; list[j+1] <span class="&quot;k&quot;">then</span>
        // swap them
        swap<span class="&quot;o&quot;">(</span> list[j], list[j+1] <span class="&quot;o&quot;">)</span>
        swapped <span class="&quot;o&quot;">=</span> <span class="&quot;nb&quot;">true
     </span>end <span class="&quot;k&quot;">if

  </span>end <span class="&quot;k&quot;">for</span>

  // <span class="&quot;k&quot;">if </span>no number was swapped that means
  // array is sorted now, <span class="&quot;nb&quot;">break </span>the loop

  <span class="&quot;k&quot;">if</span><span class="&quot;o&quot;">(</span>not swapped<span class="&quot;o&quot;">)</span> <span class="&quot;k&quot;">then
     </span><span class="&quot;nb&quot;">break
  </span>end <span class="&quot;k&quot;">if

end for

end procedure return list

This a simple algorithm yet if you’re new to algorithms and programming in general, it may seem a bit daunting.

To see the implementation in java:

What Is Meant by an Algorithm?

Algorithms are simple: they are a set of instructions broken down into simple steps in order to complete a larger task.

For example, if I want to make a recipe for stuffed peppers, I would usually follow steps such as:

  1. Preheat oven to 350°F.
  2. Cut out stem ends of bell peppers.
  3. Discard stems.
  4. Scoop out seeds.
  5. Bring 8 cups water to a boil in a large pot and blanch the peppers until tender-crisp, about 1 minute.
  6. Drain.
  7. Cool under cold running water.
  8. Set aside…
And so on.

This is a really simple example of an algorithm.

Using Algorithms in Java

In Computer Science, there are well established methods given to students to help them master algorithms. One of them is the speeding ticket scenario, where you are making a text-based system to calculate fines for speeding, speeding in a construction zone, DUI, and being under 21 with a DUI, etc.

https://www.youtube.com/embed/XSLj46Lfpfk

Here, we step through getting the data with a few conditionals:

public static void main (String args[])
	{
		// Create a scanner to read from keyboard
		Scanner kbd = new Scanner (System.in);
   <span class="&quot;c1&quot;">// declare and assign values to variables that we can use later</span>
	<span class="&quot;n&quot;">String</span> <span class="&quot;n&quot;">firstName</span><span class="&quot;o&quot;">=</span><span class="&quot;s&quot;">"N/A"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">lastName</span><span class="&quot;o&quot;">=</span><span class="&quot;s&quot;">"N/A"</span><span class="&quot;o&quot;">;</span>
	<span class="&quot;kt&quot;">int</span> <span class="&quot;n&quot;">age</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">,</span> <span class="&quot;n&quot;">speedLimit</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">,</span> <span class="&quot;n&quot;">actualSpeed</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">,</span> <span class="&quot;n&quot;">mphOverLimit</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">;</span>
	<span class="&quot;kt&quot;">int</span> <span class="&quot;n&quot;">baseFine</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">,</span> <span class="&quot;n&quot;">underAgeFine</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">,</span> <span class="&quot;n&quot;">zoneFine</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">,</span> <span class="&quot;n&quot;">totalFine</span><span class="&quot;o&quot;">=</span><span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">;</span>

//################ Input ###################### // Here, the person calculating the fine would enter the data which is // read in by the scanner object System.out.println(“Enter your first name and last name. > ”); firstName = kbd.next(); lastName = kbd.next(); System.out.println(“Enter your age. > ”); age = kbd.nextInt(); System.out.println(“Enter the speed limit. > ”); speedLimit = kbd.nextInt(); System.out.println(“Enter your actual speed. > ”); actualSpeed = kbd.nextInt(); System.out.println(“Did the violation occur in a construction zone? Enter yes or no. > ”); String inConstructionZone = kbd.next();

//########### Speed Limit Fines ######### // Algorithm, step by step, giving the computer instructions on how to calculate // the fine using conditionals if (actualSpeed > speedLimit) { mphOverLimit = actualSpeed - speedLimit; } else { mphOverLimit = 0; } if (mphOverLimit < 5) { baseFine = 0; } else if (mphOverLimit <= 20) { baseFine = mphOverLimit / 5 30; } else if (mphOverLimit > 20) { baseFine = mphOverLimit / 5 50; }

 <span class="&quot;c1&quot;">//########### Zone Fines ############</span>

if (mphOverLimit < 5) { zoneFine = 0; } else if (inConstructionZone.equals(“yes”) || inConstructionZone.equals(“y”) || inConstructionZone.equals(“Yes”) || inConstructionZone.equals(“YES”)) { zoneFine = baseFine * baseFine; } else if (inConstructionZone.equals(“no”) || inConstructionZone.equals(“n”) || inConstructionZone.equals(“No”) || inConstructionZone.equals(“NO”)) { zoneFine = 0; }

 <span class="&quot;c1&quot;">//############## Age Fines #############</span>

 <span class="&quot;k&quot;">if</span> <span class="&quot;o&quot;">(</span><span class="&quot;n&quot;">age</span> <span class="&quot;o&quot;">&lt;</span> <span class="&quot;mi&quot;">21</span> <span class="&quot;o&quot;">&amp;&amp;</span> <span class="&quot;n&quot;">mphOverLimit</span> <span class="&quot;o&quot;">&gt;</span> <span class="&quot;mi&quot;">20</span><span class="&quot;o&quot;">)</span>
 <span class="&quot;o&quot;">{</span>
	 <span class="&quot;n&quot;">underAgeFine</span> <span class="&quot;o&quot;">=</span> <span class="&quot;mi&quot;">300</span><span class="&quot;o&quot;">;</span>
 <span class="&quot;o&quot;">}</span>
 <span class="&quot;k&quot;">else</span>
 <span class="&quot;o&quot;">{</span>
	 <span class="&quot;n&quot;">underAgeFine</span> <span class="&quot;o&quot;">=</span> <span class="&quot;mi&quot;">0</span><span class="&quot;o&quot;">;</span>
 <span class="&quot;o&quot;">}</span>

//############# Total Charges ##############

 <span class="&quot;n&quot;">totalFine</span> <span class="&quot;o&quot;">=</span> <span class="&quot;n&quot;">baseFine</span> <span class="&quot;o&quot;">+</span> <span class="&quot;n&quot;">zoneFine</span> <span class="&quot;o&quot;">+</span> <span class="&quot;n&quot;">underAgeFine</span><span class="&quot;o&quot;">;</span>
   <span class="&quot;c1&quot;">// Telling the JVM to print out each formatted scanner input on a new line</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">println</span><span class="&quot;o&quot;">();</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"name: %s, %sn"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">lastName</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">firstName</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"age: %d yrs.n"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">age</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"actual speed: %d mph.n"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">actualSpeed</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"speed limit: %d mph.n"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">speedLimit</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"mph over limit: %d mph.n"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">mphOverLimit</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"base fine: $%dn"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">baseFine</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"zone fine: $%dn"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">zoneFine</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"under age fine: $%dn"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">underAgeFine</span> <span class="&quot;o&quot;">);</span>
	<span class="&quot;n&quot;">System</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">out</span><span class="&quot;o&quot;">.</span><span class="&quot;na&quot;">format</span><span class="&quot;o&quot;">(</span> <span class="&quot;s&quot;">"total fine: $%dn"</span><span class="&quot;o&quot;">,</span><span class="&quot;n&quot;">totalFine</span> <span class="&quot;o&quot;">);</span>
<span class="&quot;o&quot;">}</span> <span class="&quot;c1&quot;">// END MAIN</span>

} // END CLASS

Some More Complex Algorithms

There are more complex, established algorithms that use data structures like arrays, dictionaries, and linked lists. Most algorithms are judged on their complexity and effieciency, where efficiency does not necessarily mean speed.

For example, the Bubble Sort algorithm sorts the values in an array from smallest to largest by assessing the first index and comparing it with the next. If the first value is larger than the second, the bubble sort algorithm swaps the two values.

Here is a version of the bubble sort algorithm in pseudocode:

procedure bubbleSort( list : array of items )

loop = list.count;

for i = 0 to loop-1 do: swapped = false

  </span><span class="&quot;k&quot;">for </span>j <span class="&quot;o&quot;">=</span> 0 to loop-1 <span class="&quot;k&quot;">do</span>:

     // compare the adjacent elements
     <span class="&quot;k&quot;">if </span>list[j] &gt; list[j+1] <span class="&quot;k&quot;">then</span>
        // swap them
        swap<span class="&quot;o&quot;">(</span> list[j], list[j+1] <span class="&quot;o&quot;">)</span>
        swapped <span class="&quot;o&quot;">=</span> <span class="&quot;nb&quot;">true
     </span>end <span class="&quot;k&quot;">if

  </span>end <span class="&quot;k&quot;">for</span>

  // <span class="&quot;k&quot;">if </span>no number was swapped that means
  // array is sorted now, <span class="&quot;nb&quot;">break </span>the loop

  <span class="&quot;k&quot;">if</span><span class="&quot;o&quot;">(</span>not swapped<span class="&quot;o&quot;">)</span> <span class="&quot;k&quot;">then
     </span><span class="&quot;nb&quot;">break
  </span>end <span class="&quot;k&quot;">if

end for

end procedure return list

This a simple algorithm yet if you’re new to algorithms and programming in general, it may seem a bit daunting.

To see the implementation in java:

https://www.youtube.com/embed/RqfWvIsYmsc

  • © 2019 Tiffany R. White Blog
  • Made with GatsbyJS & ☕️
  • hosted on Netlify | v7.0