¨Íif¹®Àº '¹è¿­ ¿ø¼Ò°¡ µÑ ÀÌ»óÀ̸é'ÀÇ Àǹ̸¦ ³ªÅ¸³¿ ¨ÎµÑ ÀÌ»óÀÌ¸é ±× ¹è¿­ÀÇ Áß°£À§Ä¡¸¦ ±¸ÇÏ°í
¨ÏºÐÇÒµÈ ¿ÞÂÊÀÇ ºÎºÐ¹è¿­¿¡ ´ëÇÏ¿© MergeSort¸¦ ¼øȯ È£ÃâÇÏ¿© Á¤·ÄÇÏ°í, ¿ÞÂÊ ºÎºÐ¹è¿­ÀÇ Á¤·ÄÀÌ ³¡³µÀ¸¸é
¨Ð¿À¸¥ÂÊ ºÎºÐ¹è¿­¿¡ ´ëÇÏ¿© MergeSort¸¦ ¼øȯ
È£ÃâÇÏ¿© ¿À¸¥ÂÊ ºÎºÐ¹è¿­À» Á¤·ÄÇÔ
¨Ñ¿ÞÂÊ°ú ¿À¸¥ÂÊÀÌ ³¡³µÀ¸¸é À̵éÀ» MergeÇÔ

¨è¨é¨ê¨ëÁÙÀÇ while ·çÇÁ´Â ¸ÓÁö°¡ ÁøÇàµÇ°í ÀÖ´Â µÎ ºÎºÐ¹è¿­ ¸ðµÎ¿¡ ¾ÆÁ÷ Å°°¡ ³²¾Æ ÀÖÀ» ¶§, À̵éÀ» ¸ÓÁö

¨ì¨í¨î¨ï¨ðÁÙÀÇ if¹®Àº µÎ ºÎºÐ¹è¿­ Áß¿¡¼­ ÇÑ °³ÀÇ ¹è¿­¿¡¸¸ Å°°¡ ³²¾Æ ÀÖÀ» ¶§ ÀÌ Å°µéÀ» ¹è¿­ BÀÇ ¸ÓÁöµÈ ¿ø¼Òµé µÚ¿¡ ±×´ë·Î º¹»çÇÏ´Â ÀÏ

¨ñ¨ò for·çÇÁ´Â ¸ÓÁö°¡ ³¡³ª¸é ¹è¿­ B¿¡ ÀÖ´Â Á¤·ÄµÈ Å°µéÀ» ¹è¿­ A¿¡ ¿Å±â´Â ÀÏÀ» ´ã´ç

10°³ÀÇ Å°°¡ ÀÖ´Â ¹è¿­¿¡¸ÓÁö Á¤·ÄÀ» Àû¿ëÇÏ´Â °úÁ¤

|¸ÓÁöÁ¤·Ä °³³ä | ¸ÓÁöÁ¤·Ä ¾Ë°í¸®Áò| ¸ÓÁöÁ¤·Ä ºÐ¼® |

¸ÓÁö Á¤·Ä ¾Ë°í¸®Áò

 

 

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

   void MergeSort(int A[ ], int Low, int High) /* ÀÔ·Â: A[Low:High] - Á¤·ÄÇÏ·Á´Â ¹è¿­ */

   /* Low, High - Á¤·ÄÇÒ ¿ø¼Ò°¡ ÀÖ´Â °÷À» ³ªÅ¸³»´Â ÃÖ¼Ò, ÃÖ´ë À妽º */

   /* Ãâ·Â: A[Low:High] - Á¤·ÄµÈ ¹è¿­ */

       {               

              int Mid;     

 /*a*/    if(Low < High)

              {

/*b*/           Mid = (Low + High)/2;

/*c*/           MergeSort(A[ ], Low, Mid);

/*d*/           MergeSort(A[ ], Mid+1, High);

/*e*/           Merge(A[ ], Low, Mid, High);

               }

       }                                                                   /*  ¸ÓÁö Á¤·Ä ¾Ë°í¸®Áò */

 

   void Merge(int A[ ], int Low, int Mid, int High)

   /* ÀÔ·Â: A[Low:Mid], A[Mid+1:High] - Á¤·ÄµÈ µÎ ¹è¿­ */

   /* Ãâ·Â: A[Low:High]-A[Low:Mid]¿Í A[Mid+1:High]¸¦  ÇÕº´ÇÏ¿© Á¤·ÄµÈ ¹è¿­ */

             { 

                  int B[NUM_OF_KEYS];

                  int i, LeftPtr, RightPtr, BufPtr;

/*1*/        LeftPtr = Low; RightPtr = Mid + 1; BufPtr = Low;

/*2*/        while(LeftPtr <= Mid && RightPtr <= High)

/*3*/               if(A[LeftPtr] < A[RightPtr])

/*4*/                    B[BufPtr++] = A[LeftPtr++];    

/*5*/               else B[BufPtr++] = A[RightPtr++];

/*6*/         if (LeftPtr > Mid)

/*7*/               for (i = RightPtr; i <= High; i++)

/*8*/                    B[BufPtr++] = A[i];

                 else    

/*9*/             for (i=LeftPtr; i <= Mid; i++)

/*10*/                  B[BufPtr++] = A[i];

/*11*/       for (i = Low; i <= High; i++)

/*12*/            A[i] = B[i];             

             }                                                          /* ¸ÓÁö ¾Ë°í¸®Áò  */

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