[¼ö°­±â°£] 2010. 11. 1 ~ 2011. 10. 31
½Åû¹®ÀÇ : À¥Å¸ÀÓ±³À°¼¾ÅÍ 02-3477-8300, ¿î¿µ°ü¸® : ¿ÀÇǽºÆ©ÅÍ 070-7098-2554

±³À°°úÁ¤ ³ªÀÇÇнÀ ¿ÀÇǽºÆÁ °Ë»ö ¹«·á°­ÁÂ
Home ·Î±×ÀΠȸ¿ø°¡ÀÔ °í°´¼¾ÅÍ »çÀÌÆ®¸Ê
¿ÀÇǽºÆ©ÅÍ ±â¾÷ASP¼­ºñ½º > ¹«·á°­ÁÂ
¿¢¼¿ 2000 µû¶óÇϱâ
Ȩ·¯´× ¿¢¼¿ 2000°­ÁÂ
À繫 ÇÔ¼ö °­ÁÂ
¿¢¼¿ 2002 »õ·Î¿î ±â´É
Ȳ±â¼º´ÔÀÇ VBA°­ÁÂ
°û½ÂÁÖ´ÔÀÇ VBAÀ̾߱â
Home > ¹«·á°­Á > ¿¢¼¿
¿¢¼¿, °û½ÂÁÖ´ÔÀÇ ¿ÀÆ©°ø±¸ÇÔ Á¦ÀÛÀ¸·Î ¹è¿öº¸´Â VBA À̾߱â, Excel
  

14. Á¤·Ä ù ¹ø° À̾߱â

ÀÚ·á´Ù¿î·Îµå : ¿ÀÆ©°ø±¸ÇÔ014.xls 
ÀÚ·á´Ù¿î·Îµå : 12°­ ¼öÁ¤³»¿ë.txt

¾È³çÇϼ¼¿ä ¿ÀÆ©°¡Á· ¿©·¯ºÐ!

¿À´ÃºÎÅÍ´Â Á¤·Ä¿¡ °üÇÑ ³»¿ëÀ» ´Ù·ç°íÀÚ ÇÕ´Ï´Ù. Á¤·ÄÀÌ »ç¿ëÀڵ鿡°Ô´Â °£´ÜÇØ º¸ÀÌÁö¸¸ »ç½Ç ¾ó¸¶³ª ¸¹Àº µ¥ÀÌÅ͸¦ »¡¸® Á¤·ÄÇÒ ¼ö Àִ°¡ÀÇ ¹®Á¦¿¡ À̸£¸£¸é ÀûÀÝÀÌ ¾î·Á¿î ¹®Á¦À̱⵵ ÇÕ´Ï´Ù. Á¤·ÄÀ» ÇÏ·Á¸é ¹«¾ð°¡¸¦ ¼­·Î ºñ±³ÇÏ°í ±³È¯ÇØ¾ß ÇÕ´Ï´Ù. ºñ±³È½¼ö¿Í ±³È¯È½¼ö°¡ ÀÛÀ¸¸é ÀÛÀ» ¼ö·Ï ÁÁ°ÚÁÒ.

Á¤·ÄÀ» ´Ù·ç±â¿¡ ¾Õ¼­ Áö³­ ÀúÀÇ Ä÷³¿¡¼­ ¿À·ù°¡ ÀÖ¾î ¼öÁ¤ÇÑ ¹Ù°¡ ÀÖ½À´Ï´Ù. »ç½Ç ¸¹Àº Å×½ºÆ®¸¦ Çغ¸Áú ¾Ê¾Æ¼­ ¿¡·¯°¡ ³­ °ÍÀε¥, ´ÙÇàÈ÷ ÀúÀÇ Ä÷³À» ÀоîÁֽô ¸î ºÐ²²¼­ ÁöÀûÀ» ÇØÁּ̽À´Ï´Ù. ¿©Áö²¯ Ä÷³¿¡ ´ëÇØ ¾î¶² ¸ÞÀÏÀ» º¸³»ÁֽŠºÐµéÀÌ ¾ø¾ú´Âµ¥, ¿©·¯ºÐÀÇ Çǵå¹éÀ» ¹Þ±â À§ÇØ ¾ÕÀ¸·Î´Â ÀÚÁÖ Æ²·ÁºÁ¾ß ÇÒ °Í °°½À´Ï´Ù. ^^; Àú¿¡°Ô ¸ÞÀÏ·Î ÀÌ°É ¾Ë·ÁÁֽŠºÐÀÌ ¼öÁ¤ÇÑ ³»¿ëµµ ´Ù½Ã Ʋ·È´Ù°í ÇϽø鼭 º¸³»ÁֽŠ¼Ò½ºÄڵ带 ÷ºÎÇÏ¿´½À´Ï´Ù. 

