??xml version="1.0" encoding="utf-8" standalone="yes"?>国产三级精品视频,欧美亚洲一区二区在线观看,在线电影avhttp://www.aygfsteel.com/zhengzhili/archive/2008/06/13/207531.htmlBrianBrianThu, 12 Jun 2008 16:54:00 GMThttp://www.aygfsteel.com/zhengzhili/archive/2008/06/13/207531.htmlhttp://www.aygfsteel.com/zhengzhili/comments/207531.htmlhttp://www.aygfsteel.com/zhengzhili/archive/2008/06/13/207531.html#Feedback3http://www.aygfsteel.com/zhengzhili/comments/commentRss/207531.htmlhttp://www.aygfsteel.com/zhengzhili/services/trackbacks/207531.html         ①把 n 个记录看?n 个长度ؓ l 的有序子表;
        ②进行两两归q记录关键字有序,得到 n/2 个长度ؓ 2 的有序子表; 
        ③重复第②步直到所有记录归q成一个长度ؓ n 的有序表为止?br /> ?归ƈ排序法实例
        对于归ƈ排序法q类的分ȝ法,其核心就?分解"?递归求解"。对?分解"实例Q会在下面分析msort()Ҏ中给出。我们先看合q的q程?br />         以下面描q的序列ZQ在索引范围内[first , last)的序列还有九个整数元素,它由索引范围为[first , mid]的四个元素有序子列表A和烦引范围[mid , last]的五个元素有序子列表Bl成?br />

        步骤1Q比较arr[indexA]=7与arr[indexB]=12。将较小的元?复制到数ltempArr的烦引indexC处。ƈindexA和indexC都指向下一个位|?br />

        步骤2Q比较arr[indexA]=10与arr[indexB]=12。将较小的元?0复制到数ltempArr的烦引indexC处。ƈindexA和indexC都指向下一个位|?br />
        步骤3Q比较arr[indexA]=19与arr[indexB]=12。将较小的元?2复制到数ltempArr的烦引indexC处。ƈindexB和indexC都指向下一个位|?br />

        步骤4-7Q依ơ成Ҏ较两子表的元素将17Q?9Q?1Q?5复制到数ltempArr。此ӞindexA到达子表A的未?indexA = mid)QindexB引用的gؓ30?br />
        步骤8-9Q将未到N的子表剩余数据复制到tempArr中?br />
        步骤10Q将tempArr复制到原始数据arr中?br />
