-
Notifications
You must be signed in to change notification settings - Fork 12
/
sorted_set.rb
100 lines (79 loc) · 1.97 KB
/
sorted_set.rb
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
class BasicArraySortedSet
# Create your internal data structure here. It should be an empty array
def initialize
end
def name
"Basic Array"
end
# Use the array's native insert method
def insert(element)
end
# Use the array's native include method
def include?(element)
end
# Use the array's native sort method
def get_sorted_array
end
end
# Similar to above, use the hash's internal methods to implement
# the sorted set's methods
class HashSortedSet
def initialize
end
def name
"Basic Hash"
end
# Insert the key value pair (element, true)
def insert(element)
end
def include?(element)
end
def get_sorted_array
end
end
# Internally use an array to represent the set
# Maintain alphabetical order within the array, so that you can return
# it when asking for sorted array
class ArraySortedSet
def initialize
end
def name
"Sorted Array"
end
# Insert the element at the proper index to maintain the sort order
def insert(element)
end
def include?(element)
end
# You should be able to simply return the original array
def get_sorted_array
end
end
class BinaryArraySortedSet
def initialize
end
def name
"Bsearch Array"
end
# Insert the element and maintain sorted order
def insert(element)
end
# Search for the element using binary search
def include?(element)
end
# Return the original array (it should be sorted)
def get_sorted_array
end
# A little helper method to help you implement binary search
# This method should run as follows
# If the subset has 0 or 1 element, do a simple search
# If the subset has more than 1 element...
# Find the midpoint of the range
# Compare if the middle element is higher or lower than your element
# If the element is higher
# Do a binary_search on the lower half
# If the element is lower
# Do a binary search on the upper half
def binary_search(from_index, to_index, element)
end
end