Á¤·Ä¿¡ ´ëÇÑ ¿©·¯ °¡Áö ¾Ë°í¸®Áò(insertion sort, selection sort, bubble sort, quick sort, two-way merge sort, heap sort, radix sort)ÀÌ ÀÖÁö¸¸ ¿©±â¼­´Â ºñ±³Àû ºü¸¥ Á¤·Ä¿¡ ¼ÓÇÏ´Â Äü¼ÒÆ®¸¦ ¼Ò°³ÇÏ°íÀÚ ÇÕ´Ï´Ù. Äü¼ÒÆ® ¾Ë°í¸®ÁòÀº ¿µ¿ªÀ» °è¼Ó ºÐÇÒÇÏ¿© ÀÛÀº °ªÀº ¿ÞÂÊÀ¸·Î º¸³»°í, Å« °ªÀº ¿À¸¥ÂÊÀ¸·Î ¸ð´Â ¹æ½ÄÀÔ´Ï´Ù. ±×¸®°í ´õ ÀÌ»ó ºÐÇÒÇÒ ¿©Áö°¡ ¾ø´Ù¸é Á¤·ÄÀº ¿Ï¼ºµÈ °ÍÀÔ´Ï´Ù. ±×·¯³ª Äü¼ÒÆ®°¡ ÀûÇÕÇÏÁö ¾ÊÀº °æ¿ìµµ ÀÖ½À´Ï´Ù. ´ëÇ¥ÀûÀ¸·Î ÀÌ¹Ì Á¤·ÄµÈ »óÅÂÀÇ µ¥ÀÌÅ͸¦ Á¤·ÄÇÏ´Â °ÍÀº Á¤·ÄµÇÁö ¾ÊÀº »óÅÂÀÇ °æ¿ì¿Í ºñ½ÁÇÑ ½Ã°£ÀÌ °É¸³´Ï´Ù.

±¸Ã¼ÀûÀÎ Á¤·Ä¹æ¹ý °³¿ä
Á¤·ÄÇÒ µ¥ÀÌÅÍ°¡ ¹è¿­¿¡ ÀÖ´Ù°í ÇßÀ»¶§

  1. Á¤·ÄÇÒ ¹è¿­ÀÇ »óÇÑ°ú ÇÏÇÑ°ªÀ» ±¸ÇÑ´Ù.
  2. ÇÏÇÑ°ªÀÌ »óÇÑ°ªº¸´Ù ÀÛÀ¸¸é ¹è¿­À» ³ª´«´Ù. 
  3. µÎ ¹è¿­°£ ºñ±³ÇÏ¿© ±³È¯ÇÏ°í µÎ ¹è¿­ÀÇ ÀÛ¾÷´ë»óµµ À̵¿ÇÑ´Ù.
  4. ³ª´©¾îÁø °¢°¢ÀÇ ¹è¿­¿¡ ´ëÇØ À§ÀÇ ´Ü°è¸¦ ¹Ýº¹ÇÑ´Ù.

ÀÌ·¯ÇÑ ÀýÂ÷¸¦ ´ÙÀ½°ú °°Àº ÇϳªÀÇ ¿¹¸¦ ÅëÇØ ±×¸²À¸·Î Ç¥ÇöÇÏ¸é ´ÙÀ½°ú °°½À´Ï´Ù. À̹ø ¿¹´Â ÃÑ 23´Ü°è¿¡ °ÉÃÄ ¼ÒÆ®°¡ ÁøÇàµÇÁö¸¸ ÆíÀÇ»ó ¿ø¸®¸¸ ¼³¸íÇÏ°íÀÚ 7´Ü°è¸¸ ¼³¸íÇÕ´Ï´Ù. Âü°í·Î À̹ø ¿¹¿Í Äü¼ÒÆ® ÄÚµå´Â Á¦°¡ ¸¸µç °ÍÀÌ ¾Æ´Ï¶ó Á¦°¡ °¡Áø Ã¥À» ÂüÁ¶Çß½À´Ï´Ù. Áï ´Ù¸¥ ¸»·Î ÇÏ¸é º£²¼½À´Ï´Ù. Ã¥Àº Ken Getz¿Í Mike Gilbert°¡ °øÀúÇÑ VBA Developer's Handbook ÀÔ´Ï´Ù. 

014_1.gif(31441¹ÙÀÌÆ®)

014_2.gif(19707¹ÙÀÌÆ®)

