Sunday, October 23, 2011

External MergeSort Sample Code

Today I practiced external merge sort just for fun. Here is the java code, for merge-sorting 5 sorted files, each of which contains a list of sorted integers. The input are out0.txt, out1.txt, out2.txt, out3.txt, out4.txt, output is sorted.txt.

import java.io.FileWriter;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class ExternalMergeSort
{
public static void main(String[] args) throws Exception
{
// initialize n buffers for merge sort
List bufferList = new ArrayList();
for (int i = 0; i < 5; i++)
{
bufferList.add(new MergeSortBuffer("out" + i + ".txt"));
}

// output stream
PrintWriter out = new PrintWriter(new FileWriter("sorted.txt"));

// store merge sort result into output buffer
int[] outputBuf = new int[1000];

// merge until only one mergebuffer left
while (bufferList.size() > 1)
{
int min = -1;
int minPtr = -1;
for (int i = 0; i < bufferList.size(); i++)
{
if (minPtr < 0 || bufferList.get(i).pokeNext() < min)
{
min = bufferList.get(i).pokeNext(); minPtr = i;
}
}

// now we got minPtr buffer, write it out
out.println(bufferList.get(minPtr).getNext());

// remove the maxPtr buffer if it doesn't have elements anymore
if (!bufferList.get(minPtr).hasNext())
{
bufferList.remove(minPtr);
}
}

// output last buffer
MergeSortBuffer buf = bufferList.get(0);
while(buf.hasNext())
{
out.println(buf.getNext());
}

out.close();
}
}

--------------------------------------------------

import java.io.BufferedReader;
import java.io.FileReader;

public class MergeSortBuffer
{
private BufferedReader reader;
private int[] buf = new int[1000];
private int bufSize;
private int start;

public MergeSortBuffer(String fileName) throws Exception
{
reader = new BufferedReader(new FileReader(fileName));
refillBuf();
}

public boolean hasNext() throws Exception
{
return bufSize > 0;
}

public int pokeNext() throws Exception
{
return buf[start];
}

public int getNext() throws Exception
{
int next = buf[start];
start++;
if (start >= bufSize)
{
refillBuf();
}
return next;
}

private void refillBuf() throws Exception
{
int i = -1;
while (true)
{
String line = reader.readLine();
if (line == null || line.equals(""))
{
break;
}
i++; buf[i] = Integer.parseInt(line);
if (i + 1 == buf.length)
{
break;
}
}

start = 0;
bufSize = i + 1;
}
}

Sunday, October 16, 2011

来美国父母老人探亲医疗保险问题

最近根据网上的讨论仔细攒了一篇超强的美国父母探亲保险文章.很多人的父母都有心脏病或者心脏有问题。这种病有时候确实不能耽误。大概的情况就是,即使没有保险也可以去看急诊并且不付或者少付款。 但操作中有些细节的东西, 详情请看USA Visiting Parents Insurance Coverage

Tuesday, October 11, 2011

FindTheBest.com Alternatives Everyone?

I was searching for comparison engine related topics on the web and came across FindTheBest.com. It is a startup that is trying to do this be-all-end-all type of work, to compare any and all services, products etc.

But, when I visited the site, I have to say, it is not very impressive. The site seems a bit cluttered and search loads slowly, also, the search box slides far to the right hand side. It is a surprise that the search result is powered by Google custom search.

I couldn't believe FindTheBest.com secured a lot of VC funding. A one-man shop like JiansNet.com has good search result, and the best part is that it is not powered by some type of custom search.

The bottom line is that, it is always interesting to see this and other startups popping up from time to time, but only to see that they are still perfecting their technology/websites.

Tuesday, March 29, 2011

Skechers Promos and Coupons

I buy and wear skecher shoes. They are the best, light weight and good quality. Other than Skechers, I also have new balance, Nike etc. But to me, fashion-wise, Skecher shoes are more chic/elegant.http://www.blogger.com/img/blank.gif

Just checked our site, we have some pages that are related to Skechers here:
Skechers Promotions and Sales

In the future, if Skecher has any sales, we will post more, just love it...