ÉèΪÊ×Ò³ ¼ÓÈëÊÕ²Ø

TOP

Ê®´óÅÅÐòËã·¨ÃæÊÔÌâ(¶þ)
2014-11-24 02:02:04 ¡¾´ó ÖРС¡¿ ä¯ÀÀ:803´Î
Tags£ºÊ®´ó ÅÅÐò Ëã·¨ ÊÔÌâ
gth];
int i = 0, j = 0, k = 0;
while (i < a.Length && j < b.Length)
{
if (a[i] < b[j])//×óÊý×éÖÐÔªËØСÓÚÓÒÊý×éÖÐÔªËØ
{
result[k++] = a[i++];//½«Ð¡µÄÄǸö·Åµ½½á¹ûÊý×é
}
else//×óÊý×éÖÐÔªËØ´óÓÚÓÒÊý×éÖÐÔªËØ
{
result[k++] = b[j++];//½«Ð¡µÄÄǸö·Åµ½½á¹ûÊý×é
}
}
while (i < a.Length)//ÕâÀïÆäʵÊÇ»¹ÓÐ×óÔªËØ£¬µ«Ã»ÓÐÓÒÔªËØ
{
result[k++] = a[i++];
}
while (j < b.Length)//ÓÒÓÒÔªËØ£¬ÎÞ×óÔªËØ
{
result[k++] = b[j++];
}
return result;//·µ»Ø½á¹ûÊý×é
}
×¢£º´ËËã·¨ÓÉÖÜÀû»ªÌṩ£¨http://www.cnblogs.com/architect/archive/2009/05/06/1450489.html
£©
»ùÊýÅÅÐò
»ùÊýÅÅÐò·¨Óֳơ°Í°×Ó·¨¡±£¨bucket sort£©»òbin sort£¬¹ËÃû˼Ò壬ËüÊÇ͸¹ý¼üÖµµÄ²¿·Ý×ÊѶ£¬½«ÒªÅÅÐòµÄÔªËØ·ÖÅäÖÁijЩ¡°Í°¡±ÖУ¬½åÒÔ´ïµ½ÅÅÐòµÄ×÷Ó㬻ùÊýÅÅÐò·¨ÊÇÊôÓÚÎȶ¨ÐÔµÄÅÅÐò£¬Æäʱ¼ä¸´ÔÓ¶ÈΪO (nlog(r)m)£¬ÆäÖÐrΪËù²ÉÈ¡µÄ»ùÊý£¬¶ømΪ¶ÑÊý£¬ÔÚijЩʱºò£¬»ùÊýÅÅÐò·¨µÄЧÂʸßÓÚÆäËüµÄ±È½ÏÐÔÅÅÐò·¨¡£
//»ùÊýÅÅÐò
public int[] RadixSort(int[] ArrayToSort, int digit)
{
//low to high digit
for (int k = 1; k <= digit; k++)
{
//temp array to store the sort result inside digit
int[] tmpArray = new int[ArrayToSort.Length];
//temp array for countingsort
int[] tmpCountingSortArray = new int[10]{0,0,0,0,0,0,0,0,0,0};
//CountingSort
for (int i = 0; i < ArrayToSort.Length; i++)
{
//split the specified digit from the element
int tmpSplitDigit = ArrayToSort[i]/(int)Math.Pow(10,k-1) - (ArrayToSort[i]/(int)Math.Pow(10,k))*10;
tmpCountingSortArray[tmpSplitDigit] += 1;
}
for (int m = 1; m < 10; m++)
{
tmpCountingSortArray[m] += tmpCountingSortArray[m - 1];
}
//output the value to result
for (int n = ArrayToSort.Length - 1; n >= 0; n¨C)
{
int tmpSplitDigit = ArrayToSort[n] / (int)Math.Pow(10,k ¨C 1) ¨C (ArrayToSort[n]/(int)Math.Pow(10,k)) * 10;
tmpArray[tmpCountingSortArray[tmpSplitDigit]-1] = ArrayToSort[n];
tmpCountingSortArray[tmpSplitDigit] -= 1;
}
//copy the digit-inside sort result to source array
for (int p = 0; p < ArrayToSort.Length; p++)
{
ArrayToSort[p] = tmpArray[p];
}
}
return ArrayToSort;
}
¼ÆÊýÅÅÐò
ÊýÅÅÐò, »ùÊýÅÅÐò, Í°ÅÅÐòµÈ·Ç±È½ÏÅÅÐòËã·¨,ƽ¾ùʱ¼ä¸´ÔӶȶ¼ÊÇO(n). ÕâЩÅÅÐòÒòΪÆä´ýÅÅÐòÔªËر¾Éí¾Íº¬ÓÐÁ˶¨Î»ÌØÕ÷,Òò¶ø²»ÐèÒª±È½Ï¾Í¿ÉÒÔÈ·¶¨ÆäÇ°ºóλÖÃ,´Ó¶ø¿ÉÒÔÍ»ÆƱȽÏÅÅÐòË㷨ʱ¼ä¸´ÔÓ¶ÈO(nlgn)µÄÀíÂÛÏÂÏÞ.
¼ÆÊýÅÅÐòÊÇ×î¼òµ¥µÄÌØÀý,ËüÒª Çó´ýÅÅÐòÔªËØÊÇλÓÚ0µ½kÖ®¼äµÄÕýÕûÊý , Òò¶øËüÊǺÜÌØÊâµÄÇé¿ö,»ù±¾ÉÏûÓÐÌرðµÄÓ¦ÓüÛÖµ; µ«ÊÇÁíÒ»·½Ãæ, ËüÓÖÊÇ»ùÊýÅÅÐòµÄ»ù´¡,»òÕß˵ÊÇÒ»²¿·Ö,ËùÒÔ¼òµ¥µÄÃèÊöÒ»ÏÂ:
ÊäÈëÊý×é A : ÔªËØÌØÕ÷ÊÇ 0-kµÄÕýÕûÊý,¿ÉÒÔÓÐÖظ´Öµ;
Êä³öÊý×é B : Êä³öAµÄÒ»¸ö·Ç¼õÐòÁÐ
ÖмäÊý×é C : ´óСÊÇk, ËüµÄi( 0<= i <= k)Ë÷ÒýλÖô洢µÄÊÇAÔªËؼ¯ºÏÖÐÖµÊÇkµÄÔªËصĸöÊýÓйØ.
Ëã·¨µÄ»ù±¾Ë¼ÏëÊÇ:
ͳ¼ÆAÖÐÔªËصÄÖµµÄ¼¯ºÏ, ÒÔAÖÐÔªËصÄֵΪË÷Òý, ½«ÖµµÄ¸öÊýÌîдµ½ÖмäÊý×éCµÄ¶ÔÓ¦´¦.
¶ÔC´ÓÍ·¿ªÊ¼×ÔÀÛ¼Ó, ÕâÑùCÖд洢µÄ¾ÍÊÇ, µ±ÊäÈëÊý×éAÖеÄֵΪµ±Ç°Ë÷Òýʱ, ËüÇ°ÃæµÄÔªËØÊýÁ¿(°üº¬Öظ´ÔªËØ).
½«CÒÀ´ÎÊä³öµ½Êä³öÊý×éBÖÐ.
///


