您的位置:首页 > 其它

最大N算法--最优资源实现

2010-10-24 11:46 393 查看
假如做个XX网, 当然要为金牌会员提供最优秀的资源(people)服务。

优秀资源比较规则是:

1. 有籍贯(籍贯莫有的, 不敢要)

2. 钱多

3. 籍贯为'jx'的优先, 其他...

下面程序就是求最优资源实现:

package algorithm.max;

import java.lang.reflect.Array;

import java.util.Arrays;

import java.util.Comparator;

import java.util.PriorityQueue;

/**

* max n algorithm

*

* @author yangwm Oct 23, 2010 11:36:54 PM

*/

public class MaxNAlgorithm<E> {

/**

* max + n + comparator

*

* @param <E>

* @param input

* @param n

* @param comparator

* @return

*/

public static <E> E[] max(E[] input, int n, Comparator<? super E> comparator) {

PriorityQueue<E> priorityQueue = new PriorityQueue<E>(n, comparator);

for (int i = 0; i < input.length; i++) {

priorityQueue.add(input[i]);

if (i >= n) {

priorityQueue.poll();

}

}

E[] result = newGenericArray(input[0], priorityQueue.size());

for (int i = result.length - 1; i >= 0; i--) {

result[i] = priorityQueue.poll();

}

return result;

}

/**

* max + n

*

* @param <E>

* @param input

* @param n

* @return

*/

public static <E> E[] max(E[] input, int n) {

PriorityQueue<E> priorityQueue = new PriorityQueue<E>(n);

for (int i = 0; i < input.length; i++) {

priorityQueue.add(input[i]);

if (i >= n) {

priorityQueue.poll();

}

}

E[] result = newGenericArray(input[0], priorityQueue.size());

for (int i = result.length - 1; i >= 0; i--) {

result[i] = priorityQueue.poll();

}

return result;

}

@SuppressWarnings("unchecked")

static <E> E[] newGenericArray(E e, int size) {

Class<E> type = (Class<E>) e.getClass();

return (E[]) Array.newInstance(type, size);

}

/**

* @param args

*/

public static void main(String[] args) {

Integer[] input = new Integer[] { 2, 7, 6, 8, 9, 18, 11, 5, 3, 20 };

int n = 3;

System.out.println("------MaxNAlgorithm Array max n-------");

Integer[] result = max(input, n);

System.out.println(Arrays.toString(result));

}

}

/*

------MaxNAlgorithm Array max n-------

[20, 18, 11]

*/

/**

*

*/

package algorithm.max;

import java.lang.reflect.Array;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

import java.util.Random;

/**

* max n algorithm beanch2

*

* @author yangwm Oct 23, 2010 11:58:58 PM

*/

