Skip to content

Commit 518985d

Browse files
author
Aman Kumar
committed
searching, stack
1 parent abaf727 commit 518985d

14 files changed

+639
-2
lines changed

practice/quick_sort.rb

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
def quick_sort(arr)
2+
quick_sort_helper2(arr, 0, arr.length - 1)
3+
arr
4+
end
5+
6+
def quick_sort_helper(arr, low, high)
7+
return if low >= high
8+
9+
pivot = arr[high]
10+
pi = partition(arr, pivot, low, high)
11+
quick_sort_helper(arr, low, pi - 1)
12+
quick_sort_helper(arr, pi + 1, high)
13+
end
14+
15+
def partition(arr, pivot, low, high)
16+
i = low
17+
j = low
18+
while i <= high
19+
if arr[i] <= pivot
20+
arr[i], arr[j] = arr[j], arr[i]
21+
j += 1
22+
end
23+
i += 1
24+
end
25+
j - 1
26+
end
27+
28+
def quick_sort_helper2(arr, low, high)
29+
return if low >= high
30+
31+
pivot = low
32+
left = low + 1
33+
right = high
34+
while right >= left
35+
if arr[left] > arr[pivot] && arr[right] < arr[pivot]
36+
swap(arr, left, right)
37+
elsif arr[left] <= arr[pivot]
38+
left += 1
39+
elsif arr[right] >= arr[pivot]
40+
right -= 1
41+
end
42+
end
43+
swap(arr, right, pivot)
44+
45+
is_left_subarray_smaller = high - right + 1 > left - low - 1
46+
# we are sorting smaller subarray first
47+
if is_left_subarray_smaller
48+
quick_sort_helper2(arr, left, right - 1)
49+
quick_sort_helper2(arr, right + 1, high)
50+
else
51+
quick_sort_helper2(arr, right + 1, high)
52+
quick_sort_helper2(arr, left, right - 1)
53+
end
54+
end
55+
56+
def swap(arr, i, j)
57+
arr[i], arr[j] = arr[j], arr[i]
58+
end
59+
60+
puts quick_sort([9, 30, 2, 88, 3, 98, 45]).to_s

searching/binary_search.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# https://leetcode.com/problems/binary-search/
2+
#
13
# Recursive solution
24
def b_search(ar, target, l, r)
35
# l, r = 0, ar.length-1
@@ -23,6 +25,7 @@ def binary_search(arr, target)
2325
left = mid + 1
2426
end
2527
end
28+
-1
2629
end
2730

2831
ar = [1, 3]

searching/index_equals_value.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
# using brutforce
3+
def index_equals_value_bforce(nums)
4+
nums.each_with_index do |n, index|
5+
return index if n == index
6+
end
7+
-1
8+
end
9+
10+
def index_equals_value_bsearch(nums)
11+
left = 0
12+
right = nums.length - 1
13+
while left <= right
14+
mid = (left + right) / 2
15+
# 1st occurance at zero index
16+
if nums[mid] == mid && mid.zero?
17+
return mid
18+
elsif nums[mid] == mid && nums[mid-1] < mid - 1
19+
return mid
20+
elsif nums[mid] < mid
21+
left = mid + 1
22+
else
23+
right = mid - 1
24+
end
25+
end
26+
-1
27+
end
28+
29+
# 3
30+
puts index_equals_value_bsearch([-5, -3, 0, 3, 4, 5, 9])
31+
32+
#4
33+
puts index_equals_value_bsearch([-5, -3, 0, 2, 4, 5, 9])
34+
35+
#-1
36+
puts index_equals_value_bsearch([-5, -3, 0, 2, 5, 6, 19])

