Java sort Ô´Âë½âÎö Object part
ÉϴηÖÎöÁËJava¶ÔprimitiveµÄÅÅÐò£¬Ê¹ÓõÄÊÇÓÅ»¯µÄ¿ìËÙÅÅÐòËã·¨¡£Õâ´Î½éÉÜJava¶ÔObject¶ÔÏó(Comparable)µÄÅÅÐò¡£
JavaʹÓÃÁ˹鲢¶ÔObjectÅÅÐò£¬¹ØÓڹ鲢ÅÅÐòµÄϸ½Ú£¬Çë²é¿´http://zh.wikipedia.org/zh/%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F
¹é²¢ÅÅÐòÒ²ÓÐÒ»¸öÔËÐÐ״̬ͼ£¬²»¹ý¿´×Åȷʵͦ·Ñ½âµÄ =¡£=£¡
ÔÚÁ˽âÁ˹鲢ÅÅÐòµÄÔÀíÖ®ºó£¬ÎÒÃÇչʾJavaµÄÔ´Â룬À´ÂýÂý·ÖÎö¡£
public static void sort(Object[] a) {
Object[] aux = (Object[])a.clone();
mergeSort(aux, a, 0, a.length, 0);
}
Ê×ÏÈËü¿Ë¡ÁËÒ»·Ý¿½±´£¬È»ºó°Ñ¿½±´ºÍÔ´Êý×鶼´«ÈëmergeSort·½·¨¡£¾ÍÊǵÚÒ»µÚ¶þ¸ö²ÎÊý¡£
ÁíÍâ3¸ö²ÎÊý·Ö±ðÊÇ£ºlow£¬high£¬off¡£lowºÍhighûɶ¶à˵µÄ£¬¾ÍÊÇÉÏϽ磬²»¹ýoffͦÓÐÒâ˼µÄ¡£Õâ¸ö±äÁ¿Ã»ÓУ¬Ò²²»Ó°Ïì
ÅÅÐòµÄÂß¼¡£ÎªÊ²Ã´»áÓÐÄØ£¿ÏÈÂô¸ö¹Ø×Ó¡£
private static void
mergeSort(Object[] src,
Object[] dest,
int low,
int high,
int off) {
int length = high - low;
// Insertion sort on smallest arrays
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((Comparable) dest[j-1]).
compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}
Õâ¶Î¿´×ŷdz£ÊìϤ£¬¾ÍÊǵ±³¤¶ÈСÓÚINSERTIONSORT_THRESHOLDµÄʱºò£¬ÓòåÈëÅÅÐò¡£Õâ¸öÖµ£¬»¹ÊÇ7.
´ó¼ÒÈç¹û»¹¼ÇµÃÔÚ¿ìËÙÅÅÐòÖУ¬Ö±½Óhard codingÁËÒ»¸ö7¡£¿É¼û sun µÄ³ÌÐòÔ±£¬Ò²ÊÇ»áÓÐʧÎóµÄ£¡´ó¼Ò¶¼ÊÇÈË£¬Öйú³ÌÐòÔ±
Ö»Òª²»¸¡Ô꣬»¹ÊÇÄܹ»ºÜÅ£±ÆµÄ¡£¡£¡£ Óе㳶ԶÁË¡£
ÆäÖÐµÄ swap() ¾Í²»ÔÙÀÛÊöÁË¡£
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) >> 1;
mergeSort(dest, src, low, mid, -off);
mergeSort(dest, src, mid, high, -off);
½ÓÏÂÀ´Ò»¶Î£¬Êǵ±³¤¶È´óÓÚ7ʱ£¬°ÑÊý×é·ÖÁ½¶Ë£¬·Ö±ðµÝ¹éÅÅÐò¡£
ϸÐĵÄÈËÒѾ·¢ÏÖÁË°ÂÃîµÄµØ·½£¬ÔÚÍⲿµ÷ÓÃmergeSortʱ£¬´«ÈëµÄ²ÎÊý˳ÐòÊÇ src, dest¡£¿ÉÊÇËüµÝ¹éʱ£¬ÎªÊ²Ã´´«Èëdest£¬srcÄØ£¿
λÖû¥»»µ½µ×´ú±
Ïà¹ØÎĵµ£º
ÏÈÀ´Á˽âÒ»ÏÂÁ´±íģʽµÄÔÀí£º
Ê×ÏÈдһ¸öJavaBean£¬ÄÚÈÝÊÇÒªÌí¼ÓµÄÔªËغ͸ÃÔªËصĽڵ㡣
public class NodeBean implements Serializable
{
private Object data; //ÔªËر¾Éí
private NodeBean next; //ÏÂÒ»¸ö½Úµã
&n ......
Ó¢ÎÄÔÎĵØÖ·:
http://www.onjava.com/pub/a/onjava/2005/07/06/generics.html
ÖÐÎĵØÖ·:
http://www.matrix.org.cn/resource/article/43/43634_java_generics.html
¹Ø¼ü´Ê£º java generics java5
ÕªÒª
·ºÐÍÊÇJ2SE 5.0×îÖØÒªµÄÌØÐÔ¡£ËûÃÇÈÃÄãдһ¸ötype(Àà»ò½Ó¿Ú£©ºÍ´´½¨Ò»¸ö ......
×î½üÃæÊÔÁ˺ܶ๫˾£¬Ò»Ð©±ÊÊÔºÍÃæÊÔµÄÎÊÌâÈÃÎÒÉîÓид¥¡£ËäȻ˵×Ô¼º¶ÔJavaÒѾºÜÊìϤÁË£¬²»¹ýµ±Ãæ¶ÔһЩϸ½ÚµÄʱºò£¬×Ô¼ºÈ·ÊµÓеãÎÞ´ÓÏÂÊÖ¡£ÏÂÃæ¼òµ¥µÄÁоټ¸¸öÀý×Ó£º
1.JavaÖжѺÍÕ»µÄÇø±ð
Æäʵ£¬Õâ¸öÎÊÌâ²¢²»ÊǺÜÄÑ¡£²»¹ýÒ²ÐíºÜ¶à³ÌÐòÔ±ÍùÍùºöÂÔÁËÕâÒ»µã£¬ÓÈÆ ......
public class Test4 {
public int binarySearch(int[] items, int value){
int startIndex = 0;
int stopIndex = items.length - 1;
int middle = (int)Math.flo ......
Ö÷Ïß³ÌÖУº
InitThread initThread=new InitThread(new Semaphore(0));//³õʼ»¯Ò»¸ö×ÓỊ̈߳¬´«Ò»¸ö³õֵΪ0µÄÐźÅÁ¿¸øËü
Display.getDefault().asyncExec(initThread);
try {//´Ë´¦»á¹ÒÆð£¬Ö±µ½×ÓÏß³ÌÍê³É¹¤×÷£¬ÐÞ¸ÄÁËÐźÅÁ¿µÄÖµ£¬Ö÷Ï̲߳Żá¼ÌÐø
initThread.getSemaphore().acquire();
} catch (Inte ......