public class MaxNAlgorithmBeanch2 {

/**

* @param args

*/

public static void main(String[] args) {

/*

*

*/

int inputTotal = 1000000;

int n = 100;

/*

* test data

*/

List<People> inputList = new ArrayList<People>(inputTotal);

for (int i = 0; i < inputTotal; i++) {

People people = null;

int money = i / 10;

int factor = new Random().nextInt(5) + 1;

if (factor == 1) {

people = new People(i, "jx", money);

} else if (factor == 2) {

people = new People(i, "bj", money);

} else if (factor == 3) {

people = new People(i, "sh", money);

} else {

people = new People(i, null, money);

}

inputList.add(people);

}

Collections.shuffle(inputList);

System.out.println("test begin inputTotal: " + inputTotal + ", n: " + n);

System.out.println("test data inputList.size(): " + inputList.size());

//-----------------------------------------------------------------

People[] inputArray = new People[inputList.size()];

for (int i = 0; i < inputArray.length; i++) {

inputArray[i] = inputList.get(i);

}

System.out.println("-------JDK Arrays.sort max n with Comparable-------");

/*

* max and test print

*/

long begin = System.currentTimeMillis();

Arrays.sort(inputArray);

People[] resultArray = (People[]) Array.newInstance(People.class, n);

for (int i = inputArray.length - 1, resultIdx = 0; i >= inputArray.length - n; i--) {

resultArray[resultIdx++] = inputArray[i];

}

long end = System.currentTimeMillis();

System.out.println("cosume: " + (end - begin) + " ms");

System.out.println("resultArray.length: " + resultArray.length);

System.out.println("resultArray of first ten : " + Arrays.toString(Arrays.copyOf(resultArray, 10)));

//-----------------------------------------------------------------

inputArray = new People[inputList.size()];

for (int i = 0; i < inputArray.length; i++) {

inputArray[i] = inputList.get(i);

}

System.out.println("-------MaxNAlgorithm Array max n with Comparable-------");

/*

* max and test print

*/

begin = System.currentTimeMillis();

resultArray = MaxNAlgorithm.<People>max(inputArray, n);

end = System.currentTimeMillis();

System.out.println("cosume: " + (end - begin) + " ms");

System.out.println("resultArray.length: " + resultArray.length);

System.out.println("resultArray of first ten : " + Arrays.toString(Arrays.copyOf(resultArray, 10)));

}

/**

* test object for max n algorithm beanch

*

* @author yangwm Oct 24, 2010 4:16:58 PM

*/

static class People implements Comparable<People> {

public int id;

public String province;

public int money;

public People(int id, String province, int money) {

this.id = id;

this.province = province;

this.money = money;

}

@Override

public int compareTo(People o) {

if (province != null && o.province == null) {

return 1;

} else if (province == null && o.province != null) {

return -1;

} else {

if (money > o.money) {

return 1;

} else if (money < o.money) {

return -1;

} else {

if (province == null && o.province == null) {

return 0;

} if (province.equalsIgnoreCase("jx")) {

return 1;

} else if (o.province.equalsIgnoreCase("jx")) {

return -1;

} else {

return province.compareToIgnoreCase(o.province);

}

}

}

}

@Override

public String toString() {

StringBuilder sb = new StringBuilder();

sb.append("id=");

sb.append(id);

sb.append(" province=");

sb.append(province);

sb.append(" money=");

sb.append(money);

return sb.toString();

}

}

}

/*

test begin inputTotal: 1000000, n: 100

test data inputList.size(): 1000000

-------JDK Arrays.sort max n with Comparable-------

cosume: 1218 ms

resultArray.length: 100

resultArray of first ten : [id=999997 province=jx money=99999, id=999993 province=jx money=99999, id=999992 province=sh money=99999, id=999996 province=sh money=99999, id=999991 province=bj money=99999, id=999990 province=bj money=99999, id=999995 province=bj money=99999, id=999989 province=jx money=99998, id=999980 province=bj money=99998, id=999987 province=bj money=99998]

-------MaxNAlgorithm Array max n with Comparable-------

cosume: 578 ms

resultArray.length: 100

resultArray of first ten : [id=999997 province=jx money=99999, id=999993 province=jx money=99999, id=999996 province=sh money=99999, id=999992 province=sh money=99999, id=999990 province=bj money=99999, id=999991 province=bj money=99999, id=999995 province=bj money=99999, id=999989 province=jx money=99998, id=999985 province=bj money=99998, id=999980 province=bj money=99998]

*/

/**

*

*/

package algorithm.max;

import java.lang.reflect.Array;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.Random;

/**

* max n algorithm beanch3

*

* @author yangwm Oct 24, 2010 4:16:12 PM

*/