searching/quick_select.rb

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Write a function that takes in an array of distinct integers as well as an integer k and that returns the kth smallest integer in that array.
2+
3+
# The function should do this in linear time, on average.
4+
5+
# Sample Input
6+
# array = [8, 5, 2, 9, 7, 6, 3]
7+
# k = 3
8+
# Sample Output
9+
# 5
10+
#
11+
12+
def quick_select(nums, k)
13+
pos = k - 1
14+
return if nums.empty? || nums.length < pos
15+
16+
quick_select_helper(nums, pos, 0, nums.length - 1)
17+
end
18+
19+
def quick_select_helper(nums, pos, start, ending)
20+
while start <= ending
21+
pivot = start
22+
left = start + 1
23+
right = ending
24+
25+
while right >= left
26+
if nums[left] > nums[pivot] && nums[right] < nums[pivot]
27+
swap(nums, left, right)
28+
elsif nums[left] <= nums[pivot]
29+
left += 1
30+
elsif nums[right] >= nums[pivot]
31+
right -= 1
32+
end
33+
end
34+
35+
swap(nums, pivot, right)
36+
if pos == right
37+
return nums[right]
38+
elsif right < pos
39+
start = right + 1
40+
else
41+
ending = right - 1
42+
end
43+
end
44+
end
45+
46+
def swap(ar, i, j)
47+
ar[i], ar[j] = ar[j], ar[i]
48+
end
49+
50+
# 5
51+
puts quick_select([8, 5, 2, 9, 7, 6, 3], 3)
52+
53+
# 1
54+
puts quick_select([1], 1)
55+
56+
# 24
57+
puts quick_select([43, 24, 37], 1)
58+
59+
# 2
60+
puts quick_select([8, 3, 2, 5, 1, 7, 4, 6], 2)

searching/range_search_iterative.rb

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
2+
# Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
3+
# If target is not found in the array, return [-1, -1].
4+
# You must write an algorithm with O(log n) runtime complexity.
5+
# Example 1:
6+
# Input: nums = [5,7,7,8,8,10], target = 8
7+
# Output: [3,4]
8+
9+
def search_range(nums, target)
10+
output_range = []
11+
# try to go left
12+
output_range << binary_search_helper(nums, target, true)
13+
14+
# go right
15+
output_range << binary_search_helper(nums, target, false)
16+
output_range
17+
end
18+
19+
def binary_search_helper(nums, target, go_left)
20+
left = 0
21+
right = nums.length - 1
22+
while left <= right
23+
mid = (left + right) / 2
24+
if target > nums[mid]
25+
left = mid + 1
26+
elsif target < nums[mid]
27+
right = mid - 1
28+
elsif go_left
29+
if mid.zero? || nums[mid - 1] != target
30+
return mid
31+
else
32+
right = mid - 1
33+
end
34+
elsif mid == nums.length - 1 || nums[mid + 1] != target
35+
return mid
36+
else
37+
left = mid + 1
38+
end
39+
end
40+
-1
41+
end
42+
43+
# [-1, -1]
44+
puts search_range([5, 7, 7, 8, 8, 10], 6).to_s
45+
46+
puts search_range([0, 1, 21, 33, 45, 45, 45, 45, 45, 45, 61, 71, 73], 45).to_s
47+
48+
# [3,4]
49+
puts search_range([5, 7, 7, 8, 8, 10], 8).to_s

searching/range_search_recursive.rb

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
2+
# Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
3+
# If target is not found in the array, return [-1, -1].
4+
# You must write an algorithm with O(log n) runtime complexity.
5+
# Example 1:
6+
# Input: nums = [5,7,7,8,8,10], target = 8
7+
# Output: [3,4]
8+
#
9+
10+
# recursive solution
11+
def search_range(nums, target)
12+
output_range = [-1, -1]
13+
# try to go left
14+
binary_search_helper(nums, target, 0, nums.length - 1, output_range, true)
15+
16+
# go right
17+
binary_search_helper(nums, target, 0, nums.length - 1, output_range, false)
18+
output_range
19+
end
20+
21+
# recursive solution
22+
def binary_search_helper(nums, target, left, right, output_range, go_left)
23+
return if left > right
24+
25+
mid = (left + right) / 2
26+
if target > nums[mid]
27+
binary_search_helper(nums, target, mid + 1, right, output_range, go_left)
28+
elsif target < nums[mid]
29+
binary_search_helper(nums, target, left, mid - 1, output_range, go_left)
30+
elsif go_left
31+
# if value found at mid, we will try to go left or right
32+
if mid.zero? || nums[mid - 1] != target
33+
output_range[0] = mid
34+
else
35+
binary_search_helper(nums, target, left, mid - 1, output_range, go_left)
36+
end
37+
# if we are at 1st value or previous value is not same as target
38+
# means we have found the start of the range value
39+
elsif mid == nums.length - 1 || target != nums[mid + 1]
40+
# we have reached at end of array or the next value is not equal to target
41+
# we have found end of the range
42+
output_range[1] = mid
43+
else
44+
binary_search_helper(nums, target, mid + 1, right, output_range, go_left)
45+
end
46+
end
47+
48+
puts search_range([5, 7, 7, 8, 8, 10], 6).to_s
49+
50+
puts search_range([0, 1, 21, 33, 45, 45, 45, 45, 45, 45, 61, 71, 73], 45).to_s

