1. Trang chủ
  2. » Công Nghệ Thông Tin

DATA STRUCTURES AND ALGORITHMS USING VISUAL BASIC.NET phần 8 pps

42 309 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 42
Dung lượng 312,08 KB

Nội dung

P1: IWV 0521547652c14 CB820-McMillan-v1 April 21, 2005 18:46 284 ADVANCED SORTING ALGORITHMS good increment to use is based on the code fragment While (h <= numElements / 3) h=h*3+1 End While where numElements is the number of elements in the data set being sorted, such as an array. For example, if the sequence number generated by this code is 4, every fourth element of the data set is sorted. Then a new sequence number is chosen, using this code: h=(h-1)/3 Then the next h elements are sorted, and so on. Let’s look at the code for the ShellSort algorithm (using the ArrayClass code from Chapter 4): Public Sub ShellSort() Dim inner, outer, temp As Integer Dim h As Integer = 1 While (h <= numElements / 3) h=h*3+1 End While While (h > 0) For outer=hTonumElements - 1 temp = arr(outer) inner = outer While (inner>h-1 AndAlso arr(inner - h) >= temp) arr(inner) = arr(inner - h) inner -= h End While arr(inner) = temp Next h=(h-1)/3 End While End Sub P1: IWV 0521547652c14 CB820-McMillan-v1 April 21, 2005 18:46 MergeSort Algorithm 285 Here’s some code to test the algorithm: Sub Main() Const SIZE As Integer = 19 Dim theArray As New CArray(SIZE) Dim index As Integer For index=0ToSIZE theArray.Insert(Int(100 * Rnd() + 1)) Next Console.WriteLine() theArray.showArray() Console.WriteLine() theArray.ShellSort() theArray.showArray() Console.Read() End Sub The output from this code looks like this: The ShellSort is often considered a good advanced sorting algorithm to use because of its fairly easy implementation, but its performance is acceptable even for data sets in the tens of thousands of elements. MERGESORT ALGORITHM The MergeSort algorithm exemplifies a recursive algorithm. This algorithm works by breaking up the data set into two halves and recursively sorting each half. When the two halves are sorted, they are brought together using a merge routine. The easy work comes when sorting the data set. Let’s say we have the following data in the set: 71 54 58 29 31 78 2 77. First, the data set is broken P1: IWV 0521547652c14 CB820-McMillan-v1 April 21, 2005 18:46 286 ADVANCED SORTING ALGORITHMS up into two separate sets: 71 54 58 29 and 31 78 2 77. Then each half is sorted to give 29 54 58 71 and 2 31 77 78. Then the two sets are merged, resulting in 2 29 31 54 58 71 77 78. The merge process compares the first two elements of each data set (stored in temporary arrays), copying the smaller value to yet another array. The element not added to the third array is then compared to the next element in the other array. The smaller element is added to the third array, and this process continues until both arrays run out of data. But what if one array runs out of data before the other? Because this is likely to happen, the algorithm makes provisions for this situation. The algorithm uses two extra loops that are called only if one or the other of the two arrays still has data in it after the main loop finishes. Now let’s look atthe code for performing a MergeSort. The first two methods are the MergeSort and the recMergeSort methods. The first method simply launches the recursive subroutine recMergeSort, which performs the sorting of the array. Here is our code: Public Sub mergeSort() Dim tempArray(numElements) As Integer recMergeSort(tempArray, 0, numElements - 1) End Sub Public Sub recMergeSort(ByVal tempArray() As Integer, _ ByVal lbound As Integer, ByVal _ ubound As Integer) If (lbound = ubound) Then Return Else Dim mid As Integer = (lbound + ubound) \ 2 recMergeSort(tempArray, lbound, mid) recMergeSort(tempArray, mid + 1, ubound) merge(tempArray, lbound, mid + 1, ubound) End If End Sub In recMergeSort, the first If statement is the base case of the recursion, re- turning to the calling program when the condition becomes True. Otherwise, the middle point of the array is found and the routine is called recursively on the bottom half of the array (the first call to recMergeSort) and then on P1: IWV 0521547652c14 CB820-McMillan-v1 April 21, 2005 18:46 MergeSort Algorithm 287 the top half of the array (the second call to recMergeSort). Finally, the entire array is merged by calling the merge method. Here is the code for the merge method: Public Sub merge(ByVal tempArray() As Integer, ByVal _ lowp As Integer, ByVal highp As _ Integer, ByVal ubound As Integer) Dim j As Integer = 0 Dim lbound As Integer = lowp Dim mid As Integer = highp - 1 Dim n As Integer = ubound - lbound + 1 While (lowp <= mid And highp <= ubound) If (arr(lowp) < arr(highp)) Then tempArray(j) = arr(lowp) j+=1 lowp += 1 Else tempArray(j) = arr(highp) j+=1 highp += 1 End If End While While (lowp <= mid) tempArray(j) = arr(lowp) j+=1 lowp += 1 End While While (highp <= ubound) tempArray(j) = arr(highp) j+=1 highp += 1 End While Forj=0Ton-1 arr(lbound + j) = tempArray(j) Next End Sub This method is called each time the recMergeSort subroutines perform a preliminary sort. To demonstrate better how this method works along with P1: IWV 0521547652c14 CB820-McMillan-v1 April 21, 2005 18:46 288 ADVANCED SORTING ALGORITHMS recMergeSort, let’s add one line of code to the end of the merge method: Me.showArray() With this one line, we can view the array in its different temporary states before it is completely sorted. Here’s the output: The first line shows the array in the original state. The second line shows the beginning of the lower half being sorted. By the fifth line, the lower half is completely sorted. The sixth line shows that the upper half of the array is beginning to be sorted and the ninth line shows that both halves are completed sorted. The tenth line is the output from the final merge and the eleventh line is just another call to the showArray method. HEAPSORT ALGORITHM The HeapSort algorithm makes use of a data structure called a heap.Aheap is similar to a binary tree, but with some important differences. The Heap- Sort algorithm, although not the fastest algorithm in this chapter, has some attractive features that encourage its use in certain situations. Building a Heap Unlike binary trees, heaps are usually built using arrays rather than using node references. There are two very important conditions for a heap: 1. A heap must be complete, meaning that each row must be filled in, and 2. each P1: IWV 0521547652c14 CB820-McMillan-v1 April 21, 2005 18:46 HeapSort Algorithm 289 77 71 31 29 78 58 2 82 54 FIGURE 14.1. A Heap. node contains data that are greater than or equal to the data in the child nodes below it. An example of a heap is shown in Figure 14.1. The array that stores the heap is shown in Figure 14.2. The data we store in a heap are built from a Node class, similar to the nodes we’ve used in other chapters. This particular Node class, however, will hold just one piece of data, its primary, or key, value. We don’t need any references to other nodes but we prefer using a class for the data so that we can easily change the data type of the data being stored in the heap if we need to. Here’s the code for the Node class: Class Node Public data As Integer Public Sub New(ByVal key As Integer) data = key End Sub End Class Heaps are built by inserting nodes into the heap array. A new node is always placed at the end of the array in an empty array element. However, doing this 82 77 78 71 31 58 2 54 29 012345678 FIGURE 14.2. An Array For Storing the Heap in Figure 14-1. P1: IWV 0521547652c14 CB820-McMillan-v1 April 21, 2005 18:46 290 ADVANCED SORTING ALGORITHMS will probably break the heap condition because the new node’s data value may be greater than some of the nodes above it. To restore the array to the proper heap condition, we must shift the new node up until it reaches its proper place in the array. We do this with a method called ShiftUp. Here’s the code: Public Sub ShiftUp(ByVal index As Integer) Dim parent As Integer = (index - 1) / 2 Dim bottom As Node = heapArray(index) While (index>0And heapArray(parent).data < _ bottom.data) heapArray(index) = heapArray(parent) index = parent parent = (parent - 1) / 2 End While heapArray(index) = bottom End Sub And here’s the code for the Insert method: Public Function Insert(ByVal key As Integer) As Boolean If (currSize = maxSize) Then Return False End If Dim newNode As New Node(key) heapArray(currSize) = newNode ShiftUp(currSize) currSize += 1 Return True End Function The new node is added at the end of the array. This immediately breaks the heap condition, so the new node’s correct position in the array is found by the ShiftUp method. The argument to this method is the index of the new node. The parent of this node is computed in the first line of the method. The new node is then saved in a Node variable, bottom. The While loop then finds the correct spot for the new node. The last line then copies the new node from its temporary location in bottom to its correct position in the array. P1: IWV 0521547652c14 CB820-McMillan-v1 April 21, 2005 18:46 HeapSort Algorithm 291 Removing a node from a heap always means removing the node with highest value. This is easy to do because the maximum value is always in the root node. The problem is that once the root node is removed, the heap is incomplete and must be reorganized. To make the heap complete again we use the following algorithm: 1. Remove the node at the root. 2. Move the node in the last position to the root. 3. Trickle the last node down until it is below a larger node and above a smaller node. Applying this algorithm continually removes the data from the heap in sorted order. Here is the code for the Remove and TrickleDown methods: Public Function Remove() As Node Dim root As Node = heapArray(0) currSize -= 1 heapArray(0) = heapArray(currSize) ShiftDown(0) Return root End Function Public Sub ShiftDown(ByVal index As Integer) Dim largerChild As Integer Dim top As Node = heapArray(index) While (index < (currSize \ 2)) Dim leftChild As Integer=2*index + 1 Dim rightChild As Integer = leftChild + 1 If (rightChild <currSize And heapArray(leftChild). _ data < heapArray(rightChild).data) Then largerChild = rightChild Else largerChild = leftChild End If If (top.data >= heapArray(largerChild).data) Then Exit While End If heapArray(index) = heapArray(largerChild) P1: IWV 0521547652c14 CB820-McMillan-v1 April 21, 2005 18:46 292 ADVANCED SORTING ALGORITHMS index = largerChild End While heapArray(index) = top End Sub We now have all we need to perform a heap sort, so let’s look at a program that builds a heap and then sorts it: Sub Main() Const SIZE As Integer = 9 Dim aHeap As New Heap(SIZE) Dim sortedHeap(SIZE) As Node Dim index As Integer For index=0ToSIZE - 1 Dim aNode As New Node(Int(100 * Rnd() + 1)) aHeap.InsertAt(index, aNode) aHeap.incSize() Next Console.Write("Random: ") aHeap.showArray() Console.WriteLine() Console.Write("Heap: ") For index = SIZE \ 2-1To0Step -1 aHeap.ShiftDown(index) Next aHeap.showArray() For index = SIZE-1To0Step -1 Dim bigNode As Node = aHeap.Remove() aHeap.InsertAt(index, bigNode) Next Console.WriteLine() Console.Write("Sorted: ") aHeap.showArray() Console.Read() End Sub The first For loop begins the process of building the heap by insert- ing random numbers into the heap. The second loop heapifies the heap, P1: IWV 0521547652c14 CB820-McMillan-v1 April 21, 2005 18:46 QuickSort Algorithm 293 and the third For loop then uses the Remove method and the TrickleDown method to rebuild the heap in sorted order. Here’s the output from the program: HeapSort is the second fastest of the advanced sorting algorithms we ex- amine in this chapter. Only the QuickSort algorithm, which we discuss in the next section, is faster. QUICK SORT ALGORITHM QuickSort has a reputation, deservedly earned, as the fastest algorithm of the advanced algorithms we’re discussing in this chapter. This is true only for large, mostly unsorted data sets. If the data set is small (100 elements or less), or if the data are relatively sorted, you should use one of the fundamental algorithms discussed in Chapter 4. Description of the QuickSort Algorithm To understand how the QuickSort algorithm works, imagine you are a teacher and you have to alphabetize a stack of student papers. You will pick a letter from the middle of the alphabet, such as M, putting student papers whose name starts with A through M in one stack and those whose names start with N through Z in another stack. Then you split the A–M stack into two stacks and the N–Z stack into two stacks using the same technique. Then you do the same thing again until you have a set of small stacks (A–C, D–F, ,X–Z) of two or three elements that sort easily. Once the small stacks are sorted, you simply put all the stacks together and you have a set of sorted papers. As you should have noticed, this process is recursive, since each stack is divided into smaller and smaller stacks. Once a stack is broken down into one element, that stack cannot be further divided and the recursion stops. [...]... last until split value or > last Decrement last until . theFirst and last Repeat process Decrement last until <= split value 87 84 65 72 91 99 89 theFirst first last 87 84 65 72 91 99 89 65 84 87 72 91 99 89 theFirst firstlast Swap elements at first and. separate sets: 71 54 58 29 and 31 78 2 77. Then each half is sorted to give 29 54 58 71 and 2 31 77 78. Then the two sets are merged, resulting in 2 29 31 54 58 71 77 78. The merge process compares. IWV 0521547652c14 CB820-McMillan-v1 April 21, 2005 18: 46 HeapSort Algorithm 289 77 71 31 29 78 58 2 82 54 FIGURE 14.1. A Heap. node contains data that are greater than or equal to the data in the child

Ngày đăng: 12/08/2014, 16:21

TỪ KHÓA LIÊN QUAN