From aa27a50d56e7a60901cf49ba054c08621c897cb4 Mon Sep 17 00:00:00 2001 From: leonardogonfiantini Date: Tue, 7 Mar 2023 14:16:29 +0100 Subject: [PATCH 1/3] Added new algorithm, reverse-array for go language --- algorithms/Go/arrays/reverse-array.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 algorithms/Go/arrays/reverse-array.go diff --git a/algorithms/Go/arrays/reverse-array.go b/algorithms/Go/arrays/reverse-array.go new file mode 100644 index 00000000..9a29e940 --- /dev/null +++ b/algorithms/Go/arrays/reverse-array.go @@ -0,0 +1,21 @@ +//Description : This program reverses array elements by swapping the first half part of the array +//Time Complexity : O(n/2), where n is the array size +//Auxiliary Space : O(1) + +package arrays + +import ("fmt") + +func TestReverseArray() { + // create an array of integers + arr := []int{1, 2, 3, 4, 5} + // get the length of the array + n := len(arr) + // iterate over half of the array and swap corresponding elements + for i := 0; i < n/2; i++ { + arr[i], arr[n-i-1] = arr[n-i-1], arr[i] + } + // print the reversed array + fmt.Println(arr) +} + From 449bd57f2b5c2f6b821935ae762980159e969b9d Mon Sep 17 00:00:00 2001 From: Leonardo Gonfiantini Date: Sat, 27 May 2023 12:16:19 +0200 Subject: [PATCH 2/3] Update reverse-array.go --- algorithms/Go/arrays/reverse-array.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/algorithms/Go/arrays/reverse-array.go b/algorithms/Go/arrays/reverse-array.go index 9a29e940..2d44ba21 100644 --- a/algorithms/Go/arrays/reverse-array.go +++ b/algorithms/Go/arrays/reverse-array.go @@ -6,9 +6,7 @@ package arrays import ("fmt") -func TestReverseArray() { - // create an array of integers - arr := []int{1, 2, 3, 4, 5} +func ReverseArray(arr int[]) { // get the length of the array n := len(arr) // iterate over half of the array and swap corresponding elements From a4edbaf506e66675bb94d043393047c6d289fefd Mon Sep 17 00:00:00 2001 From: Leonardo Gonfiantini Date: Sat, 27 May 2023 12:22:17 +0200 Subject: [PATCH 3/3] Update reverse-array.go --- algorithms/Go/arrays/reverse-array.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/algorithms/Go/arrays/reverse-array.go b/algorithms/Go/arrays/reverse-array.go index 2d44ba21..dd85705a 100644 --- a/algorithms/Go/arrays/reverse-array.go +++ b/algorithms/Go/arrays/reverse-array.go @@ -6,7 +6,7 @@ package arrays import ("fmt") -func ReverseArray(arr int[]) { +func ReverseArray(arr []int) { // get the length of the array n := len(arr) // iterate over half of the array and swap corresponding elements