public class MaxNAlgorithmBeanch3 {

/**

* @param args

*/

public static void main(String[] args) {

/*

*

*/

int inputTotal = 1000000;

int n = 100;

/*

* test data

*/

List<People> inputList = new ArrayList<People>(inputTotal);

for (int i = 0; i < inputTotal; i++) {

People people = null;

int money = i / 10;

int factor = new Random().nextInt(5) + 1;

if (factor == 1) {

people = new People(i, "jx", money);

} else if (factor == 2) {

people = new People(i, "bj", money);

} else if (factor == 3) {

people = new People(i, "sh", money);

} else {

people = new People(i, null, money);

}

inputList.add(people);

}

Collections.shuffle(inputList);

System.out.println("test begin inputTotal: " + inputTotal + ", n: " + n);

System.out.println("test data inputList.size(): " + inputList.size());

//-----------------------------------------------------------------

People[] inputArray = new People[inputList.size()];

for (int i = 0; i < inputArray.length; i++) {

inputArray[i] = inputList.get(i);

}

System.out.println("-------JDK Arrays.sort max n with Comparable-------");

/*

* max and test print

*/

long begin = System.currentTimeMillis();

Arrays.sort(inputArray, new PeopleComparator());

People[] resultArray = (People[]) Array.newInstance(People.class, n);

for (int i = inputArray.length - 1, resultIdx = 0; i >= inputArray.length - n; i--) {

resultArray[resultIdx++] = inputArray[i];

}

long end = System.currentTimeMillis();

System.out.println("cosume: " + (end - begin) + " ms");

System.out.println("resultArray.length: " + resultArray.length);

System.out.println("resultArray of first ten : " + Arrays.toString(Arrays.copyOf(resultArray, 10)));

//-----------------------------------------------------------------

inputArray = new People[inputList.size()];

for (int i = 0; i < inputArray.length; i++) {

inputArray[i] = inputList.get(i);

}

System.out.println("-------MaxNAlgorithm Array max n with Comparable-------");

/*

* max and test print

*/

begin = System.currentTimeMillis();

resultArray = MaxNAlgorithm.<People>max(inputArray, n, new PeopleComparator());

end = System.currentTimeMillis();

System.out.println("cosume: " + (end - begin) + " ms");

System.out.println("resultArray.length: " + resultArray.length);

System.out.println("resultArray of first ten : " + Arrays.toString(Arrays.copyOf(resultArray, 10)));

}

/**

* test object for max n algorithm beanch

*

* @author yangwm Oct 24, 2010 4:16:58 PM

*/

static class People {

public int id;

public String province;

public int money;

public People(int id, String province, int money) {

this.id = id;

this.province = province;

this.money = money;

}

@Override

public String toString() {

StringBuilder sb = new StringBuilder();

sb.append("id=");

sb.append(id);

sb.append(" province=");

sb.append(province);

sb.append(" money=");

sb.append(money);

return sb.toString();

}

}

static class PeopleComparator implements Comparator<People> {

@Override

public int compare(People o1, People o2) {

if (o1 != null && o2.province == null) {

return 1;

} else if (o1.province == null && o2.province != null) {

return -1;

} else {

if (o1.money > o2.money) {

return 1;

} else if (o1.money < o2.money) {

return -1;

} else {

if (o1.province == null && o2.province == null) {

return 0;

} else if (o1.province.equalsIgnoreCase("jx")) {

return 1;

} else if (o2.province.equalsIgnoreCase("jx")) {

return -1;

} else {

return o1.province.compareToIgnoreCase(o2.province);

}

}

}

}

}

}

/*

test begin inputTotal: 1000000, n: 100

test data inputList.size(): 1000000

-------JDK Arrays.sort max n with Comparable-------

cosume: 1078 ms

resultArray.length: 100

resultArray of first ten : [id=999990 province=jx money=99999, id=999993 province=jx money=99999, id=999991 province=sh money=99999, id=999999 province=sh money=99999, id=999982 province=jx money=99998, id=999984 province=jx money=99998, id=999981 province=sh money=99998, id=999989 province=sh money=99998, id=999988 province=bj money=99998, id=999983 province=bj money=99998]

-------MaxNAlgorithm Array max n with Comparable-------

cosume: 547 ms

resultArray.length: 100

resultArray of first ten : [id=999990 province=jx money=99999, id=999993 province=jx money=99999, id=999991 province=sh money=99999, id=999999 province=sh money=99999, id=999984 province=jx money=99998, id=999982 province=jx money=99998, id=999981 province=sh money=99998, id=999989 province=sh money=99998, id=999980 province=bj money=99998, id=999988 province=bj money=99998]

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: