algorithm@ lower_bound implementation(Binary Search)
一道来自jhu algorithm的作业题:
Given two sorted arrays A, B, give a linear time algorithm that finds two entries i,j such that|A[i]−B[j]|is minimized. Prove the correctness of your algorithm and analyze the running time.
Solution:我们可以对于每个a[i],然后再B数组中二分查找距离a[i] “最近”的数,这里的“最近”是指|A[i]−B[j]|最小。
贴上一个algo代码 和 一个简单test案例。
package abc.com;
import java.util.Collections;
public class TestLowerBound {
public static void prt(Object o) {
System.out.print(o);
}
//return a index where (key <= a[index])
public static int lowerBound(int[] a, int key) {
int l = 0, r = a.length-1, mid;
if(key > a[r]) return -1;
while(l < r) {
mid = (l+r)/2;
if(a[mid] < key) l = mid + 1;
else r = mid;
}
return l;
}
public static int min_abs_dif(int[] a, int val) {
int l = 0, r = a.length-1, mid;
if(val <= a[l]) {
return Math.abs(val - a[l]);
}
else if(val >= a[r]) {
return Math.abs(val - a[r]);
}
else {
int pos = lowerBound(a, val);
//prt(pos);
if(Math.abs(a[pos] - val) == 0) return 0;
int min = Integer.MAX_VALUE;
if(pos-1 >= 0 && Math.abs(a[pos-1] - val) < min) min = Math.abs(a[pos-1] - val);
if(pos >= 0 && (Math.abs(a[pos] - val) < min)) min = Math.abs(a[pos] - val);
return min;
}
}
public static void main(String[] args) {
int[] a = {1, 29, 32, 42, 61, 63, 64, 84, 88, 99};
/*
for(int i=0; i<a.length; ++i) {
a[i] = (int)(Math.random() * 100);
prt(a[i] + " ");
}*/
int min_abs = min_abs_dif(a, 450);
prt("\n" + min_abs);
}
}
algorithm@ lower_bound implementation(Binary Search)的更多相关文章
- [Algorithm] Beating the Binary Search algorithm – Interpolation Search, Galloping Search
From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...
- [Algorithms] Binary Search Algorithm using TypeScript
(binary search trees) which form the basis of modern databases and immutable data structures. Binary ...
- 【437】Binary search algorithm,二分搜索算法
Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...
- js binary search algorithm
js binary search algorithm js 二分查找算法 二分查找, 前置条件 存储在数组中 有序排列 理想条件: 数组是递增排列,数组中的元素互不相同; 重排 & 去重 顺序 ...
- 2 - Binary Search & LogN Algorithm - Apr 18
38. Search a 2D Matrix II https://www.lintcode.com/problem/search-a-2d-matrix-ii/description?_from=l ...
- 2 - Binary Search & LogN Algorithm
254. Drop Eggs https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1 ...
- [Algorithm] Delete a node from Binary Search Tree
The solution for the problem can be divided into three cases: case 1: if the delete node is leaf nod ...
- [Algorithm] Check if a binary tree is binary search tree or not
What is Binary Search Tree (BST) A binary tree in which for each node, value of all the nodes in lef ...
- [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search
Let's say we are going to find out number of occurrences of a number in a sorted array using binary ...
随机推荐
- 搭建 Android 开发环境,初试HelloWorld (win7) (下) (转)
5. 创建AVD 为使Android应用程序可以在模拟器上运行,必须创建AVD. 在Eclipse菜单中,选择 Windows -> Android Virtual Device Manage ...
- Hybrid App 和 React Native 开发那点事
简介:Hybrid App(混合模式移动应用)开发是指介于Web-app.Native-App这两者之间的一种开发模式,兼具「Native App 良好用户交互体验的优势」和「Web App 跨平台开 ...
- poj 2065 SETI 高斯消元
看题就知道要使用高斯消元求解! 代码如下: #include<iostream> #include<algorithm> #include<iomanip> #in ...
- 微软在 .NET 3.5 新增了一个 HashSet 类,在 .NET 4 新增了一个 SortedSet 类,本文介绍他们的特性,并比较他们的异同。
微软在 .NET 3.5 新增了一个 HashSet 类,在 .NET 4 新增了一个 SortedSet 类,本文介绍他们的特性,并比较他们的异同. .NET Collection 函数库的 Has ...
- C#一个简单下载程序实例(可用于更新)
运行时的界面 using System; using System.Collections.Generic; using System.ComponentModel; using System.Dat ...
- 图形学:图像围绕着某个点P(a,b)旋转------白话版
前提:在研究图形时候,我们并没有规定图形的大小,所以任意图形多是支持的,这也另外说明了一点,图形转换和图形的大小没有关系. 如果图像围绕着某个点P(a,b)旋转,则先要将坐标系平移到该点,再进行旋转, ...
- 基于http.sys来开发的,真的是非常稳定
真正的WEB服务器是不会用Indy写的.因为它是基于每连接每线程的. 其实真正的服务器需要下很多功夫,无法快速开发的.比如说,字符串处理.玩服务器基本上就是玩内存.举个例子: var str:Ansi ...
- update多表陷阱
今天同学发了个sql题目 A1表 B1表 id num id snum 1 10 1 90 2 2000 3 4000 3 30 B表的数据插入A表当中 最后的结果 A表 1 90 2 2000 3 ...
- 重载操作符 operator overloading 学习笔记
重载操作符,只是另外一种调用函数的方法和表现方式,在某些情况它可以让代码更简单易读.注意不要过度使用重载操作符,除非它让你的类更简单,让你的代码更易读. 1语法 如下: 其中友元,关键字不是必须的,但 ...
- allegro添加多个过孔
place--Via arrays matrix: [数] 矩阵:模型:[生物][地质] 基质:母体:子宫:[地质] 脉石 boundary:边界:范围:分界线 unplaced : adj. 未受到 ...