Press ESC to close · Ctrl+K to open

Bubble Algorithm - Generated by AI

Bubble Algorithm - Generated by AI

In this post, we will demonstrate how it is possible to generate the code for the bubble algorithm in C# through an AI.

Today we will use the "PlayGround" of the beta version of openai. This tool allows us to give instructions to the AI, that is, inputs in human language format and receive as output automatically generated code, which we can copy and paste into our favorite code editor and execute it.

Normally, if the codes are "simple", they usually do not fail. However, we will also see in future posts how to solve the problems that may arise when creating code with this AI.

Image generated by AI about "bubble".

Note: The bubble algorithm is a simple sorting algorithm that compares each element of a list with its next element. If the current element is greater than the next, then the elements are swapped. This process is repeated until the list is sorted. The bubble algorithm is one of the simplest algorithms for sorting a list, although it is also one of the slowest. - By AI

Example 1 - Code in C# compiled and executed in Visual Studio 2022, with a "Console Application" project.

Step 1: Generate instruction in openai.

Instruction in PlayGround

Step 2: Press the "submit" button and wait for the AI to do its magic.

Code
using System;

public class Program
{
    public static void Main()
    {
        // Variable declaration
        int[] numbers = {5,2,4,6,1,3};
        int aux;

        // Bubble algorithm
        for (int i = 0; i < numbers.Length - 1; i++)
        {
            for (int j = 0; j < numbers.Length - 1; j++)
            {
                if (numbers[j] > numbers[j + 1])
                {
                    aux = numbers[j + 1];
                    numbers[j + 1] = numbers[j];
                    numbers[j] = aux;
                }
            }
        }

        // Output
        Console.WriteLine("Sorted array:");
        foreach (int number in numbers)
        {
            Console.Write(number + " ");
        }
    }
}

Step 3: visually analyze the code.

In this step, it is important to know what the AI has returned to us, since our question was about the bubble algorithm, but we did not ask for any example. However, the AI provided us with one, in which it will try to sort the following array: numbers ={5,2,4,6,1,3};

Hint*: if at this step, we did not know exactly what the system is doing, we can ask it. Pay attention:

AI Instructions: more info.

Step 4: Copy and paste the result into the previously generated Visual Studio project and once done, compile and execute.

Visual Studio 2022 Code

Step 5: analyze the results, checking that the program returns what is expected.

Incredible. The result is correct.

We have been able to program a classic computer algorithm in a matter of seconds. Logically, you can do your own tests and introduce different arrays, with different sizes and values and check that it continues to work in all circumstances.

Pedro Pagán Pallarés

Industrial Automation Expert and AI Researcher.