Using AI to Code Tutorial Projects — C++ Bubble Sort

Jay (Vijayasimha BR)
4 min readNov 28, 2024

--

Using AI to Code Tutorial Projects — C++ Bubble Sort

For some time now, I have been using AI to build code for my tutoring sessions. Here is how I would go about using AI to get myself a bubble sort.

Tools at my disposal.

  1. Access to a AI service. In my case, I have a premium subscription to GPT-4o, obtained as part of one of my developer subscriptions.
  2. Code Blocks for C++, to run my C++ code.

Right then, first up, would be, setting up the context. Usually, I start off by setting the right situation for the AI.

I will say,

Let's do some C++

and the AI responds.

Great! What specific task or project would you like to work on in C++? Are you looking to develop a particular application, solve a problem, or perhaps work on an existing codebase? Let me know how I can assist you!

Then, I simply ask what I want.

give me a bubble sort that uses integers

and I get.

#include <iostream>
#include <vector>

void bubbleSort(std::vector<int>& arr) {
int n = arr.size();
bool swapped;
for (int i = 0; i < n - 1; ++i) {
swapped = false;
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
swapped = true;
}
}
// If no two elements were swapped by inner loop, then break
if (!swapped)
break;
}
}

void printArray(const std::vector<int>& arr) {
for (const int& num : arr) {
std::cout << num << " ";
}
std::cout << std::endl;
}

int main() {
std::vector<int> arr = {64, 34, 25, 12, 22, 11, 90};
std::cout << "Original array: ";
printArray(arr);

bubbleSort(arr);

std::cout << "Sorted array: ";
printArray(arr);

return 0;
}

Now, I see that AI assumes that I want to use vectors. There are some scenarios where I like to use vectors. Right now though, suppose, I am tutoring a beginner student. I want to use a simpe array. So, I ask AI to revise it for me.

So, I say.

vectors are a little too advanced for me. I am a beginner. can you revise the code to use simple arrays.

AI Responds

#include <iostream>

void bubbleSort(int arr[], int n) {
bool swapped;
for (int i = 0; i < n - 1; ++i) {
swapped = false;
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
swapped = true;
}
}
// If no two elements were swapped by inner loop, then break
if (!swapped)
break;
}
}

void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);

std::cout << "Original array: ";
printArray(arr, n);

bubbleSort(arr, n);

std::cout << "Sorted array: ";
printArray(arr, n);

return 0;
}

Now, I put this code in codeblocks, I get the following output.

Original array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90

Process returned 0 (0x0) execution time : 0.092 s
Press any key to continue.

So, it works. Now, let’s ask the AI to do some more changes, and how nicely, it does.

I wonder if this can be changed to work with strings, like names of super heroes
#include <iostream>
#include <string>

void bubbleSort(std::string arr[], int n) {
bool swapped;
for (int i = 0; i < n - 1; ++i) {
swapped = false;
for (int j = 0; j < n - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
std::swap(arr[j], arr[j + 1]);
swapped = true;
}
}
// If no two elements were swapped by inner loop, then break
if (!swapped)
break;
}
}

void printArray(const std::string arr[], int n) {
for (int i = 0; i < n; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}

int main() {
std::string superheroes[] = {"Batman", "Superman", "Spiderman", "Ironman", "Wonder Woman"};
int n = sizeof(superheroes) / sizeof(superheroes[0]);

std::cout << "Original array: ";
printArray(superheroes, n);

bubbleSort(superheroes, n);

std::cout << "Sorted array: ";
printArray(superheroes, n);

return 0;
}

and I try this in CodeBlocks, and get the following.

Original array: Batman Superman Spiderman Ironman Wonder Woman
Sorted array: Batman Ironman Spiderman Superman Wonder Woman

So, there you go. The more I use AI, the more I seem to be getting better at discovering conversing with this entity.

I work as a coding tutor. You can hire me on Upwork, Fiverr and Codementor. You can also book a session on calendly, and visit my website. Also, video tutorials on my YouTube Channel.

--

--

No responses yet