1, Problem
There are two ordered list, the list A and the List B, such:
A => [1, 14, 20, 36]
B => [,]
The result that I want to get is:
Result => [(), (7, null), (), (25, null), (32, null), ()]
Put it into word: Find the closest number (which delta between counterpart is less than 5) in list A for each item in list B.
First of all, I decide to divide the problem:
1. Find closest number for each item in a, since a is less elements than B.
2, compose a result into a list. If no proper value be found, put it to null.
Because A & B are ordered list, so binary search is fast way to find out corresponding closest value. the complexity of binary search for a ordered list is O (log (n )). each of item in list A have O (N), so the total complexity is O (n * log (n )).
2, code
How to use the code:
Static void main (string [] ARGs) {int [] arrtime = {1, 3, 5, 7, 8, 9, 10, 12, 15, 18, 25 }; int [] arralign = {3, 6, 11, 15, 22}; findclosestnumber finder = new findclosestnumber (3); var lst = finder. findclosestalignment (arrtime, arralign); foreach (VAR item in lst) {console. writeline ("{0} \ t {1}", item. time, item. alignment);} console. readkey ();}
There is the code:
Public class pair {public pair (Int? Time, Int? Alignment) {time = time; Alignment = alignment;} public Int? Time {set; get;} public Int? Alignment {set; get ;}} public class findclosestnumber {public int threshold {Get; Set ;} public findclosestnumber () {This. threshold = 50;} public findclosestnumber (INT threshold) {This. threshold = threshold;} public list <pair> findclosestalignment (INT [] arrtime, int [] arralignment) {list <pair> List = findclosesttime (arrtime, arralignment ); list <pair> reslut = new list <pair> (); For (INT I = 0; I <arrtime. length; I ++) {pair = new pair (arrtime [I], null); foreach (VAR P in list) {If (pair. time = P. time) pair. alignment = P. alignment;} reslut. add (pair) ;}return reslut;} private list <pair> findclosesttime (INT [] arrtime, int [] arralignment) {list <pair> List = new list <pair> (); For (INT I = 0; I <arralignment. length; I ++) {int El = arralignment [I]; int Index = findclosestind Exinorderlist (El, arrtime, this. threshold); If (Index =-1) {list. add (new pair (null, El);} else {list. add (new pair (arrtime [Index], El) ;}return list;} private int findclosestindexinorderlist (INT Val, int [] arr, int threshold) {int Index = array. binarysearch (ARR, Val); If (index> = 0) {// found Return Index;} else {// bitwise complement for index int compl = (~ Index); If (compl = arr. length) {// Val is biggest value in the ARR if (math. ABS (ARR [arr. length-1]-Val)> threshold) Return-1; else return (ARR. length-1);} else if (compl = 0) {// Val is smallest value in the ARR if (math. ABS (ARR [0]-Val)> threshold) Return-1; else return 0;} else {int preindex = compl-1; int deltatoprevious = math. ABS (ARR [preindex]-Val); int deltatocurrent = math. ABS (ARR [compl]-Val); If (deltatoprevious> threshold) Return-1; if (deltatocurrent> threshold) Return-1; return deltatoprevious <= deltatocurrent? Preindex: compl ;}}}}