searching/search_sorted_matrix.rb

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
def search_sorted_matrix(matrix, target); end
1+
def search_sorted_matrix(matrix, target)
2+
row = 0
3+
col = matrix[0].length - 1
4+
5+
while row < matrix.length && col >= 0
6+
if matrix[row][col] > target
7+
col -= 1
8+
elsif matrix[row][col] < target
9+
row += 1
10+
else
11+
return [row, col]
12+
end
13+
end
14+
[-1, -1]
15+
end
216

317
matrix = [
418
[1, 4, 7, 12, 15, 1000],
@@ -9,4 +23,6 @@ def search_sorted_matrix(matrix, target); end
923
]
1024
target = 44
1125

12-
puts search_sorted_matrix(matrix, target)
26+
puts search_sorted_matrix(matrix, target).to_s
27+
puts search_sorted_matrix(matrix, 99).to_s
28+
puts search_sorted_matrix(matrix, 2323).to_s

searching/shifted_binary_search.rb

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# https://leetcode.com/problems/search-in-rotated-sorted-array/submissions/
2+
3+
def shifted_binary_search(arr, target)
4+
shifted_binary_search_helper(arr, target, 0, arr.length - 1)
5+
end
6+
7+
# recursive solution
8+
def shifted_binary_search_helper(arr, target, left, right)
9+
return -1 if left > right
10+
11+
mid = (left + right) / 2
12+
13+
left_num = arr[left]
14+
right_num = arr[right]
15+
mid_num = arr[mid]
16+
if target == mid_num
17+
mid
18+
elsif left_num <= mid_num
19+
if target >= left_num && target < mid_num
20+
shifted_binary_search_helper(arr, target, left, mid - 1)
21+
else
22+
shifted_binary_search_helper(arr, target, mid + 1, right)
23+
end
24+
elsif target > mid_num && target <= right_num
25+
shifted_binary_search_helper(arr, target, mid + 1, right)
26+
else
27+
shifted_binary_search_helper(arr, target, left, mid - 1)
28+
end
29+
end
30+
31+
def shifted_binary_search_iterative(arr, target)
32+
left = 0
33+
right = arr.length - 1
34+
35+
while left <= right
36+
mid = (left + right) / 2
37+
left_num = arr[left]
38+
right_num = arr[right]
39+
mid_num = arr[mid]
40+
if target == mid_num
41+
return mid
42+
elsif left_num <= mid_num
43+
if target >= left_num && target < mid_num
44+
right = mid - 1
45+
else
46+
left = mid + 1
47+
end
48+
elsif target > mid_num && target <= right_num
49+
left = mid + 1
50+
else
51+
right = mid - 1
52+
end
53+
54+
end
55+
-1
56+
end
57+
58+
# 8
59+
puts shifted_binary_search([45, 61, 71, 72, 73, 0, 1, 21, 33, 37], 33)
60+
61+
# 2
62+
puts shifted_binary_search([5, 23, 111, 1], 111)
63+
64+
# -1
65+
puts shifted_binary_search([23, 111, 1, 5], 35)
66+
67+
# 2
68+
puts shifted_binary_search([5, 1, 3], 3)
69+
70+
# 8
71+
puts shifted_binary_search_iterative([45, 61, 71, 72, 73, 0, 1, 21, 33, 37], 33)
72+
73+
# 2
74+
puts shifted_binary_search_iterative([5, 23, 111, 1], 111)
75+
76+
# -1
77+
puts shifted_binary_search_iterative([23, 111, 1, 5], 35)
78+
79+
# 2
80+
puts shifted_binary_search_iterative([5, 1, 3], 3)

0 commit comments

Comments
 (0)