Skip to content

Commit c6da1a4

Browse files
committed
Translate Go algorithm comments from Portuguese to English
1 parent 117af42 commit c6da1a4

13 files changed

+330
-327
lines changed

src/go/.vscode/settings.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"editor.formatOnSave": false,
3+
"editor.formatOnSaveMode": "modificationsIfAvailable"
4+
}

src/go/bubble_sort.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,34 @@ package main
22

33
import "fmt"
44

5-
// Iterativo
6-
func BubbleSortIterativo(slice []int) {
7-
for indice1 := len(slice) - 1; indice1 > 0; indice1-- {
8-
for indice2 := 0; indice2 < indice1; indice2++ {
9-
if slice[indice2] > slice[indice2+1] {
10-
slice[indice2], slice[indice2+1] = slice[indice2+1], slice[indice2]
5+
// Iterative
6+
func BubbleSortIterative(slice []int) {
7+
for index1 := len(slice) - 1; index1 > 0; index1-- {
8+
for index2 := 0; index2 < index1; index2++ {
9+
if slice[index2] > slice[index2+1] {
10+
slice[index2], slice[index2+1] = slice[index2+1], slice[index2]
1111
}
1212
}
1313
}
1414
}
1515

16-
// Recursivo
17-
func BubbleSortRecursivo(slice []int, tamanho int) {
18-
trocas := 0
19-
for indice := 0; indice < tamanho-1; indice++ {
20-
if slice[indice] > slice[indice+1] {
21-
slice[indice], slice[indice+1] = slice[indice+1], slice[indice]
22-
trocas++
16+
// Recursive
17+
func BubbleSortRecursive(slice []int, size int) {
18+
swaps := 0
19+
for index := 0; index < size-1; index++ {
20+
if slice[index] > slice[index+1] {
21+
slice[index], slice[index+1] = slice[index+1], slice[index]
22+
swaps++
2323
}
2424
}
25-
if trocas != 0 {
26-
BubbleSortRecursivo(slice, tamanho-1)
25+
if swaps != 0 {
26+
BubbleSortRecursive(slice, size-1)
2727
}
2828
}
2929

3030
func main() {
3131
slice := []int{5, 2, 1, 6, 9, 8, 7, 3, 4}
3232
fmt.Println("Slice:", slice)
33-
BubbleSortIterativo(slice)
33+
BubbleSortIterative(slice)
3434
fmt.Println("BubbleSort:", slice)
3535
}

src/go/cocktail_sort.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import "fmt"
55
func CocktailSort(slice []int) {
66

77
swapped := true
8-
begining := 0
8+
beginning := 0
99
ending := len(slice) - 1
1010

11-
for begining < ending && swapped {
11+
for beginning < ending && swapped {
1212

13-
for index := begining; index < ending; index++ {
13+
for index := beginning; index < ending; index++ {
1414
if slice[index] > slice[index+1] {
1515
slice[index], slice[index+1] = slice[index+1], slice[index]
1616
swapped = true
@@ -21,15 +21,15 @@ func CocktailSort(slice []int) {
2121

2222
if swapped {
2323
swapped = false
24-
for index := ending; index > begining; index-- {
24+
for index := ending; index > beginning; index-- {
2525
if slice[index] < slice[index-1] {
2626
slice[index], slice[index-1] = slice[index-1], slice[index]
2727
swapped = true
2828
}
2929
}
3030
}
3131

32-
begining++
32+
beginning++
3333
}
3434
}
3535

src/go/dijkstra.go

+36-36
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
* Grafos - Algoritmo de Dijkstra em Go
3-
* Complexidade: Teta(n^2)
4-
* Implementação utilizando matriz de adjacências (matriz de distância)
2+
* Graphs - Dijkstra's Algorithm in Go
3+
* Complexity: Theta(n^2)
4+
* Implementation using adjacency matrix (distance matrix)
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+
* One to all - Non-negative weighted edges - 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,18 +19,18 @@
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-
* O objetivo é sair do ponto inicial (0) e chegar ao destino (4) pelo caminho mais curto
31-
* Resposta: (0)->(1)->(3)->(2)->(4) = 12
30+
* The goal is to start from the initial point (0) and reach the destination (4) by the shortest path
31+
* Answer: (0)->(1)->(3)->(2)->(4) = 12
3232
*
33-
* link Go PlayGround: https://play.golang.org/p/HyWAcYJ3qXY
33+
* Go PlayGround link: https://play.golang.org/p/HyWAcYJ3qXY
3434
*/
3535

3636
package main
@@ -39,56 +39,56 @@ import "fmt"
3939

4040
var nroVertices = 5
4141

42-
type Matriz [][]int
42+
type Matrix [][]int
4343

4444
var maxInt = 4294967295
4545

46-
// Algoritmo de Dijkstra recebe como parâmetro a matriz de distância e o número de vértices
47-
func Dijkstra(matriz Matriz, n int) {
48-
visitados := make([]bool, n) // Variável que guarda true para os vértices visitados
49-
// O valor 'i' do for abaixo não é utilizado, pois o for serve apenas para percorrer todo o número de colunas da matriz
50-
for i := 1; i < n; i++ { // Começa em 1 pois não precisa comparar o vértice com ele mesmo
46+
// Dijkstra's Algorithm takes the distance matrix and the number of vertices as parameters
47+
func Dijkstra(matrix Matrix, n int) {
48+
visited := make([]bool, n) // Variable that stores true for visited vertices
49+
// The 'i' value in the for loop below is not used, as the loop is only used to iterate over the number of columns in the matrix
50+
for i := 1; i < n; i++ { // Starts at 1 because there is no need to compare the vertex with itself
5151

52-
min := -1 // Variável que guarda a posição do menor valor, inicia em -1 pois é uma posição inválida
53-
minValor := maxInt // Variável que guarda o menor valor encontrado, inicia com 'infinito', assim, sempre na primeira passada o valor será menor que esta variável
52+
min := -1 // Variable that stores the position of the smallest value, starts at -1 because it is an invalid position
53+
minValue := maxInt // Variable that stores the smallest value found, starts with 'infinity', so in the first pass the value will be smaller than this variable
5454

55-
// For que percorre todas as linhas na coluna [0]
55+
// For loop that iterates over all rows in column [0]
5656
for j := 1; j < n; j++ {
57-
// Se o vertice ainda não foi visitado e o valor for menor que o 'MinValor'
58-
if !visitados[j] && matriz[j][0] < minValor {
59-
min = j // Guarda a posição do menor
60-
minValor = matriz[j][0] // Guarda o menor valor
57+
// If the vertex has not been visited yet and the value is smaller than 'minValue'
58+
if !visited[j] && matrix[j][0] < minValue {
59+
min = j // Stores the position of the smallest value
60+
minValue = matrix[j][0] // Stores the smallest value
6161
}
6262
}
6363

64-
visitados[min] = true // Marca o valor a posição do minimo como visitado
64+
visited[min] = true // Marks the position of the minimum as visited
6565

66-
// For de 1 até n
66+
// For loop from 1 to n
6767
for j := 1; j < n; j++ {
68-
// 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]
69-
// Atualiza a primeira coluna da matriz, que será utilizado para as próximas iterações
70-
if (matriz[min][0] + matriz[min][j]) < matriz[j][0] {
71-
matriz[j][0] = matriz[min][0] + matriz[min][j]
68+
// If the value of column [0] + the value of the current column is smaller than the value of the current row and column [0]
69+
// Updates the first column of the matrix, which will be used for the next iterations
70+
if (matrix[min][0] + matrix[min][j]) < matrix[j][0] {
71+
matrix[j][0] = matrix[min][0] + matrix[min][j]
7272
}
7373
}
7474
}
7575
}
7676

7777
func main() {
78-
matriz := Matriz{
78+
matrix := Matrix{
7979
{0, 6, 10, maxInt, maxInt},
8080
{6, 0, maxInt, 2, maxInt},
8181
{10, maxInt, 0, 1, 3},
8282
{maxInt, 2, 1, 0, 8},
8383
{maxInt, maxInt, 3, 8, 0},
8484
}
8585

86-
Dijkstra(matriz, nroVertices)
86+
Dijkstra(matrix, nroVertices)
8787

88-
fmt.Printf("Total caminho mais curto do vertice 0 ao 4: %v\n\n", matriz[4][0]) // Caminho total mais curto
88+
fmt.Printf("Total shortest path from vertex 0 to 4: %v\n\n", matrix[4][0]) // Total shortest path
8989

90-
// Da print na matriz com os valores atualizados
91-
fmt.Println("Matriz:")
90+
// Prints the matrix with the updated values
91+
fmt.Println("Matrix:")
9292

9393
fmt.Printf("- | |v0 |v1 |v2 |v3 |v4\n")
9494
fmt.Println("_____________________________________________")
@@ -100,10 +100,10 @@ func main() {
100100
fmt.Printf("v%v |", i)
101101
}
102102

103-
if matriz[i][j] == maxInt {
103+
if matrix[i][j] == maxInt {
104104
fmt.Printf(" |inf")
105105
} else {
106-
fmt.Printf(" |%v", matriz[i][j])
106+
fmt.Printf(" |%v", matrix[i][j])
107107
}
108108
}
109109
fmt.Println()

src/go/exponentiation.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package main
22

33
import "fmt"
44

5-
func Exponenciacao(base int, expoente int) int {
6-
for index := 0; index < (expoente - 1); index++ {
7-
base *= expoente
5+
func Exponentiation(base int, exponent int) int {
6+
for index := 0; index < (exponent - 1); index++ {
7+
base *= exponent
88
}
99
return base
1010
}
1111

1212
func main() {
13-
fmt.Println("Exponenciacao:", Exponenciacao(5, 5))
13+
fmt.Println("Exponentiation:", Exponentiation(5, 5))
1414
}

src/go/factorial.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package main
22

33
import "fmt"
44

5-
func Fatorial(value int) int {
5+
func Factorial(value int) int {
66
if value == 1 {
77
return 1
88
}
9-
return value * Fatorial(value-1)
9+
return value * Factorial(value-1)
1010
}
1111

1212
func main() {
13-
fmt.Println("Fatorial:", Fatorial(6))
13+
fmt.Println("Factorial:", Factorial(6))
1414
}

src/go/floyd_warshall.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ package main
22

33
import "fmt"
44

5-
// Grafos - Algoritmo de Floyd-Warshall em GO
6-
// link Go PlayGround: https://go.dev/play/p/tIRTHkNf7Fz
5+
// Graphs - Floyd-Warshall Algorithm in GO
6+
// Go PlayGround link: https://go.dev/play/p/tIRTHkNf7Fz
77

8-
// Algoritmo de Floyd-Warshall
8+
// Floyd-Warshall Algorithm
99
func FloydWarshall(graph [][]int) [][]int {
10-
// Inicializa a matriz de distancias
10+
// Initialize the distance matrix
1111
dist := make([][]int, len(graph))
1212
for i := range dist {
1313
dist[i] = make([]int, len(graph))
1414
copy(dist[i], graph[i])
1515
}
1616

17-
// Percorre os vértices
17+
// Traverse the vertices
1818
for k := 0; k < len(graph); k++ {
19-
// Percorre as linhas
19+
// Traverse the rows
2020
for i := 0; i < len(graph); i++ {
21-
// Percorre as colunas
21+
// Traverse the columns
2222
for j := 0; j < len(graph); j++ {
23-
// Verifica se o caminho passando pelo vértice k é menor
23+
// Check if the path passing through vertex k is shorter
2424
if dist[i][k]+dist[k][j] < dist[i][j] {
2525
dist[i][j] = dist[i][k] + dist[k][j]
2626
}

src/go/min_max_recursive.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package main
22

33
import "fmt"
44

5-
func MaximoDivisaoEConquista(vetor []int, inicio int, fim int) int {
6-
if inicio == fim {
7-
return vetor[inicio]
5+
func MaxDivideAndConquer(array []int, start int, end int) int {
6+
if start == end {
7+
return array[start]
88
}
99

10-
meio := (inicio + fim) / 2
11-
aux1 := MaximoDivisaoEConquista(vetor, inicio, meio)
12-
aux2 := MaximoDivisaoEConquista(vetor, meio+1, fim)
10+
middle := (start + end) / 2
11+
aux1 := MaxDivideAndConquer(array, start, middle)
12+
aux2 := MaxDivideAndConquer(array, middle+1, end)
1313

1414
if aux1 > aux2 {
1515
return aux1
@@ -18,22 +18,22 @@ func MaximoDivisaoEConquista(vetor []int, inicio int, fim int) int {
1818
return aux2
1919
}
2020

21-
func MinimoMaximoRecursivo(vetor []int, minimo int, maximo int, indice int) {
22-
if vetor[indice] < minimo {
23-
minimo = vetor[indice]
21+
func RecursiveMinMax(array []int, min int, max int, index int) {
22+
if array[index] < min {
23+
min = array[index]
2424
}
25-
if vetor[indice] > maximo {
26-
maximo = vetor[indice]
25+
if array[index] > max {
26+
max = array[index]
2727
}
28-
if indice < len(vetor)-1 {
29-
MinimoMaximoRecursivo(vetor, minimo, maximo, indice+1)
28+
if index < len(array)-1 {
29+
RecursiveMinMax(array, min, max, index+1)
3030
} else {
31-
fmt.Println("Minimo:", minimo)
32-
fmt.Println("Maximo:", maximo)
31+
fmt.Println("Minimum:", min)
32+
fmt.Println("Maximum:", max)
3333
}
3434
}
3535

3636
func main() {
3737
slice := []int{2, 3, 9, 1, 6, 8, 5}
38-
MinimoMaximoRecursivo(slice, 999, 0, 0)
38+
RecursiveMinMax(slice, 999, 0, 0)
3939
}

0 commit comments

Comments
 (0)