?归ƈ排序法的实?br />     了解了合q过E,那么理解下面的代码ƈ不是一仉事。下面提供了归ƈ法的非泛型版本和泛型版本?br />
public class MergeSort {
    
    
public static void sort(Object[] arr) {
        
//create a temporary array to store partitioned elements
        Object[] tempArr = arr.clone();

        
//call msort with arrays arr and tempArr along
        
//with the index range
        msort(arr, tempArr, 0, arr.length);
    }


    
public static <extends Comparable<? super T>> void sort(T[] arr) {
        
//create a temporary aray to store partitioned elements
        T[] tempArr = (T[]) arr.clone();

        
//call msort with arrays arr and tempArr along
        
//with the index range
        msort(arr, tempArr, 0, arr.length);
    }


    
private static void msort(Object[] arr, Object[] tempArr, int first,
                              
int last) {
        
//if the sublist has more than 1 element continue
        if (first + 1 < last) {
            
//for sublists of size 2 or more, call msort()
            
//for the left and right sublists and than
            
//merge the sorted sublists using merge()
            int midpt = (last + first) / 2;

            msort(arr, tempArr, first, midpt);
            msort(arr, tempArr, midpt, last);

            
//if list is already sorted, just copy src to
            
//dest; this is an optimization that results in faster
            
//sorts for nearly ordered lists
            if (((Comparable) arr[midpt - 1]).compareTo(arr[midpt]) <= 0)
                
return;
            
//the elements in the ranges [first,mid] and
            
//[mid,last] are ordered;merge the ordered sublists
            
//into an ordered sequence in the range [first , last]
            
//using the temporary array
            int indexA, indexB, indexC;

            
//set indexA to scan sublist A with rang [first , mid]
            
//and indexB to scan sublist B with rang [mid , last]
            indexA = first;
            indexB 
= midpt;
            indexC 
= first;

            
//while both sublists are not exhausted, compare
            
//arr[indexA] and arr[indexB]; copy the smaller
            
//to tempArr
            while (indexA < midpt && indexB < last) {
                
if (((Comparable) arr[indexA]).compareTo(arr[indexB]) < 0{
                    tempArr[indexC] 
= arr[indexA]; //copyto tempArr
                    indexA++//increment indexA
                }
 else {
                    tempArr[indexC] 
= arr[indexB]; //copyto tempArr
                    indexB++//increment indexB
                }

                indexC
++//increment indexC
            }

            
//copy the tail of the sublist that is not exhausted
            while (indexA < midpt) {
                tempArr[indexC
++= arr[indexA++]; //copy to tempArr
            }
 while (indexB < last) {
                tempArr[indexC
++= arr[indexB++]; //copy to tempArr
            }

            
//copy elements form temporary array to original array
            for (int i = first; i < last; i++)
                arr[i] 
= tempArr[i];
        }

    }

}
        
        上述代码中最核心的msort()Ҏ是一递归法。下图说明了msort()Ҏ中子列表的分割与合ƈ?nbsp;   

?归ƈ排序法的效?br />         归ƈ排序的最坏情况与q_情况q行旉都ؓO(nlog2n)。假定数l具有n=2k个元素。如下图Q?br />          
        在层?上对msort()Ҏ的第一个调用会产生两个递归调用Q这两个递归调用产生长度为n/2的两个半部分列表Q而merge()Ҏ上qC个半部分列表l合的一个有序的n元素列表Q在层数1上存在两个msort()Ҏ的调用,每个调用又会产生另外两个寚w度ؓn/4的列表的递归调用。每个合q会两个长度ؓn/4的子列表q接Z个长度ؓn/2的有序列表;在层?上存在对merge()Ҏ?=22个调用,每个调用会创Z个长度ؓn/4的有序列表。通常Q在层数i上存在对merge()Ҏ?i个调用,每个调用会创Z个长度ؓn/2i的有序子列表?br />         层数0Q存在对merge()Ҏ?=20ơ调用。这个调用对n个元素排序?br />         层数1Q存在对merge()Ҏ?=21ơ调用。这个调用对n/2个元素排序?br />         层数2Q存在对merge()Ҏ?=22ơ调用。这个调用对n/4个元素排序?br />         ......
        层数iQ存在对merge()Ҏ?iơ调用。这个调用对n/i个元素排序?br />         在树中的每一层,合ƈ涉及hU性运行时间的n/2i个元素,q个U性运行时间需要少于n/2iơ的比较。在层数i上组合的2i个合q操作需要少?i*n/2i=nơ的比较。假定n=2k,分割q程会在n/2k=1的k层数上终止。那么所有层上完成的工作总量?k*n = nlog2n。因此msort()Ҏ的最坏情冉|率ؓO(nlog2n)?

Brian 2008-06-13 00:54 发表评论
]]>
插入排序思\与泛型版本的实现http://www.aygfsteel.com/zhengzhili/archive/2008/06/11/207253.htmlBrianBrianWed, 11 Jun 2008 15:56:00 GMThttp://www.aygfsteel.com/zhengzhili/archive/2008/06/11/207253.htmlhttp://www.aygfsteel.com/zhengzhili/comments/207253.htmlhttp://www.aygfsteel.com/zhengzhili/archive/2008/06/11/207253.html#Feedback4http://www.aygfsteel.com/zhengzhili/comments/commentRss/207253.htmlhttp://www.aygfsteel.com/zhengzhili/services/trackbacks/207253.html一.插入排序法的思\
        假定q个数组的序是排好的Q然后从头往后,如果有数比当前外层元素的值大Q则这个数的位|往后挪Q直到当前外层元素的值大于或者等于它前面的位|ؓ止?br /> ?插入排序法实例
        用五个名?Monroe,Chin,Flores,Stein和Dare)的列表的插入排序法ZQ?br />                                        Monroe    从Monroe开?br />
        处理名字Chin        Chine  Monroe    Chin插入C|?QMonroeUd至位|?

        处理名字Flores     Chine  Flores  Monroe    Flores插入C|?QMonroeUd至位|?

        处理名字Stein       Chine  Flores  Monroe  Stein    Stein位置正确 

        处理名字Dare       Chine  Dare  Flores  Monroe  Stein    Dare插入在位|?Q列表尾部向右移?nbsp;

?插入排序法的实?br />
public class InsertSort {
    
//sort an array of elements using insertion sort

    public static <extends Comparable<? super T>> void sort(T[] arr) {
        
int i, j, n =
 arr.length;
        T target;

        
/**
         * place element at index i into the sublist
         * from index 0 to i-1 where 1<= i,
         * so it is in the correct positon
         
*/

        
for (i = 1; i < n; i++{
            
//
index j scans down list from index i looking for
            
//
correct position to locate target; assigns it to
            
//arr at index j

            j = i;
            target 
=
 arr[i];
            
//
locate insertion point by scanning downward as long
            
//
as target < arr[j] and we have not encountered the
            
//beginning of the array

            while (j > 0 && target.compareTo(arr[j - 1]) < 0{
                
//shift elements up list to make room for insertion

                arr[j] = arr[j - 1];
                j
--
;
            }

            
//the location is found;insert target
            arr[j] = target;
        }

    }

}

?插入排序法的效?br />         假定n是数l的长度Q那么插入排序需要n-1遍。对于通用的遍i来说Q插入操作从arr[0]到arr[i-1]的子列表中,q且需要^均i/2ơ比较。比较的q_L为:
                 T(n) = 1/2 + 2/2 + 3/2 + ...... + (n-2)/2 + (n-1)/2 = n(n-1)/4
        ҎT(n)的主,插入排序法的^均运行时间ؓO(n2)。最好情况ؓO(n)Q最坏情况ؓO(n2)?

Brian 2008-06-11 23:56 发表评论
]]>
վ֩ģ壺 | | Ƹ| | ƽ| ԫ| ˶| DZɽ| | | ʡ| ƽ| | | ɽ| | | | Ǽ| Ȫ| | | ϲ| ǿ| | Զ| | ˫| Т| ӯ| | | | | ɳ| | ¡| ˫| | ɽ| Ʊ|