Skip to content

Commit b01461a

Browse files
Merge branch 'kelvins:main' into feature/my-contribution
2 parents 44fde4d + 1fc84c8 commit b01461a

12 files changed

+699
-83
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ In order to achieve greater coverage and encourage more people to contribute to
183183
<tr>
184184
<td><a href="https://en.wikipedia.org/wiki/Binary_search_algorithm">Binary Search</a></td>
185185
<td> <!-- C -->
186-
<a href="./CONTRIBUTING.md">
187-
<img align="center" height="25" src="./logos/github.svg" />
186+
<a href="./src/c/BinarySearch.c">
187+
<img align="center" height="25" src="./logos/c.svg" />
188188
</a>
189189
</td>
190190
<td> <!-- C++ -->
@@ -309,8 +309,8 @@ In order to achieve greater coverage and encourage more people to contribute to
309309
</a>
310310
</td>
311311
<td> <!-- Java -->
312-
<a href="./CONTRIBUTING.md">
313-
<img align="center" height="25" src="./logos/github.svg" />
312+
<a href="src/java/LinearSearchIterative.java">
313+
<img align="center" height="25" src="./logos/java.svg" />
314314
</a>
315315
</td>
316316
<td> <!-- Python -->
@@ -362,8 +362,8 @@ In order to achieve greater coverage and encourage more people to contribute to
362362
</a>
363363
</td>
364364
<td> <!-- C++ -->
365-
<a href="./CONTRIBUTING.md">
366-
<img align="center" height="25" src="./logos/github.svg" />
365+
<a href="./src/cpp/LinearSearchRecursive.cpp">
366+
<img align="center" height="25" src="./logos/cplusplus.svg" />
367367
</a>
368368
</td>
369369
<td> <!-- Java -->
@@ -1285,13 +1285,13 @@ In order to achieve greater coverage and encourage more people to contribute to
12851285
<tr>
12861286
<td>Min and Max (D&C)</td>
12871287
<td> <!-- C -->
1288-
<a href="./CONTRIBUTING.md">
1289-
<img align="center" height="25" src="./logos/github.svg" />
1288+
<a href="./src/c/MinMaxDC.c">
1289+
<img align="center" height="25" src="./logos/c.svg" />
12901290
</a>
12911291
</td>
12921292
<td> <!-- C++ -->
1293-
<a href="./CONTRIBUTING.md">
1294-
<img align="center" height="25" src="./logos/github.svg" />
1293+
<a href="./src/cpp/MinMaxDC.cpp">
1294+
<img align="center" height="25" src="./logos/cplusplus.svg" />
12951295
</a>
12961296
</td>
12971297
<td> <!-- Java -->
@@ -2162,8 +2162,8 @@ In order to achieve greater coverage and encourage more people to contribute to
21622162
</a>
21632163
</td>
21642164
<td> <!-- C++ -->
2165-
<a href="./CONTRIBUTING.md">
2166-
<img align="center" height="25" src="./logos/github.svg" />
2165+
<a href="./src/cpp/DoublyLinkedList.cpp">
2166+
<img align="center" height="25" src="./logos/cplusplus.svg" />
21672167
</a>
21682168
</td>
21692169
<td> <!-- Java -->
@@ -3150,8 +3150,8 @@ In order to achieve greater coverage and encourage more people to contribute to
31503150
</a>
31513151
</td>
31523152
<td> <!-- C++ -->
3153-
<a href="./CONTRIBUTING.md">
3154-
<img align="center" height="25" src="./logos/github.svg" />
3153+
<a href="./src/cpp/MergeSort.cpp">
3154+
<img align="center" height="25" src="./logos/cplusplus.svg" />
31553155
</a>
31563156
</td>
31573157
<td> <!-- Java -->
@@ -3194,9 +3194,9 @@ In order to achieve greater coverage and encourage more people to contribute to
31943194
<img align="center" height="25" src="./logos/github.svg" />
31953195
</a>
31963196
</td>
3197-
<td> <!-- Kotlin -->
3198-
<a href="./CONTRIBUTING.md">
3199-
<img align="center" height="25" src="./logos/github.svg" />
3197+
<td> <!-- kotlin -->
3198+
<a href="./src/kotlin/MergeSort.kt">
3199+
<img align="center" height="25" src="./logos/kotlin.svg" />
32003200
</a>
32013201
</td>
32023202
</tr>
@@ -3732,8 +3732,8 @@ In order to achieve greater coverage and encourage more people to contribute to
37323732
</a>
37333733
</td>
37343734
<td> <!-- C++ -->
3735-
<a href="./CONTRIBUTING.md">
3736-
<img align="center" height="25" src="./logos/github.svg" />
3735+
<a href="src/cpp/LeibnizFormulaForPi.cpp">
3736+
<img align="center" height="25" src="./logos/cplusplus.svg" />
37373737
</a>
37383738
</td>
37393739
<td> <!-- Java -->

src/c/AlgoritmoDijkstra.c

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
* Grafos - Algoritmo de Dijkstra em C
2+
* Graphs - Dijkstra Algorithm in C
33
* Kelvin Salton do Prado - 2015
4-
* Complexidade: Teta(n^2)
4+
* Complexity: Theta(n^2)
55
*
6-
* 1 para todos - Arestas de pesos não negativo - Algoritmo guloso
7-
* Encontra o caminho mais curto de um vértice (inicio) a outro (destino)
6+
* 1 for all - Edges with non-negative weights - Greedy algorithm
7+
* Finds the shortest path from one vertex (start) to another (destination)
88
*
9-
* Grafo com 5 vértices e 6 arestas
9+
* Graph with 5 vertices and 6 edges
1010
*
1111
* 6
1212
* (0)-----------------(1)
@@ -19,77 +19,77 @@
1919
* \ /
2020
* -----(4)-----
2121
*
22-
* Matriz de Distância
22+
* Distance Matrix
2323
* 0 1 2 3 4
2424
* 0 0 6 10 - -
2525
* 1 6 0 - 2 -
2626
* 2 10 - 0 1 3
2727
* 3 - 2 1 0 8
2828
* 4 - - 3 8 0
2929
*
30-
* Para valores infinitos será considerado o valor: 4294967295
31-
* O objetivo é sair do ponto inicial (0) e chegar ao destino (4) pelo caminho mais curto
32-
* Resposta: (0)->(1)->(3)->(2)->(4) = 12
30+
* For infinite values, the value will be considered: 4294967295
31+
* The objective is to leave the starting point (0) and reach the destination (4) by the shortest route
32+
* Response: (0)->(1)->(3)->(2)->(4) = 12
3333
*
3434
*/
3535

3636
#include <stdio.h>
3737
#include <stdbool.h>
3838

39-
#define nroVertices 5 // Define uma constante 5 que é a quantidade de vértices do grafo
39+
#define noVertices 5 // Defines a constant 5 which is the number of vertices in the graph
4040

