Skip to content

Commit 78350b7

Browse files
committed
add tasks
1 parent dc1205e commit 78350b7

File tree

6 files changed

+260
-0
lines changed

6 files changed

+260
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Nuts and bolts
2+
3+
A disorganized carpenter has a mixed pile of `n` nuts and `n` bolts. The goal is to find the corresponding pairs of nuts and bolts. Each nut fits exactly one bolt and each bolt fits exactly one nut. By fitting a nut and a bolt together, the carpenter can see which one is bigger (but the carpenter cannot compare two nuts or two bolts directly). Design an algorithm for the problem that uses at most proportional to `n*log(n)` compares (probabilistically).
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# def merge_sort(array, start: nil, finish: nil, buffer: nil)
2+
# return if start == finish
3+
4+
# mid = (finish - start) / 2 + start
5+
6+
# merge_sort(array, start: start, finish: mid, buffer: buffer)
7+
# merge_sort(array, start: mid + 1, finish: finish, buffer: buffer)
8+
9+
# i = 0
10+
# j = 0
11+
# b = 0
12+
# loop do
13+
# remaining = (j + mid + 1)..finish if i == mid + 1 - start
14+
# remaining = (i + start)..mid if j == finish - mid
15+
# if remaining
16+
# remaining.each do |k|
17+
# buffer[b] = array[k]
18+
# b += 1
19+
# end
20+
# break
21+
# end
22+
# x = array[i + start]
23+
# y = array[j + mid + 1]
24+
# if x < y
25+
# buffer[b] = x
26+
# i += 1
27+
# else
28+
# buffer[b] = y
29+
# j += 1
30+
# end
31+
# b += 1
32+
# end
33+
# (0...b).each do |k|
34+
# array[start + k] = buffer[k]
35+
# end
36+
37+
# array
38+
# end
39+
40+
# n = 10
41+
# a = n.times.map { rand(0..99) }.sort + n.times.map { rand(0..99) }.sort
42+
# b = Array.new(n)
43+
44+
# p a
45+
46+
# k = 0
47+
# i = 0
48+
# j = n
49+
50+
# while k < n do
51+
# if a[i] <= a[j]
52+
# b[k] = a[i]
53+
# a[i] = nil
54+
# i += 1
55+
# else
56+
# b[k] = a[j]
57+
# a[j] = nil
58+
# j += 1
59+
# end
60+
# k += 1
61+
# end
62+
63+
# i = 2 * n - 1
64+
# k = 2 * n - 1
65+
# while k >= n do
66+
# if a[i]
67+
# a[k] = a[i] unless k == i
68+
# k -= 1
69+
# end
70+
# i -= 1
71+
# end
72+
73+
# b.each_with_index do |e, i|
74+
# a[i] = e
75+
# end
76+
77+
# merge_sort(a, start: n, finish: 2*n - 1, buffer: b)
78+
79+
# p b
80+
# p a
81+
# p a.sort
82+
# p a == a.sort
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Selection in two sorted arrays
2+
3+
Given two sorted arrays `a[]` and `b[]`, of lengths `n1` and `n2` and an integer `0 ≤ k < n1 + n2`, design an algorithm to find a key of rank `k`. The order of growth of the worst case running time of your algorithm should be `log(n)`, where `n = n1 + n2`.
4+
5+
- Version 1: `n1 = n2` (equal length arrays) and `k = n / 2` (median)
6+
- Version 2: `k = n / 2` (median)
7+
- Version 3: no restrictions
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# def merge_sort(array, start: nil, finish: nil, buffer: nil)
2+
# return if start == finish
3+
4+
# mid = (finish - start) / 2 + start
5+
6+
# merge_sort(array, start: start, finish: mid, buffer: buffer)
7+
# merge_sort(array, start: mid + 1, finish: finish, buffer: buffer)
8+
9+
# i = 0
10+
# j = 0
11+
# b = 0
12+
# loop do
13+
# remaining = (j + mid + 1)..finish if i == mid + 1 - start
14+
# remaining = (i + start)..mid if j == finish - mid
15+
# if remaining
16+
# remaining.each do |k|
17+
# buffer[b] = array[k]
18+
# b += 1
19+
# end
20+
# break
21+
# end
22+
# x = array[i + start]
23+
# y = array[j + mid + 1]
24+
# if x < y
25+
# buffer[b] = x
26+
# i += 1
27+
# else
28+
# buffer[b] = y
29+
# j += 1
30+
# end
31+
# b += 1
32+
# end
33+
# (0...b).each do |k|
34+
# array[start + k] = buffer[k]
35+
# end
36+
37+
# array
38+
# end
39+
40+
# n = 10
41+
# a = n.times.map { rand(0..99) }.sort + n.times.map { rand(0..99) }.sort
42+
# b = Array.new(n)
43+
44+
# p a
45+
46+
# k = 0
47+
# i = 0
48+
# j = n
49+
50+
# while k < n do
51+
# if a[i] <= a[j]
52+
# b[k] = a[i]
53+
# a[i] = nil
54+
# i += 1
55+
# else
56+
# b[k] = a[j]
57+
# a[j] = nil
58+
# j += 1
59+
# end
60+
# k += 1
61+
# end
62+
63+
# i = 2 * n - 1
64+
# k = 2 * n - 1
65+
# while k >= n do
66+
# if a[i]
67+
# a[k] = a[i] unless k == i
68+
# k -= 1
69+
# end
70+
# i -= 1
71+
# end
72+
73+
# b.each_with_index do |e, i|
74+
# a[i] = e
75+
# end
76+
77+
# merge_sort(a, start: n, finish: 2*n - 1, buffer: b)
78+
79+
# p b
80+
# p a
81+
# p a.sort
82+
# p a == a.sort
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Decimal dominants
2+
3+
Given an array with `n` keys, design an algorithm to find all values that occur more than `n/10` times. The expected running time of your algorithm should be linear.
4+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# def merge_sort(array, start: nil, finish: nil, buffer: nil)
2+
# return if start == finish
3+
4+
# mid = (finish - start) / 2 + start
5+
6+
# merge_sort(array, start: start, finish: mid, buffer: buffer)
7+
# merge_sort(array, start: mid + 1, finish: finish, buffer: buffer)
8+
9+
# i = 0
10+
# j = 0
11+
# b = 0
12+
# loop do
13+
# remaining = (j + mid + 1)..finish if i == mid + 1 - start
14+
# remaining = (i + start)..mid if j == finish - mid
15+
# if remaining
16+
# remaining.each do |k|
17+
# buffer[b] = array[k]
18+
# b += 1
19+
# end
20+
# break
21+
# end
22+
# x = array[i + start]
23+
# y = array[j + mid + 1]
24+
# if x < y
25+
# buffer[b] = x
26+
# i += 1
27+
# else
28+
# buffer[b] = y
29+
# j += 1
30+
# end
31+
# b += 1
32+
# end
33+
# (0...b).each do |k|
34+
# array[start + k] = buffer[k]
35+
# end
36+
37+
# array
38+
# end
39+
40+
# n = 10
41+
# a = n.times.map { rand(0..99) }.sort + n.times.map { rand(0..99) }.sort
42+
# b = Array.new(n)
43+
44+
# p a
45+
46+
# k = 0
47+
# i = 0
48+
# j = n
49+
50+
# while k < n do
51+
# if a[i] <= a[j]
52+
# b[k] = a[i]
53+
# a[i] = nil
54+
# i += 1
55+
# else
56+
# b[k] = a[j]
57+
# a[j] = nil
58+
# j += 1
59+
# end
60+
# k += 1
61+
# end
62+
63+
# i = 2 * n - 1
64+
# k = 2 * n - 1
65+
# while k >= n do
66+
# if a[i]
67+
# a[k] = a[i] unless k == i
68+
# k -= 1
69+
# end
70+
# i -= 1
71+
# end
72+
73+
# b.each_with_index do |e, i|
74+
# a[i] = e
75+
# end
76+
77+
# merge_sort(a, start: n, finish: 2*n - 1, buffer: b)
78+
79+
# p b
80+
# p a
81+
# p a.sort
82+
# p a == a.sort

0 commit comments

Comments
 (0)