Skip to content

Commit 31bad2b

Browse files
committed
Array Algorithms
1 parent cbb4190 commit 31bad2b

File tree

5 files changed

+305
-0
lines changed

5 files changed

+305
-0
lines changed

arrays/binarySearch.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
// Binary Search in an array of a given size
3+
// Prints if a given number is found in a sorted array and prints the index of the number if it is found
4+
*/
5+
6+
package main
7+
8+
import ("fmt"
9+
//"reflect"
10+
)
11+
12+
// Binary Search Iterative
13+
14+
func BinarySearchIterative(array *[]int,startIdx int,endIdx int, searchNum int) int {
15+
arr := *array
16+
var returnVal int
17+
fmt.Println("Using the iterative Binary Search approach")
18+
for (true) {
19+
mid := startIdx + (endIdx - startIdx)/2
20+
if (endIdx<startIdx) {
21+
//fmt.Println("Element not found")
22+
returnVal := -1
23+
return returnVal
24+
} else if arr[mid] == searchNum {
25+
//fmt.Println("Element found at index:",mid)
26+
returnVal := mid
27+
return returnVal
28+
} else if arr[mid] > searchNum {
29+
endIdx = mid-1
30+
} else {
31+
startIdx = mid+1
32+
}
33+
}
34+
return returnVal
35+
}
36+
37+
// Binary Search Recursive algorithm
38+
func BinarySearchRecursive(array *[]int,startIdx int,endIdx int, searchNum int) int {
39+
arr := *array
40+
var returnVal int
41+
//fmt.Println("Using the recursive Binary Search approach")
42+
mid := startIdx + (endIdx - startIdx)/2
43+
if (endIdx<startIdx) {
44+
//fmt.Println("Element not found")
45+
returnVal = -1
46+
return returnVal
47+
} else if (arr[mid] == searchNum) {
48+
//fmt.Println("Element found at Index:",mid)
49+
returnVal = mid
50+
return returnVal
51+
} else if (arr[mid] > searchNum) {
52+
returnVal = BinarySearchRecursive(array,startIdx,mid-1,searchNum)
53+
} else {
54+
returnVal = BinarySearchRecursive(array,mid+1,endIdx,searchNum)
55+
}
56+
return returnVal
57+
}
58+
59+
// Unit Test for Binary Search
60+
func main(){
61+
62+
/*
63+
var length,searchNum int
64+
fmt.Println("Enter the size of the array")
65+
fmt.Scanf("%d",&length)
66+
fmt.Println("Enter the ascending order of array elements seperated by a space")
67+
arr := make([]int,length)
68+
for i:=0;i<length;i++ {
69+
fmt.Scanln(&arr[i])
70+
}
71+
fmt.Println("Enter the number to search in the array")
72+
fmt.Scanf("%d",&searchNum)
73+
fmt.Println("Array:",arr)
74+
*/
75+
76+
arr := []int{1,10,20,47,59,63,75,88,99,107,120,133,155,162,176,188,199,200,210,222}
77+
searchNum := 155 //Solution is 12
78+
//search Num := 75 //Solution is 6
79+
80+
index1 := BinarySearchIterative(&arr,0,len(arr)-1,searchNum)
81+
fmt.Println("Number found at index:",index1)
82+
fmt.Println("Using the recursive Binary Search approach")
83+
index1 = BinarySearchRecursive(&arr,0,len(arr)-1,searchNum)
84+
fmt.Println("Number found at index:",index1)
85+
86+
}

arrays/binarySearch/binarySearch.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
// Binary Search in an array of a given size
3+
// Prints if a given number is found in a sorted array and prints the index of the number if it is found
4+
*/
5+
6+
package binarySearch
7+
8+
import ("fmt"
9+
//"reflect"
10+
)
11+
12+
// Binary Search Iterative
13+
14+
func BinarySearchIterative(array *[]int,startIdx int,endIdx int, searchNum int) int {
15+
arr := *array
16+
var returnVal int
17+
//fmt.Println("Using the iterative Binary Search approach")
18+
for (true) {
19+
mid := startIdx + (endIdx - startIdx)/2
20+
if (endIdx<startIdx) {
21+
//fmt.Println("Element not found")
22+
returnVal := -1
23+
return returnVal
24+
} else if arr[mid] == searchNum {
25+
//fmt.Println("Element found at index:",mid)
26+
returnVal := mid
27+
return returnVal
28+
} else if arr[mid] > searchNum {
29+
endIdx = mid-1
30+
} else {
31+
startIdx = mid+1
32+
}
33+
}
34+
return returnVal
35+
}
36+
37+
// Binary Search Recursive algorithm
38+
func BinarySearchRecursive(array *[]int,startIdx int,endIdx int, searchNum int) int {
39+
arr := *array
40+
var returnVal int
41+
//fmt.Println("Using the recursive Binary Search approach")
42+
mid := startIdx + (endIdx - startIdx)/2
43+
if (endIdx<startIdx) {
44+
//fmt.Println("Element not found")
45+
returnVal = -1
46+
return returnVal
47+
} else if (arr[mid] == searchNum) {
48+
//fmt.Println("Element found at Index:",mid)
49+
returnVal = mid
50+
return returnVal
51+
} else if (arr[mid] > searchNum) {
52+
returnVal = BinarySearchRecursive(array,startIdx,mid-1,searchNum)
53+
} else {
54+
returnVal = BinarySearchRecursive(array,mid+1,endIdx,searchNum)
55+
}
56+
return returnVal
57+
}
58+
59+
// Unit Test for Binary Search
60+
func main(){
61+
62+
var length,searchNum int
63+
fmt.Println("Enter the size of the array")
64+
fmt.Scanf("%d",&length)
65+
fmt.Println("Enter the ascending order of array elements seperated by a space")
66+
arr := make([]int,length)
67+
for i:=0;i<length;i++ {
68+
fmt.Scanln(&arr[i])
69+
}
70+
fmt.Println("Enter the number to search in the array")
71+
fmt.Scanf("%d",&searchNum)
72+
fmt.Println("Array:",arr)
73+
74+
BinarySearchIterative(&arr,0,length-1,searchNum)
75+
76+
fmt.Println("Using the recursive Binary Search approach")
77+
BinarySearchRecursive(&arr,0,length-1,searchNum)
78+
79+
}