¿ø·¡ÀÇ µ¥ÀÌÅÍ´Â 79,30,24,48,26,34,05,48,21,86(10°³)ÀÔ´Ï´Ù. ÀÌ°É Äü¼ÒÆ®·Î Á¤·ÄÇÏ¿© º¸´Â °Ì´Ï´Ù. À§ÀÇ ±×¸²¿¡¼­ °¢ ´Ü°è¸¦ ¼ýÀڷΠǥ½ÃÇÏ¿´À¸¸ç °¢ ´Ü°è¸¦ ¼³¸íÇÏ°Ú½À´Ï´Ù. 

1´Ü°è: 
¹è¿­ÀÇ ÇÏÇÑÀ§Ä¡(i)ÀÇ °ªÀÌ »óÇÑÀ§Ä¡(j)ÀÇ °ªº¸´Ù ÀÛÀ¸¸é Áß°£À§Ä¡(°ªÀº 26)¸¦ ±¸ÇÕ´Ï´Ù. ±×¸®°í ÇÏÇÑÀ§Ä¡°ª(79)°ú Áß°£À§Ä¡°ª(26)º¸´Ù ÀÛÀ¸¸é ÇÏÇÑÀ§Ä¡(i)¸¦ ¿À¸¥ÂÊÀ¸·Î Áõ°¡ÇÕ´Ï´Ù. ¸¶Âù°¡Áö·Î »óÇÑÀ§Ä¡°ª(86)ÀÌ Áß°£À§Ä¡°ª(26)º¸´Ù Å©¸é »óÇÑÀ§Ä¡(j)¸¦ ¿ÞÂÊÀ¸·Î ¿Å±ä´Ù. ¿©±â¿¡¼± 86ÀÌ 26º¸´Ù Å©¹Ç·Î ÇÑ Æ÷ÀÎÆ® ¿Å°Ü 21À» °¡¸®Å°µµ·Ï ÇÏ¿´½À´Ï´Ù.
i=0
j=9->8·Î À̵¿ÇÔ.

2´Ü°è: 
ÇÏÇÑÀ§Ä¡°¡ »óÇÑÀ§Ä¡º¸´Ù ÀÛÀ¸¸é(i<j)ÀÌ¸é ¼­·ÎÀÇ °ªÀ» ±³È¯ÇÕ´Ï´Ù. 

3´Ü°è:
±³È¯ÈÄ ÇÏÇÑ°ªÀº 1Áõ°¡ÇÏ°í(i=i+1) »óÇÑ°ªÀº 1 °¨¼Ò½Ãŵ´Ï´Ù(j=j-1)
i=0->1
j=8->7

4´Ü°è:
ÇÏÇÑÀ§Ä¡°ª(30)ÀÌ Áß°£À§Ä¡°ª(26)º¸´Ù ÀÛÁö ¾ÊÀ¸¹Ç·Î ±×´ë·Î ÇÏÇÑÀ§Ä¡´Â ±×´ë·Î µÎ°í, »óÇÑÀ§Ä¡°ª(48)ÀÌ Áß°£À§Ä¡°ª(26)º¸´Ù Å©¹Ç·Î »óÇÑÀ§Ä¡¸¦ ¿ÞÂÊÀ¸·Î ¿Å°Ü(j=j-1) »óÇÑÀ§Ä¡°ªÀÌ 05°¡ µË´Ï´Ù. ÇÏÇÑÀ§Ä¡°¡ 1ÀÌ°í »óÇÑÀ§Ä¡°¡ 6ÀÔ´Ï´Ù. ÇÏÇÑÀ§Ä¡°¡ »óÇÑÀ§Ä¡º¸´Ù ÀûÀ¸¹Ç·Î ¼­·ÎÀÇ À§Ä¡°ªÀ» ±³È¯ÇÕ´Ï´Ù.

5´Ü°è:
3´Ü°è¿Í °°ÀÌ ÇÏÇÑ°ª°ú »óÇÑ°ªÀÇ À§Ä¡¸¦ Áõ°¡/°¨¼Ò½Ãŵ´Ï´Ù.

6´Ü°è¿Í 7´Ü°è ¿ª½Ã À§ÀÇ ´Ü°è¸¦ ¹Ýº¹ÇÕ´Ï´Ù. ÀÌ ¿¹¿¡¼­ Á¤·ÄÀÌ ¿Ï·áµÇ·Á¸é ÃÑ23´Ü°è¸¦ °ÅĨ´Ï´Ù. »ç½Ç ÀÌ·¯ÇÑ ¾Ë°í¸®ÁòÀ» ÈÍÈ÷ ¾Ë¸é ÁÁÁö¸¸ Àú ¿ª½Ã ±×·± ºÐ¾ß¸¦ º¸¸é ÇÏÇ°ÀÌ ³ª´Â óÁö¶ó Äü¼ÒÆ®°¡ ¾î¶»°Ô ÀÌ·ç¾î Áö´Â°¡¸¸ ¾Ë¾Æ º¸¾Ò½À´Ï´Ù. 

´ÙÀ½Àº Äü¼ÒÆ®¼Ò½º¿Í »ç¿ë¿¹ÀÔ´Ï´Ù.

Quick Sort¼Ò½º
Attribute VB_Name = "QuickSort"
Option Explicit

' From "VBA Developer's Handbook"
' by Ken Getz and Mike Gilbert
' Copyright 1997; Sybex, Inc. All rights reserved.

' Quicksort for simple data types.

' Indicate that a parameter is missing.
Const dhcMissing = -2

Sub dhQuickSort(varArray As Variant, _
   Optional intLeft As Integer = dhcMissing, _
   Optional intRight As Integer = dhcMissing)

     ' From "VBA Developer's Handbook"
     ' by Ken Getz and Mike Gilbert
     ' Copyright 1997; Sybex, Inc. All rights reserved.

     ' Entry point for sorting the array.

     ' This technique uses the recursive Quicksort
     ' algorithm to perform its sort.

     ' In:
     '     varArray:
     '           A variant pointing to an array to be sorted.
     '           This had better actually be an array, or the
     '           code will fail, miserably. You could add
     '           a test for this:
     '           If Not IsArray(varArray) Then Exit Sub
     '           but hey, that would slow this down, and it's
     '           only YOU calling this procedure.
     '           Make sure it's an array. It's your problem.
     '     intLeft:
     '     intRight:
     '           Lower and upper bounds of the array to be sorted.
     '           If you don't supply these values (and normally, you won't)
     '           the code uses the LBound and UBound functions
     '           to get the information. In recursive calls
     '           to the sort, the caller will pass this information in.
     '           To allow for passing integers around (instead of
     '           larger, slower variants), the code uses -2 to indicate
     '           that you've not passed a value. This means that you won't
     '           be able to use this mechanism to sort arrays with negative
     '           indexes, unless you modify this code.
     ' Out:
     '          The data in varArray will be sorted.

     Dim i As Integer
     Dim j As Integer
     Dim varTestVal As Variant
     Dim intMid As Integer

     If intLeft = dhcMissing Then intLeft = LBound(varArray)
     If intRight = dhcMissing Then intRight = UBound(varArray)

     If intLeft < intRight Then
          intMid = (intLeft + intRight) \ 2
          varTestVal = varArray(intMid)
          i = intLeft
          j = intRight
          Do
               Do While varArray(i) < varTestVal
                    i = i + 1
               Loop
               Do While varArray(j) > varTestVal
                    j = j - 1
               Loop
               If i <= j Then
                    SwapElements varArray, i, j
                    i = i + 1
                    j = j - 1
               End If
          Loop Until i > j
          ' To optimize the sort, always sort the
          ' smallest segment first.
          If j <= intMid Then
               Call dhQuickSort(varArray, intLeft, j)
               Call dhQuickSort(varArray, i, intRight)
          Else
               Call dhQuickSort(varArray, i, intRight)
               Call dhQuickSort(varArray, intLeft, j)
          End If
     End If
End Sub


Private Sub SwapElements(varItems As Variant, intItem1 As Integer, intItem2 As Integer)
     Dim varTemp As Variant

     varTemp = varItems(intItem2)
     varItems(intItem2) = varItems(intItem1)
     varItems(intItem1) = varTemp
End Sub
Option Explicit

Sub Demo_QuickSort()
     Dim varItem
     Dim i As Byte

     varItem = Array(79, 30, 24, 48, 26, 34, 5, 48, 21, 86)
     Call dhQuickSort(varItem)

     For i = LBound(varItem) To UBound(varItem)
          Debug.Print varItem(i)
     Next
End Sub

´ÙÀ½ ½Ã°£¿¡´Â ¼ÒÆ®¿Í °ü·ÃÇÑ ¿¢¼¿°ú VBAÀÇ À̸ðÀú¸ð¿¡ ´ëÇØ ¾Ë¾Æº¼±î ÇÕ´Ï´Ù. 

¸ñÂ÷ | ÀÌÀü | ´ÙÀ½

¡¡

¿ÀÇǽºÆ©ÅÍ ±â¾÷ASP¼­ºñ½º 135-880 ¼­¿ï½Ã °­³²±¸ »ï¼ºµ¿ 157-3 ¿¤ÁöÆ®À©ÅÚ 2Â÷ 1603È£ ¢Ï070-7098-2554
Copyright ¨Ï 1999-2008 Officetutor.com All rights reserved
¡¡