Skip to content

Commit a4ba99e

Browse files
committed
Merge branch 'master' of https://github.com/brayo-pip/Go
2 parents a516ad1 + 3a6ca15 commit a4ba99e

File tree

4 files changed

+35
-38
lines changed

4 files changed

+35
-38
lines changed

sorts/bubble_sort.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
//Package sorts a package for demonstrating sorting algorithms in Go
22
package sorts
33

4-
func bubbleSort(arrayzor []int) []int {
5-
6-
7-
swapped := true
8-
for swapped {
9-
swapped = false
10-
for i := 0; i < len(arrayzor)-1; i++ {
11-
if arrayzor[i+1] < arrayzor[i] {
12-
swap(arrayzor, i, i+1)
13-
swapped = true
14-
}
4+
func bubbleSort(array []int)[]int{
5+
for a:=0; a<len(array)-1;a++{
6+
for i:=a;i<len(array)-1;i++{
7+
j:=i+1
8+
array =swap(array, i,j)
159
}
1610
}
17-
return arrayzor
18-
}
11+
return array
1912

20-
func swap(arrayzor []int, i, j int) {
21-
tmp := arrayzor[j]
22-
arrayzor[j] = arrayzor[i]
23-
arrayzor[i] = tmp
2413
}
14+
15+
func swap(array []int, i int, j int)[]int{
16+
if array[i]>array[j]{
17+
array[i],array[j] =array[j],array[i]
18+
}
19+
return array
20+
21+
}

sorts/heap_sort.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
//Package sorts a package for demonstrating sorting algorithms in Go
22
package sorts
33

4-
type MaxHeap struct {
4+
type maxHeap struct {
55
slice []int
66
heapSize int
77
}
88

9-
func BuildMaxHeap(slice []int) MaxHeap {
10-
h := MaxHeap{slice: slice, heapSize: len(slice)}
9+
func buildMaxHeap(slice []int) maxHeap {
10+
h := maxHeap{slice: slice, heapSize: len(slice)}
1111
for i := len(slice) / 2; i >= 0; i-- {
1212
h.MaxHeapify(i)
1313
}
1414
return h
1515
}
1616

17-
func (h MaxHeap) MaxHeapify(i int) {
17+
func (h maxHeap) MaxHeapify(i int) {
1818
l, r := 2*i+1, 2*i+2
1919
max := i
2020

@@ -31,10 +31,10 @@ func (h MaxHeap) MaxHeapify(i int) {
3131
}
3232
}
3333

34-
func (h MaxHeap) size() int { return h.heapSize } // ???
34+
func (h maxHeap) size() int { return h.heapSize } // ???
3535

3636
func heapSort(slice []int) []int {
37-
h := BuildMaxHeap(slice)
37+
h := buildMaxHeap(slice)
3838
//log.Println(slice)
3939
for i := len(h.slice) - 1; i >= 1; i-- {
4040
h.slice[0], h.slice[i] = h.slice[i], h.slice[0]

sorts/quick_sort.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ func quickSort(arr []int) []int {
1313

1414
median := arr[rand.Intn(len(arr))]
1515

16-
low_part := make([]int, 0, len(arr))
17-
high_part := make([]int, 0, len(arr))
18-
middle_part := make([]int, 0, len(arr))
16+
lowPart := make([]int, 0, len(arr))
17+
highPart := make([]int, 0, len(arr))
18+
middlePart := make([]int, 0, len(arr))
1919

2020
for _, item := range arr {
2121
switch {
2222
case item < median:
23-
low_part = append(low_part, item)
23+
lowPart = append(lowPart, item)
2424
case item == median:
25-
middle_part = append(middle_part, item)
25+
middlePart = append(middlePart, item)
2626
case item > median:
27-
high_part = append(high_part, item)
27+
highPart = append(highPart, item)
2828
}
2929
}
3030

31-
low_part = quickSort(low_part)
32-
high_part = quickSort(high_part)
31+
lowPart = quickSort(lowPart)
32+
highPart = quickSort(highPart)
3333

34-
low_part = append(low_part, middle_part...)
35-
low_part = append(low_part, high_part...)
34+
lowPart = append(lowPart, middlePart...)
35+
lowPart = append(lowPart, highPart...)
3636

37-
return low_part
37+
return lowPart
3838
}

strings/naiveStringSearch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import (
1616

1717
func naivePatternSearch(text string, pattern string) []int {
1818
var positions []int
19-
for i := 0; i < len(text); i++ {
19+
for i := 0; i < len(text)-len(pattern); i++ {
2020
var match bool = true
21-
for j := 0; j < len(pattern); j++ {
22-
if text[i+j] != pattern[j] {
21+
j := i +(len(pattern))
22+
if text[i+j] != pattern[j] {
2323
match = false
2424
break
2525
}
26-
}
26+
2727
if match {
2828
positions = append(positions, i)
2929
}

0 commit comments

Comments
 (0)