arrays/maxInSlidingWindow.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Given a large array of integers and a window size to searh in, find the current max value in the window
3+
when sliding through the window and print it
4+
*/
5+
6+
package main
7+
8+
import ("fmt"
9+
"container/list"
10+
)
11+
12+
/*
13+
Function takes in the value of the array to search for and the window size and returns
14+
the maximum values
15+
*/
16+
17+
func maxInSlidingWindow(arr *[]int,windowSize int) {
18+
19+
/*
20+
Initializing a doublylinkedlist to store the index of the elements. We need
21+
a DS that can perform like a deque. Doubly Linkedlist perfectly accomplishes that
22+
*/
23+
dll := list.New()
24+
array := *arr
25+
26+
//fmt.Println(dll.Front())
27+
28+
//Traversing the array and adding the right elements to the dll
29+
// The first index in the dll is always the max in the current sliding window
30+
31+
for idx,val := range array {
32+
// This ensures that if we just started the array we fill the window first element with the first element of the array
33+
if dll.Front() != nil {
34+
e := dll.Back()
35+
for {
36+
if e != nil && val >= array[e.Value.(int)]{
37+
dll.Remove(e)
38+
e = dll.Back()
39+
} else {
40+
dll.PushBack(idx)
41+
break
42+
}
43+
}
44+
//Pop the first element in the window if it doesnt fall in the window any more
45+
e = dll.Front()
46+
if e.Value.(int) < (idx - windowSize + 1) {
47+
e = dll.Front()
48+
dll.Remove(e)
49+
}
50+
} else {
51+
e := dll.PushBack(idx)
52+
if e == nil {
53+
fmt.Println(e)
54+
}
55+
}
56+
// This condition ensure that we start printing only after we have visited all the elements in the First window
57+
if idx >= (windowSize-1) {
58+
e := dll.Front()
59+
fmt.Println("Current Sliding Window Max: ",array[e.Value.(int)])
60+
}
61+
}
62+
}
63+
64+
func main() {
65+
arr := []int{-4,2,-5,1,-1,6}
66+
67+
68+
maxInSlidingWindow(&arr,3)
69+
70+
//fmt.Println(dll)
71+
//fmt.Println(l.Front())
72+
//fmt.Println(l.Back())
73+
}

arrays/searchRotatedArray.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Given number in a sorted array that has been rotated by some number of times. Return -1 if the number does not exist or the index if it does exist
3+
No duplicate entries in the array. The current solution is only for unique entries
4+
*/
5+
6+
package main
7+
8+
import ("fmt"
9+
bS "AlgorithmsGolang/arrays/binarySearch"
10+
)
11+
12+
13+
func searchRotatedArrayRecursive(array *[]int, startIdx int,endIdx int, searchNum int) int {
14+
arr := *array
15+
var returnVal int
16+
mid := startIdx + (endIdx - startIdx)/2
17+
//Case 1 and Case 2 are cases of regular binarySearch
18+
if arr[mid] < searchNum && arr[endIdx] >= searchNum {
19+
returnVal = bS.BinarySearchIterative(array,mid+1,endIdx,searchNum)
20+
return returnVal
21+
} else if arr[mid] > searchNum && arr[startIdx] <= searchNum {
22+
returnVal = bS.BinarySearchIterative(array,startIdx,mid-1,searchNum)
23+
return returnVal
24+
} else if arr[mid] == searchNum {
25+
returnVal = mid
26+
return returnVal
27+
} else if arr[mid] < searchNum && arr[mid] < arr[startIdx] {
28+
returnVal = searchRotatedArrayRecursive(array,startIdx,mid-1,searchNum)
29+
return returnVal
30+
} else {
31+
returnVal = searchRotatedArrayRecursive(array,mid+1,endIdx,searchNum)
32+
return returnVal
33+
}
34+
return returnVal
35+
}
36+
37+
// Unit Test
38+
39+
func main() {
40+
41+
arr := []int{176,188,199,200,210,222,1,10,20,47,59,63,75,88,99,107,120,133,155,162}
42+
searchNum1 := 200
43+
searchNum2 := 176
44+
index1 := searchRotatedArrayRecursive(&arr,0,len(arr)-1,searchNum1)
45+
index2 := searchRotatedArrayRecursive(&arr,0,len(arr)-1,searchNum2)
46+
fmt.Println("searchNum1:",searchNum1,"is at Index:",index1)
47+
fmt.Println("searchNum2:",searchNum2,"is at Index:",index2)
48+
}

arrays/smallestCommonNumber.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Given 3 sorted arrays, find the smallest number among the three that is common to all
3+
else return -1
4+
*/
5+
6+
package main()
7+
8+
import ("fmt"
9+
"AlgorithmsGolang/arrays/binarySearch"
10+
)
11+
12+
func smallestCommonNumber() {
13+
14+
}
15+
16+
17+
func main() {
18+
19+
}

0 commit comments

Comments
 (0)