diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 00000000..ee1a40f7 --- /dev/null +++ b/.github/workflows/go.yml @@ -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 ./...) diff --git a/algorithms/Go/README.md b/algorithms/Go/README.md new file mode 100644 index 00000000..ffa0542a --- /dev/null +++ b/algorithms/Go/README.md @@ -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) diff --git a/scheduling/go/interval-scheduling.go b/algorithms/Go/scheduling/interval-scheduling.go similarity index 95% rename from scheduling/go/interval-scheduling.go rename to algorithms/Go/scheduling/interval-scheduling.go index c95d02d5..ccf8fb20 100644 --- a/scheduling/go/interval-scheduling.go +++ b/algorithms/Go/scheduling/interval-scheduling.go @@ -5,7 +5,7 @@ Algorithm Type: Greedy Time Complexity: O(n*log(n)) */ -package main +package scheduling import ( "fmt" @@ -29,7 +29,7 @@ func getOptSchedule(jobs [][]int) []int { return optSchedule } -func main() { +func RunIntervalScheduling() { jobs := [][]int{ {0, 2, 8}, // [job_id, start_time, finish_time] {1, 6, 10}, diff --git a/algorithms/Go/searching/binary-search.go b/algorithms/Go/searching/binary-search.go new file mode 100644 index 00000000..a3e2a99c --- /dev/null +++ b/algorithms/Go/searching/binary-search.go @@ -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) +} diff --git a/searching/go/linear-search.go b/algorithms/Go/searching/linear-search.go similarity index 61% rename from searching/go/linear-search.go rename to algorithms/Go/searching/linear-search.go index 6410ed8c..dd6f325a 100644 --- a/searching/go/linear-search.go +++ b/algorithms/Go/searching/linear-search.go @@ -1,4 +1,4 @@ -package main +package searching import ( "fmt" @@ -6,13 +6,8 @@ import ( "time" ) -func main() { - rand.Seed(time.Now().UnixNano()) - var ( - arr = rand.Perm(20) - elem = rand.Intn(30) - n = len(arr) - ) +func linearSearch(arr []int, elem int) { + n := len(arr) fmt.Printf("Array: %v\nElement: %v\n", arr, elem) @@ -24,3 +19,10 @@ func main() { } fmt.Printf("Element not found!") } + +func RunLinearSearch() { + rand.Seed(time.Now().UnixNano()) + arr := rand.Perm(20) + elem := rand.Intn(30) + linearSearch(arr, elem) +} diff --git a/algorithms/Go/sorting/bubble-sort.go b/algorithms/Go/sorting/bubble-sort.go new file mode 100644 index 00000000..37eed220 --- /dev/null +++ b/algorithms/Go/sorting/bubble-sort.go @@ -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) +} diff --git a/sorting/go/insertion-sort.go b/algorithms/Go/sorting/insertion-sort.go similarity index 93% rename from sorting/go/insertion-sort.go rename to algorithms/Go/sorting/insertion-sort.go index 9b51738d..f6e6e698 100644 --- a/sorting/go/insertion-sort.go +++ b/algorithms/Go/sorting/insertion-sort.go @@ -7,7 +7,7 @@ * O(n^2) runtime (the deck is sorted in descending order). */ -package main +package sorting import ( "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} fmt.Println("Unsorted Array:", arr) insertionSort(arr) diff --git a/sorting/go/quick-sort.go b/algorithms/Go/sorting/quicksort.go similarity index 55% rename from sorting/go/quick-sort.go rename to algorithms/Go/sorting/quicksort.go index 890adede..5d055574 100644 --- a/sorting/go/quick-sort.go +++ b/algorithms/Go/sorting/quicksort.go @@ -1,12 +1,12 @@ -/* - Quick Sort is a divide and conquer algorithm. - First we choose a pivot and split the array in two parts, one containing all elements less than or equal to the pivot and other contains the rest (the pivot element is in neither of them) - Then we recursively sort the two arrays and finally concatenate them to get the sorted array. - Average Time Complexity: O(n*log(n)) - For details explanation and proof of correctness check this -> https://cs.pomona.edu/classes/cs140/pdf/l09-quicksort-proof.pdf +/* + Quick Sort is a divide and conquer algorithm. + First we choose a pivot and split the array in two parts, one containing all elements less than or equal to the pivot and other contains the rest (the pivot element is in neither of them) + Then we recursively sort the two arrays and finally concatenate them to get the sorted array. + Average Time Complexity: O(n*log(n)) + 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 ( "fmt" @@ -37,7 +37,7 @@ func quickSort(arr []int) []int { return sortedArr } -func main() { +func RunQuickSort() { arr := []int{10, 1, 6, 256, 2, 53, 235, 53, 1, 7, 23} fmt.Printf("Unsorted Array: %v\nSorted Array: %v", arr, quickSort(arr)) } diff --git a/scheduling/README.md b/scheduling/README.md index 2ad227ad..bd30ba55 100644 --- a/scheduling/README.md +++ b/scheduling/README.md @@ -3,7 +3,3 @@ ### Python 1. [Interval Scheduling](python/interval-scheduling.py) - -### Golang -1. [Interval Scheduling](go/interval-scheduling.go) - diff --git a/searching/README.md b/searching/README.md index 7490bf4c..71f16957 100644 --- a/searching/README.md +++ b/searching/README.md @@ -7,8 +7,3 @@ 3. [Jump Search](c-or-cpp/jump-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) - -### Golang - -1. [Linear Search](go/linear-search.go) -2. [Binary Search](go/binary-search.go) diff --git a/searching/go/binary-search.go b/searching/go/binary-search.go deleted file mode 100644 index 0c78157a..00000000 --- a/searching/go/binary-search.go +++ /dev/null @@ -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") -} diff --git a/sorting/README.md b/sorting/README.md index 74853dfd..0eedfc6f 100644 --- a/sorting/README.md +++ b/sorting/README.md @@ -14,9 +14,3 @@ 10. [Shell Sort](c-or-cpp/shell-sort.cpp) 11. [Comb Sort](c-or-cpp/comb-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) - diff --git a/sorting/go/bubble-sort.go b/sorting/go/bubble-sort.go deleted file mode 100644 index 43135763..00000000 --- a/sorting/go/bubble-sort.go +++ /dev/null @@ -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) -}