41-
// Algoritmo de Dijkstra recebe como parâmetro a matriz de distância e o número de vértices
42-
void Dijkstra(unsigned long int matriz[nroVertices][nroVertices], int n){
43-
bool visitados[n]; // Variável que guarda true para os vértices visitados
41+
// Dijkstra's algorithm takes as parameters the distance matrix and the number of vertices
42+
void Dijkstra(unsigned long int matrix[noVertices][noVertices], int n){
43+
44+
bool visited[n]; // Variable that holds true for visited vertices
4445

45-
// O valor 'i' do for abaixo não é utilizado, pois o for serve apenas para percorrer todo o número de colunas da matriz
46-
for(int i = 1; i < n; i++){ // Começa em 1 pois não precisa comparar o vértice com ele mesmo
46+
// The value 'i' from the for below is not used, as the for is only used to traverse the entire number of columns in the matrix
47+
for(int i = 1; i < n; i++){ // Starts at 1 because you don't need to compare the vertex with itself
4748

48-
int min = -1; // Variável que guarda a posição do menor valor, inicia em -1 pois é uma posição inválida
49-
unsigned long int MinValor = 4294967295; // Variável que guarda o menor valor encontrado, inicia com 'infinito', assim, sempre na primeira passada o valor será menor que esta variável
50-
51-
// For que percorre todas as linhas na coluna [0]
49+
int min = -1; // Variable that stores the position of the smallest value, starts at -1 as it is an invalid position
50+
unsigned long int MinValue = 4294967295; // Variable that stores the smallest value found, starts with 'infinity', so always on the first pass the value will be smaller than this variable
51+
// For that loops through all rows in column [0]
5252
for(int j = 1; j < n; j++){
53-
// Se o vertice ainda não foi visitado e o valor for menor que o 'MinValor'
54-
if( !visitados[j] && matriz[j][0] < MinValor ){
55-
min = j; // Guarda a posição do menor
56-
MinValor = matriz[j][0]; // Guarda o menor valor
53+
// If the vertex has not yet been visited and the value is less than the 'MinValor'
54+
if( !visited[j] && matrix[j][0] < MinValue ){
55+
min = j; // Saves the position of the smallest
56+
MinValue = matrix[j][0]; // Save the smallest value
5757
}
5858
}
5959

60-
visitados[min] = true; // Marca o valor a posição do minimo como visitado
60+
visited[min] = true; // Mark the value of the minimum position as visited
6161

62-
// For de 1 até n
62+
// Goes from 1 to n
6363
for(int j = 1; j < n; j++){
64-
// Se o valor da coluna [0] + o valor da coluna que está passando for menor que o valor da linha que está passando e coluna [0]
65-
// Atualiza a primeira coluna da matriz, que será utilizado para as próximas iterações
66-
if( (matriz[min][0] + matriz[min][j]) < matriz[j][0] ){
67-
matriz[j][0] = matriz[min][0] + matriz[min][j];
64+
// If the value of column [0] + the value of the passing column is less than the value of the passing row and column [0]
65+
// Update the first column of the matrix, which will be used for the next iterations
66+
if( (matrix[min][0] + matrix[min][j]) < matrix[j][0] ){
67+
matrix[j][0] = matrix[min][0] + matrix[min][j];
6868
}
6969
}
7070
}
7171
}
7272

7373
int main(){
7474

75-
unsigned long int Matriz[nroVertices][nroVertices] = {{ 0, 6, 10, 4294967295, 4294967295 },
75+
unsigned long int Matrix[noVertices][noVertices] = {{ 0, 6, 10, 4294967295, 4294967295 },
7676
{ 6, 0, 4294967295, 2, 4294967295 },
7777
{ 10, 4294967295, 0, 1, 3 },
7878
{ 4294967295, 2, 1, 0, 8 },
7979
{ 4294967295, 4294967295, 3, 8, 0 }};
8080

81-
Dijkstra(Matriz, nroVertices);
81+
Dijkstra(Matrix, noVertices);
8282

83-
printf("Total caminho mais curto do vertice 0 ao 4: %lu\n", Matriz[4][0]); // Caminho total mais curto
83+
printf("Total shortest path from vertex 0 to 4: %lu\n", Matrix[4][0]); // Shortest total path
8484

85-
// Da print na matriz com os valores atualizados
86-
printf("Matriz:\n");
87-
for (int i = 0; i < nroVertices; ++i){
88-
for (int e = 0; e < nroVertices; ++e){
89-
if( Matriz[i][e] < 10 )
90-
printf(" %lu ", Matriz[i][e]);
85+
// Print the matrix with the updated values
86+
printf("Matrix:\n");
87+
for (int i = 0; i < noVertices; ++i){
88+
for (int e = 0; e < noVertices; ++e){
89+
if( Matrix[i][e] < 10 )
90+
printf(" %lu ", Matrix[i][e]);
9191
else
92-
printf("%lu ", Matriz[i][e]);
92+
printf("%lu ", Matrix[i][e]);
9393
}
9494
printf("\n");
9595
}

src/c/BinarySearch.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include<stdio.h>
2+
3+
int BinarySearch(int array[], int size, int value) {
4+
int start = 0;
5+
int end = size - 1;
6+
int middle = end / 2;
7+
8+
while (start < end && array[middle] != value) {
9+
// new start
10+
if (value > array[middle])
11+
start = middle + 1;
12+
13+
// new end
14+
if (value < array[middle])
15+
end = middle - 1;
16+
17+
// new middle
18+
middle = (start + end) / 2;
19+
}
20+
21+
if (array[middle] == value)
22+
return middle;
23+
24+
return -1;
25+
}
26+
27+
int main() {
28+
int value;
29+
int array[] = {1, 5, 10, 12, 18, 22, 87, 90, 112, 129};
30+
size_t size = sizeof(array) / sizeof(array[0]);
31+
32+
printf("Please provide the number you want to value for: ");
33+
scanf("%d", &value);
34+
35+
int pos = BinarySearch(array, size, value);
36+
37+
if (pos != -1)
38+
printf("Found in position = %d.\nValue = %d\n", pos, array[pos]);
39+
else
40+
printf("Not found\n");
41+
42+
return 0;
43+
}

src/c/MinMaxDC.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <stdio.h>
2+
3+
/**
4+
* Function to find both the minimum and maximum elements in an array
5+
*
6+
* @param arr The array to search in
7+
* @param left The left index of the sub-array.
8+
* @param right The right index of the sub-array
9+
* @param min Pointer to store the minimum element
10+
* @param max Pointer to store the maximum element
11+
*/
12+
13+
void MinAndMax(int arr[], int left, int right, int* min, int* max) {
14+
// if there is only one element in the sub-array, set it as both min and max
15+
if (left == right) {
16+
*min = *max = arr[left];
17+
return;
18+
}
19+
20+
// if there are two elements in the sub-array, compare and set min and max
21+
if (right - left == 1) {
22+
if (arr[left] < arr[right]) {
23+
*min = arr[left];
24+
*max = arr[right];
25+
} else {
26+
*min = arr[right];
27+
*max = arr[left];
28+
}
29+
return;
30+
}
31+
32+
// calculate the middle index of the sub-array
33+
int mid = (left + right) / 2;
34+
int leftMin, leftMax, rightMin, rightMax;
35+
36+
// recursively find min and max in the left and right sub-arrays
37+
MinAndMax(arr, left, mid, &leftMin, &leftMax);
38+
MinAndMax(arr, mid + 1, right, &rightMin, &rightMax);
39+
40+
// update the minimum and maximum values
41+
*min = (leftMin < rightMin) ? leftMin : rightMin;
42+
*max = (leftMax > rightMax) ? leftMax : rightMax;
43+
}
44+
45+
int main() {
46+
int arr[] = {10, 5, 20, 8, 15, 30, -12, 24};
47+
int arrSize = sizeof(arr) / sizeof(arr[0]);
48+
49+
int min, max;
50+
51+
MinAndMax(arr, 0, arrSize - 1, &min, &max);
52+
53+
printf("Minimum element: %d\n", min);
54+
printf("Maximum element: %d\n", max);
55+
56+
return 0;
57+
}

0 commit comments

Comments
 (0)