/// input array /// the value arrange in input array ///
public int[] CountingSort(int[] arrayA, int arrange)
{
//array to store the sorted result,
//size is the same with input array.
int[] arrayResult = new int[arrayA.Length];
//array to store the direct value in sorting process
//include index 0;
//size is arrange+1;
int[] arrayTemp = new int[arrange+1];
//clear up the temp array
for(int i = 0; i <= arrange; i++)
{
arrayTemp[i] = 0;
}
//now temp array stores the count of value equal
for(int j = 0; j < arrayA.Length; j++)
{
arrayTemp[arrayA[j]] += 1;
}
//now temp array stores the count of value lower and equal
for(int k = 1; k <= arrange; k++)
{
arrayTemp[k] += arrayTemp[k - 1];
}
//output the value to result
for (int m = arrayA.Length-1; m >= 0; m¨C)
{
arrayResult[arrayTemp[arrayA[m]] ¨C 1] = arrayA[m];
arrayTemp[arrayA[m]] -= 1;
}
return arrayResult;
}
¸ù¶ÑÅÅÐò
¶ÑÅÅÐòÊÇÒ»ÖÖÊ÷ÐÎÑ¡ÔñÅÅÐò£¬ÔÚÅÅÐò¹ý³ÌÖУ¬½«A[n]¿´³ÉÊÇÍêÈ«¶þ²æÊ÷µÄ˳Ðò´æ´¢½á¹¹£¬ÀûÓÃÍêÈ«¶þ²æÊ÷ÖÐË«Ç×½áµãºÍº¢×Ó½áµãÖ®¼äµÄÄÚÔÚ¹ØϵÀ´Ñ¡Ôñ×îСµÄÔªËØ¡£


¶ÑÅÅÐòÊDz»Îȶ¨µÄ¡£Ë㷨ʱ¼ä¸´ÔÓ¶ÈO(nlog n)¡£
///


/// /// ///


private void HeapSort(ref double[] dblArray)
{
for (int i = dblArray.Length ¨C 1; i >= 0; i¨C)
{
if (2 * i + 1 < dblArray.Length)
{
int MinChildrenIndex = 2 * i + 1;
//±È½Ï×ó×ÓÊ÷ºÍÓÒ×ÓÊ÷£¬¼Ç¼×îСֵµÄIndex
if (2 * i + 2 < dblArray.Length)
{
if (dblArray[2 * i + 1] > dblArray[2 * i + 2])
MinChildrenIndex = 2 * i + 2;
}
if (dblArray[i] > dblArray[MinChildrenIndex])
{


Exchageva lue(ref dblArray[i], ref dblArray[MinChildrenIndex]);
NodeSort(ref dblArray, MinChildrenIndex);
}
}
}
}
///


/// ///


private void NodeSort(ref double[] dblArray, int StartIndex)
{
while (2 * StartIndex + 1 < dblArray.Length)
{
int MinChildrenIndex = 2 * StartIndex + 1;
if (2 * StartIndex + 2 < dblArray.Length)
{
if (dblArray[2 * StartIndex + 1] > d

Ê×Ò³ ÉÏÒ»Ò³ 1 2 3 ÏÂÒ»Ò³ βҳ 2/3/3
¡¾´ó ÖРС¡¿¡¾´òÓ¡¡¿ ¡¾·±Ìå¡¿¡¾Í¶¸å¡¿¡¾Êղء¿ ¡¾ÍƼö¡¿¡¾¾Ù±¨¡¿¡¾ÆÀÂÛ¡¿ ¡¾¹Ø±Õ¡¿ ¡¾·µ»Ø¶¥²¿¡¿
ÉÏһƪ£ºÈí¼þ²âÊÔÃæÊÔ±ØÎÊÌâ¼°´ð°¸ ÏÂһƪ£ºÇ©Ô¼»ªÎª Ãæ¾­·ÖÏí

×îÐÂÎÄÕÂ

ÈÈÃÅÎÄÕÂ

Hot ÎÄÕÂ

Python

C ÓïÑÔ

C++»ù´¡

´óÊý¾Ý»ù´¡

linux±à³Ì»ù´¡

C/C++ÃæÊÔÌâÄ¿