-
Notifications
You must be signed in to change notification settings - Fork 0
/
median-of-two-sorted-arrays.go
148 lines (130 loc) · 3.7 KB
/
median-of-two-sorted-arrays.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"math/rand"
"fmt"
)
func findMedianSortedArraysSimple(array1 []int, array2 []int) {
array := append(array1, array2...)
fmt.Printf("Concatenated array: %v \n", array)
array = quickSort(array)
fmt.Printf("Sorted concatenated array: %v \n", array)
medianIndex := 0
median := 0.0
if (len(array) % 2 == 0) {
medianIndex = len(array) / 2
median = (float64(array[medianIndex - 1]) + float64(array[medianIndex])) / 2
} else {
medianIndex = len(array) / 2
median = float64(array[medianIndex])
}
fmt.Printf("Median [%d] %.2f \n", medianIndex, median)
}
func quickSort(a []int) []int {
if len(a) < 2 {
return a
}
left:= 0
right := len(a) - 1
// Pick a pivot
pivotIndex := rand.Int() % len(a)
// Move the pivot to the right
a[pivotIndex], a[right] = a[right], a[pivotIndex]
// Pile elements smaller than the pivot on the left
for i := range a {
if a[i] < a[right] {
//swap
a[i], a[left] = a[left], a[i]
left++
}
}
// Place the pivot after the last smaller element
a[left], a[right] = a[right], a[left]
// Go down the rabbit hole
quickSort(a[:left])
quickSort(a[left + 1:])
return a
}
func testMedianSortedArraysSimple() {
fmt.Println()
fmt.Printf("Median Sorted Arrays Simple")
findMedianSortedArraysSimple([]int{1,3}, []int{2})
fmt.Println()
findMedianSortedArraysSimple([]int{3,4}, []int{1,2})
}
//Complexity: O(log(min(m,n))).
//Task from: https://leetcode.com/problems/median-of-two-sorted-arrays/
//Solution from: https://medium.com/@hazemu/finding-the-median-of-2-sorted-arrays-in-logarithmic-time-1d3f2ecbeb46
func findMedianSortedArraysFast(a []int, b []int) float32 {
if(a == nil || b == nil) {
fmt.Printf("Invalid arrays. A: %v; B: %v \n", a, b)
return 0.0
}
fmt.Printf("Input arrays. A: %v; B: %v \n", a, b)
lenghtA := len(a)
lenghtB := len(b)
leftHalfLenght := (lenghtA + lenghtB + 1) / 2
fmt.Printf("Left half lenght: %d \n", leftHalfLenght)
//Make sure A is shorted
if(lenghtA > lenghtB) {
//Swap lenght and array
a, b = b, a
lenghtA, lenghtB = lenghtB, lenghtA
}
fmt.Printf("Lenght of arrays after swap: A[%d]; B[%d] \n",lenghtA, lenghtB)
aMinCount := 0
aMaxCount := lenghtA
for aMinCount <= aMaxCount {
aCount := aMinCount + (aMaxCount - aMinCount) / 2
bCount := leftHalfLenght - aCount
if(aCount > 0 && a[aCount - 1] > b[bCount]) {
aMaxCount = aCount - 1
} else if(aCount < lenghtA && b[bCount - 1] > a[aCount]) {
aMinCount = aCount + 1
} else {
leftSideEnd := func() int {
if(aCount == 0) {
return b[bCount - 1]
} else if(bCount == 0) {
return a[aCount - 1]
}
if(a[aCount - 1] > b[bCount - 1]) {
return a[aCount - 1]
}
//else
return b[bCount - 1]
}()
//If number is odd - we else found element found
if(isOdd(lenghtA + lenghtB)) {
fmt.Printf("Is Odd result: %d \n", leftSideEnd)
return float32(leftSideEnd)
}
rightSideStart := func() int {
if(aCount == lenghtA) {
return b[bCount]
} else if(bCount == lenghtB) {
return a[aCount]
}
if(a[aCount] < b[bCount]) {
return a[aCount]
}
return b[bCount]
}()
//for NOT odd number we need to find a middle manually
result := (float32(leftSideEnd) + float32(rightSideStart)) / 2
fmt.Printf("Is not Odd result: %.2f \n", result)
return result
}
}
panic("This code should not be reached. Method should finish in 'else' ")
}
//The least significant bit of any odd number is 1
func isOdd(number int) bool {
return number & 1 == 1
}
func testMedianSortedArraysFast() {
fmt.Println()
fmt.Printf("Median Sorted Arrays Fast \n")
findMedianSortedArraysFast([]int{1,3}, []int{2})
fmt.Println()
findMedianSortedArraysFast([]int{3,4}, []int{1,2})
}