
In this class I will propose a simple exercise that will force you to think of an ingenious solution. This type of situation will initiate your development of programming logic.
The code for this class is here:
int subAlg(int a, int b) {
int value = 0;
while (a > b) {
b++;
value++;
}
return value;
}
In this class we continue developing algorithms based on the previous class.
The code of this class is here:
int mulAlg(int a, int b) {
int value = 0;
while (a) {
a--;
value += b;
}
return value;
}
In this class we develop more complicated algorithms and at the same time I recommend some websites where you can learn from the code of other professional experts.
The code of this class is here:
int divAlg(int a, int b) {
int value = 0;
while (a>=b) {
value++;
a -= b;
}
cout << "15 / 3 = " << value << "(" << a << ")" << endl;
return value;
}
In this class we will see how a known exercise can be VERY MUCH optimized. Here you will acquire the essential habit of optimization.
The code for this class is here:
void verifyPrime(int number) {
bool prime = true;
for (int i = 2; i <= number/2 && prime; i++) {
if (number % i == 0) prime = false;
}
if (prime) cout << number << " is prime" << endl;
else cout << number << " is not prime" << endl;
}
Before explaining each of the algorithms we will see, we will have an overview of this section. In this class we will talk about why sorting algorithms are important and we will make a comparison between them.
In this class I will explain the logic behind the Bubble Sort algorithm to help you understand this sorting method. After watching this video I encourage you to try to code the algorithm. And then in the next class you will be able to check how I have done it. In this way you can test your skills and perfect your ability to convert logical thinking about solving a problem into code.
In this class we will see the code implementation of the BubbleSort algorithm with the exact explanation of what each line and element of the code does (part 1).
The code of this class is here:
void byBubbleSort() {
//int v[] = { 2,7,1,4,3,5,0,8,2,-1,2 };
int v[] = { 7, 28, 2, 53, 16, 13, 45, 345, 2345, 234, 35235 ,123, 4346,75, 4,7,3,68,2,6,63,2,0,68,436,12,343,54,25, 13, 15, 2, 97, 81, 981 ,618 ,61, 3280, 18, 5, 78, 23, 81, 65, 84, 72, 95 };
int size = sizeof(v) / sizeof(*v);
int aux;
int rounds = 0;
bool ord = false;
cout << endl;
for (int i = 0; i < size; i++) cout << v[i] << " ";
while (!ord) {
ord = true;
for (int i = 0; i < size - 1 - rounds; i++) {
if (v[i] > v[i + 1]) {
aux = v[i + 1];
v[i + 1] = v[i];
v[i] = aux;
ord = false;
}
}
rounds++;
}
cout << endl;
for (int i = 0; i < size; i++) cout << v[i] << " ";
}
In this class we will see the code implementation of the Bubble Sort algorithm with the exact explanation of what each line and element of the code does (part 2).
The code of this class is here
void byBubbleSort() {
//int v[] = { 2,7,1,4,3,5,0,8,2,-1,2 };
int v[] = { 7, 28, 2, 53, 16, 13, 45, 345, 2345, 234, 35235 ,123, 4346,75, 4,7,3,68,2,6,63,2,0,68,436,12,343,54,25, 13, 15, 2, 97, 81, 981 ,618 ,61, 3280, 18, 5, 78, 23, 81, 65, 84, 72, 95 };
int size = sizeof(v) / sizeof(*v);
int aux;
int rounds = 0;
bool ord = false;
cout << endl;
for (int i = 0; i < size; i++) cout << v[i] << " ";
while (!ord) {
ord = true;
for (int i = 0; i < size - 1 - rounds; i++) {
if (v[i] > v[i + 1]) {
aux = v[i + 1];
v[i + 1] = v[i];
v[i] = aux;
ord = false;
}
}
rounds++;
}
cout << endl;
for (int i = 0; i < size; i++) cout << v[i] << " ";
}
In this class I will explain the logic behind the Selection algorithm so that you can understand this sorting method. After watching this video I encourage you to try to code the algorithm. And then in the next class you will be able to check how I have done it. In this way you can test your skills and perfect your ability to convert logical thinking about solving a problem into code.
Code Implementation of Algorithm by Selection Part 1
In this class we will see the coding step of the Selection algorithm with the exact explanation of what each line and element of the code does.
The code of this class is here
void bySelectionSort() {
//int v[] = { 2,7,1,4,3,5,0,8,2,-1,2 };
int v[] = { 7, 28, 2, 53, 16, 13, 45, 345, 2345, 234, 35235 ,123, 4346,75, 4,7,3,68,2,6,63,2,0,68,436,12,343,54,25, 13, 15, 2, 97, 81, 981 ,618 ,61, 3280, 18, 5, 78, 23, 81, 65, 84, 72, 95 };
int size = sizeof(v) / sizeof(*v);
int aux;
int PosMin;
cout << endl;
for (int i = 0; i < size; i++) cout << v[i] << " ";
for (int i = 0; i < size; i++) {
PosMin = i;
for (int j = i + 1; j < size; j++) {
if (v[PosMin] > v[j])
PosMin = j;
}
aux = v[i];
v[i] = v[PosMin];
v[PosMin] = aux;
}
cout << endl;
for (int i = 0; i < size; i++) cout << v[i] << " ";
}
Code Implementation of Algorithm by Selection Part 2
In this class we will see the coding step of the Selection algorithm with the exact explanation of what each line and element of the code does.
The code of this class is here
void bySelectionSort() {
//int v[] = { 2,7,1,4,3,5,0,8,2,-1,2 };
int v[] = { 7, 28, 2, 53, 16, 13, 45, 345, 2345, 234, 35235 ,123, 4346,75, 4,7,3,68,2,6,63,2,0,68,436,12,343,54,25, 13, 15, 2, 97, 81, 981 ,618 ,61, 3280, 18, 5, 78, 23, 81, 65, 84, 72, 95 };
int size = sizeof(v) / sizeof(*v);
int aux;
int PosMin;
cout << endl;
for (int i = 0; i < size; i++) cout << v[i] << " ";
for (int i = 0; i < size; i++) {
PosMin = i;
for (int j = i + 1; j < size; j++) {
if (v[PosMin] > v[j])
PosMin = j;
}
aux = v[i];
v[i] = v[PosMin];
v[PosMin] = aux;
}
cout << endl;
for (int i = 0; i < size; i++) cout << v[i] << " ";
}
Part 1 of the Creating Your Own Sorting Algorithm Project
In this video I will explain the algorithm I have personally developed for sorting data and the corresponding implementation in code.
Please try to create your own algorithm and share your code! :)
Part 2 of Creating Your Own Sorting Algorithm Project
In this video I will explain the algorithm I have personally developed for sorting data and the corresponding implementation in code.
Please try to create your own algorithm and share your code! :)
Part 3 of Creating Your Own Sorting Algorithm Project
In this video I will explain the algorithm I have personally developed for sorting data and the corresponding implementation in code.
Please try to create your own algorithm and share your code! :)
The code for this class is here
void byJJ() {
unsigned t0, t1;
t0 = clock();
//int v[] = { 2,7,1,4, 5,8,0};
int v[] = { 7, 28, 2, 53, 16, 13, 45, 345, 2345, 234, 35235 ,123, 4346,75, 4,7,3,68,2,6,63,2,0,68,436,12,343,54,25, 13, 15, 2, 97, 81, 981 ,618 ,61, 3280, 18, 5, 78, 23, 81, 65, 84, 72, 95 };
int size = sizeof(v) / sizeof(*v);
int aux;
int PosMin;
int PosMax;
int PosFinal;
PosFinal = size - 1;
cout << endl;
for (int i = 0; i < size; i++) cout << v[i] << " ";
for (int i = 0; i < size / 2; i++) {
PosMin = i;
PosMax = i;
for (int j = i; j <= PosFinal; j++) {
if (v[j] > v[PosMax]) PosMax = j;
if (v[j] < v[PosMin]) PosMin = j;
}
aux = v[PosMin];
v[PosMin] = v[i];
v[i] = aux;
if (PosMax == i) PosMax = PosMin;
aux = v[PosFinal];
v[PosFinal] = v[PosMax];
v[PosMax] = aux;
PosFinal--;
//cout << endl;
//for (int n = 0; n < size; n++) cout << v[n] << " ";
}
cout << endl;
for (int i = 0; i < size; i++) cout << v[i] << " ";
t1 = clock();
double time = (double(t1 - t0) / CLOCKS_PER_SEC);
cout << endl << endl << "Execution Time byJJ: " << time << endl;
}
Before closing the algorithms section I give you some tips to further develop your programming skill
In this Algorithms course you will learn the basics to develop your programming logic. This course will give you a complete tour of all the most basic concepts to learn how to create your own algorithms, thanks to the global vision you will acquire. In fact throughout the course you will learn how to apply each idea to any programming language.
You will learn fundamental programming skills:
Elementary Algorithms
Development of programming logic
Best practices when writing code
Optimization of resources, time and operations in an algorithm.
You will also have downloadable didactic material, tips from my +15 years of programming experience and solved exercises so you will understand what logic each algorithm follows and know how to implement it in the language of your choice.
You will learn how to develop from scratch algorithms to solve problems no matter what programming language or technology you use. We will mainly focus on the development logic so that you will be able to transfer the idea to your own development environment. Therefore, no matter what language you use, the contents of these classes will be totally useful for you.
Among the different algorithms that we will see are included the sorting algorithms with a detailed explanation of how they work and each line and element of the code that they have. Did you know that in this course we will create a sorting algorithm faster than the famous Bubble Sort, Selection or Insertion????? If you want to know how to create algorithms with optimization and efficiency in mind...this is the ideal course for you.
All concepts are explained in detail, step by step and understanding the why of each thing. This way you will be able to grasp the basics of these programming pillars from the very beginning of your professional development. This course will allow you to expand into any area of software development.
Isn't it great? Well, the best of all is that it is at your fingertips.
Buy the course and enjoy all that is waiting for you.
See you soon!
Jose Javier Villena