Migration of Go (#215)
parent
a9745378c6
commit
799b40b7e3
|
@ -0,0 +1,27 @@
|
||||||
|
name: Go
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ main ]
|
||||||
|
paths: '**.go'
|
||||||
|
pull_request:
|
||||||
|
branches: [ main ]
|
||||||
|
paths: '**.go'
|
||||||
|
jobs:
|
||||||
|
Go:
|
||||||
|
name: Go
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- run: go list -e -json -compiled=true -test=true -export=true -deps=true -find=false -- ./...
|
||||||
|
- name: golangci-lint
|
||||||
|
uses: golangci/golangci-lint-action@v2
|
||||||
|
with:
|
||||||
|
version: latest
|
||||||
|
|
||||||
|
# args: ./...
|
||||||
|
# Optional: working directory, useful for monorepos
|
||||||
|
# working-directory: algorithms/Go
|
||||||
|
|
||||||
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
|
# only-new-issues: true
|
||||||
|
- run: go test $(go list ./...)
|
|
@ -0,0 +1,13 @@
|
||||||
|
# Go
|
||||||
|
|
||||||
|
## Scheduling
|
||||||
|
1. [Interval Scheduling](scheduling/interval-scheduling.go)
|
||||||
|
|
||||||
|
## Searching
|
||||||
|
1. [Binary Search](searching/binary-search.go)
|
||||||
|
2. [Linear Search](searching/linear-search.go)
|
||||||
|
|
||||||
|
## Sorting
|
||||||
|
1. [Bubble Sort](sorting/bubble-sort.go)
|
||||||
|
2. [Insertion Sort](sorting/insertion-sort.go)
|
||||||
|
3. [Quicksort](sorting/quicksort.go)
|
|
@ -5,7 +5,7 @@
|
||||||
Algorithm Type: Greedy
|
Algorithm Type: Greedy
|
||||||
Time Complexity: O(n*log(n))
|
Time Complexity: O(n*log(n))
|
||||||
*/
|
*/
|
||||||
package main
|
package scheduling
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -29,7 +29,7 @@ func getOptSchedule(jobs [][]int) []int {
|
||||||
return optSchedule
|
return optSchedule
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func RunIntervalScheduling() {
|
||||||
jobs := [][]int{
|
jobs := [][]int{
|
||||||
{0, 2, 8}, // [job_id, start_time, finish_time]
|
{0, 2, 8}, // [job_id, start_time, finish_time]
|
||||||
{1, 6, 10},
|
{1, 6, 10},
|
|
@ -0,0 +1,37 @@
|
||||||
|
package searching
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func binarySearch(arr []int, elem int) {
|
||||||
|
var (
|
||||||
|
start = 0
|
||||||
|
end = len(arr) - 1
|
||||||
|
)
|
||||||
|
|
||||||
|
fmt.Printf("Array: %v\nNumber to be searched: %v\n", arr, elem)
|
||||||
|
|
||||||
|
for start <= end {
|
||||||
|
mid := start + (end-start)/2
|
||||||
|
if elem == arr[mid] {
|
||||||
|
fmt.Printf("Number found at index %v", mid)
|
||||||
|
return
|
||||||
|
} else if elem < arr[mid] {
|
||||||
|
end = mid - 1
|
||||||
|
} else {
|
||||||
|
start = mid + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Number not found!\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunBinarySearch() {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
arr := []int{1, 2, 4, 7, 8, 9, 12, 14, 17, 18, 20, 22, 23, 24, 26, 27, 29}
|
||||||
|
elem := rand.Intn(20)
|
||||||
|
binarySearch(arr, elem)
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package searching
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -6,13 +6,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func linearSearch(arr []int, elem int) {
|
||||||
rand.Seed(time.Now().UnixNano())
|
n := len(arr)
|
||||||
var (
|
|
||||||
arr = rand.Perm(20)
|
|
||||||
elem = rand.Intn(30)
|
|
||||||
n = len(arr)
|
|
||||||
)
|
|
||||||
|
|
||||||
fmt.Printf("Array: %v\nElement: %v\n", arr, elem)
|
fmt.Printf("Array: %v\nElement: %v\n", arr, elem)
|
||||||
|
|
||||||
|
@ -24,3 +19,10 @@ func main() {
|
||||||
}
|
}
|
||||||
fmt.Printf("Element not found!")
|
fmt.Printf("Element not found!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RunLinearSearch() {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
arr := rand.Perm(20)
|
||||||
|
elem := rand.Intn(30)
|
||||||
|
linearSearch(arr, elem)
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
Bubble sort is a recursive algorithm based on swapping 2 values closest to each other: k and k+1
|
||||||
|
We start by checking the first two values of the array, and we swap them if the first value is bigger than the second one.
|
||||||
|
Then we recursively swap the second value with the third, if needed and so on until the array is sorted.
|
||||||
|
Average Time Complexity: O(n^2))
|
||||||
|
*/
|
||||||
|
|
||||||
|
package sorting
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func bubbleSort(sliceInt []int, n int) {
|
||||||
|
for k := 0; k < n-1; k++ {
|
||||||
|
if sliceInt[k] > sliceInt[k+1] {
|
||||||
|
// We swaps the value if sliceInt[k] > sliceInt[k+1]
|
||||||
|
sliceInt[k], sliceInt[k+1] = sliceInt[k+1], sliceInt[k]
|
||||||
|
bubbleSort(sliceInt, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunBubbleSort() {
|
||||||
|
sliceInt := []int{1, 2, -1, 0, 534, -100, 9, 53, 203}
|
||||||
|
bubbleSort(sliceInt, len(sliceInt))
|
||||||
|
fmt.Println(sliceInt)
|
||||||
|
}
|
|
@ -7,7 +7,7 @@
|
||||||
* O(n^2) runtime (the deck is sorted in descending order).
|
* O(n^2) runtime (the deck is sorted in descending order).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package main
|
package sorting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -26,7 +26,7 @@ func insertionSort(arr []int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func RunInsertionSort() {
|
||||||
arr := []int{10, 1, 6, 256, 2, 53, 235, 53, 1, 7, 23}
|
arr := []int{10, 1, 6, 256, 2, 53, 235, 53, 1, 7, 23}
|
||||||
fmt.Println("Unsorted Array:", arr)
|
fmt.Println("Unsorted Array:", arr)
|
||||||
insertionSort(arr)
|
insertionSort(arr)
|
|
@ -6,7 +6,7 @@
|
||||||
For details explanation and proof of correctness check this -> https://cs.pomona.edu/classes/cs140/pdf/l09-quicksort-proof.pdf
|
For details explanation and proof of correctness check this -> https://cs.pomona.edu/classes/cs140/pdf/l09-quicksort-proof.pdf
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package main
|
package sorting
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -37,7 +37,7 @@ func quickSort(arr []int) []int {
|
||||||
return sortedArr
|
return sortedArr
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func RunQuickSort() {
|
||||||
arr := []int{10, 1, 6, 256, 2, 53, 235, 53, 1, 7, 23}
|
arr := []int{10, 1, 6, 256, 2, 53, 235, 53, 1, 7, 23}
|
||||||
fmt.Printf("Unsorted Array: %v\nSorted Array: %v", arr, quickSort(arr))
|
fmt.Printf("Unsorted Array: %v\nSorted Array: %v", arr, quickSort(arr))
|
||||||
}
|
}
|
|
@ -3,7 +3,3 @@
|
||||||
### Python
|
### Python
|
||||||
|
|
||||||
1. [Interval Scheduling](python/interval-scheduling.py)
|
1. [Interval Scheduling](python/interval-scheduling.py)
|
||||||
|
|
||||||
### Golang
|
|
||||||
1. [Interval Scheduling](go/interval-scheduling.go)
|
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,3 @@
|
||||||
3. [Jump Search](c-or-cpp/jump-search.cpp)
|
3. [Jump Search](c-or-cpp/jump-search.cpp)
|
||||||
4. [finding squareroot using binary search](c-or-cpp/sqrt-monotonic-binary-search.cpp)
|
4. [finding squareroot using binary search](c-or-cpp/sqrt-monotonic-binary-search.cpp)
|
||||||
5. [Interpolation Search](c-or-cpp/interpolation-search.cpp)
|
5. [Interpolation Search](c-or-cpp/interpolation-search.cpp)
|
||||||
|
|
||||||
### Golang
|
|
||||||
|
|
||||||
1. [Linear Search](go/linear-search.go)
|
|
||||||
2. [Binary Search](go/binary-search.go)
|
|
||||||
|
|
|
@ -1,33 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"math/rand"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
|
||||||
var (
|
|
||||||
arr = []int{1, 2, 4, 7, 8, 9, 12, 14, 17, 18, 20, 22, 23, 24, 26, 27, 29}
|
|
||||||
start = 0
|
|
||||||
end = len(arr) - 1
|
|
||||||
elem = rand.Intn(20)
|
|
||||||
)
|
|
||||||
|
|
||||||
fmt.Printf("Array: %v\nElement: %v\n", arr, elem)
|
|
||||||
|
|
||||||
for start <= end {
|
|
||||||
mid := start + (end-start)/2
|
|
||||||
if elem == arr[mid] {
|
|
||||||
fmt.Printf("Element found at index %v", mid)
|
|
||||||
return
|
|
||||||
} else if elem < arr[mid] {
|
|
||||||
end = mid - 1
|
|
||||||
} else {
|
|
||||||
start = mid + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Element not found!\n")
|
|
||||||
}
|
|
|
@ -14,9 +14,3 @@
|
||||||
10. [Shell Sort](c-or-cpp/shell-sort.cpp)
|
10. [Shell Sort](c-or-cpp/shell-sort.cpp)
|
||||||
11. [Comb Sort](c-or-cpp/comb-sort.cpp)
|
11. [Comb Sort](c-or-cpp/comb-sort.cpp)
|
||||||
12. [3 Way Quick Sort](c-or-cpp/3way_quick_sort.cpp)
|
12. [3 Way Quick Sort](c-or-cpp/3way_quick_sort.cpp)
|
||||||
|
|
||||||
### Golang
|
|
||||||
1. [Insertion Sort](go/insertion-sort.go)
|
|
||||||
2. [Quick Sort](go/quick-sort.go)
|
|
||||||
3. [Bubble Sort](go/bubble-sort.go)
|
|
||||||
|
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
/*
|
|
||||||
Bubble sort is a recursive algorithm based on swapping 2 values closest to each other: k and k+1
|
|
||||||
We start by checking the first two values of the array, and we swap them if the first value is bigger than the second one.
|
|
||||||
Then we recursively swap the second value with the third, if needed and so on until the array is sorted.
|
|
||||||
Average Time Complexity: O(n^2))
|
|
||||||
*/
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func bubbleSort(sliceInt []int, n int) {
|
|
||||||
for k := 0; k < n-1; k++ {
|
|
||||||
if sliceInt[k] > sliceInt[k+1] {
|
|
||||||
// We swaps the value if sliceInt[k] > sliceInt[k+1]
|
|
||||||
sliceInt[k], sliceInt[k+1] = sliceInt[k+1], sliceInt[k]
|
|
||||||
bubbleSort(sliceInt, n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
sliceInt := []int{1, 2, -1, 0, 534, -100, 9, 53, 203}
|
|
||||||
bubbleSort(sliceInt, len(sliceInt))
|
|
||||||
fmt.Println(sliceInt)
|
|
||||||
}
|
|
Loading…
Reference in New Issue