PasteRack.org
Paste # 80529
2021-06-05 11:17:20

Fork as a new paste.

Paste viewed 821 times.


Embed:

Median of elements in two sorted vectors

  1. #lang racket
  2.  
  3. (define len vector-length)
  4. (define ref vector-ref)
  5.  
  6. ; (kth k a b)
  7. ;   Return the k'th largest value of the values in the two sorted vectors a and b.
  8. (define (kth k a b)
  9.   ; We have a loop in which we look for the kth element in the two subvectors
  10.   ; of a and b given by the indices ai to aj and ai to bj.
  11.   ; More preceisely:
  12.   ; the indices ai and aj are the indices of the beginning and end (exclusive) of a subvector of a.
  13.   ;     If d=1,  then the indices are ai, ai+1, ..., aj-1.
  14.   ;     If d=-1, then the indices are aj+1, aj+1, ..., ai.
  15.   ; That is, if d= 1 we are looking for the kth smallest element,
  16.   ; and      if d=-1 we are looking for the kth greatest element.
  17.  
  18.   (let loop ([ai 0] [aj (len a)] ; search all of a
  19.              [bi 0] [bj (len b)] ; search all of b
  20.              [d 1] [k k])        ; look for kth smallest element
  21.     ; Let's give names to the following useful expressions.
  22.     (define m        (* d (- aj ai)))       ; length of the a subvector
  23.     (define n        (* d (- bj bi)))       ; length of the b subvector
  24.     (define a-empty? (= m 0))               ; is a empty?
  25.     (define b-empty? (= n 0))               ; is b empty?
  26.     (define ak       (quotient (+ m 1) 2))  ; index of middle element
  27.     (define bk       (quotient (+ n 1) 2))  ; index of middle element
  28.     ; The ith element from left or right according to d.
  29.     (define (aref i) (ref a (+ ai (* d i))))
  30.     (define (bref i) (ref b (+ bi (* d i))))
  31.  
  32.     (cond
  33.       [a-empty?  (bref k)]     ; a is empty, so the kth element is in b
  34.       [b-empty?  (aref k)]     ; b is empty, so the kth element is in a
  35.       [(= k 0)   (if (= d 1)   ; we have found our element!
  36.                      (min (aref k) (bref k))
  37.                      (max (aref k) (bref k)))]
  38.       [(and (< k (/ (+ m n) 2)) (> d 0))
  39.        ; This means the sought after element is in the "right" half.
  40.        ; So we flip the direction of both a and b.
  41.        (loop (- aj d) (- ai d)
  42.              (- bj d) (- bi d)
  43.              (- d)
  44.              (- (+ m n) (+ k 1)))]
  45.       ; Now we are sure that the sought after element is in the "left" half.
  46.       ; We compare the middle elements of a and b
  47.       [(< (* d (aref (- ak 1)))
  48.           (* d (bref (- bk 1))))
  49.        ; The middle element of a is smaller/larger than the middle element of b,
  50.        ; so we can rule out the ka elements in the first half of a.
  51.        (loop (+ ai (* d ak)) aj
  52.              bi              bj
  53.              d  (- k ak))]
  54.       [else
  55.        ; otherwise we can rule out the kb elements in the first half of b.
  56.        (loop ai              aj
  57.              (+ bi (* d bk)) bj
  58.              d  (- k bk))])))
  59.  
  60.  
  61. (define (median a b)
  62.   (define n   (+ (len a) (len b)))
  63.   (define n/2 (quotient n 2))
  64.   (if (odd? n)
  65.       (kth n/2 a b)
  66.       (* 1/2 (+ (kth (- n/2 1) a b)
  67.                 (kth    n/2    a b)))))
  68.  
  69.  
  70.  
  71. (= (kth 0 #(1) #())  1)
  72. (= (kth 0 #()  #(2)) 2)
  73.  
  74. (= (kth 0 #(1) #(2)) 1)
  75. (= (kth 1 #(1) #(2)) 2)
  76.  
  77. (= (kth 0 #(1 2) #(3)) 1)
  78. (= (kth 1 #(1 2) #(3)) 2)
  79. (= (kth 2 #(1 2) #(3)) 3)
  80.  
  81. (= (kth 0 #(1) #(2 3)) 1)
  82. (= (kth 1 #(1) #(2 3)) 2)
  83. (= (kth 2 #(1) #(2 3)) 3)
  84.  
  85. (= (kth 0 #(1 2) #(3 4)) 1)
  86. (= (kth 1 #(1 2) #(3 4)) 2)
  87. (= (kth 2 #(1 2) #(3 4)) 3)
  88. (= (kth 3 #(1 2) #(3 4)) 4)
  89.  
  90. (= (kth 0 #(1) #(2 3 4)) 1)
  91. (= (kth 1 #(1) #(2 3 4)) 2)
  92. (= (kth 2 #(1) #(2 3 4)) 3)
  93. (= (kth 3 #(1) #(2 3 4)) 4)
  94.  
  95. (= (kth 0 #() #(1 2 3 4)) 1)
  96. (= (kth 1 #() #(1 2 3 4)) 2)
  97. (= (kth 2 #() #(1 2 3 4)) 3)
  98. (= (kth 3 #() #(1 2 3 4)) 4)
  99.  
  100. (= (kth 0 #(1 2) #(3 4 5)) 1)
  101. (= (kth 1 #(1 2) #(3 4 5)) 2)
  102. (= (kth 2 #(1 2) #(3 4 5)) 3)
  103. (= (kth 3 #(1 2) #(3 4 5)) 4)
  104. (= (kth 4 #(1 2) #(3 4 5)) 5)
  105.  
  106. (= (kth 0 #(1 2 3) #(4 5)) 1)
  107. (= (kth 1 #(1 2 3) #(4 5)) 2)
  108. (= (kth 2 #(1 2 3) #(4 5)) 3)
  109. (= (kth 3 #(1 2 3) #(4 5)) 4)
  110. (= (kth 4 #(1 2 3) #(4 5)) 5)
  111.  
  112. (= (median #(1 2 3) #(4 5))   3)
  113. (= (median #(1 2)   #(3 4 5)) 3)
  114. (= (median #(1 2 3) #(4 5 6)) 3.5)

=>

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t

#t