`
zendj
  • 浏览: 116340 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

C#排序算法汇集

阅读更多
  1. AbstractSort
    usingSystem;

    namespaceCore
    ...{
    internalabstractclassAbstractSort
    ...{
    protectedint[]list;

    publiceventSortingDelegateGreenDataChangedEvent;
    publiceventSortingDelegateRedDataChangedEvent;
    publiceventSortingDelegateDataChangedEvent;

    protectedboolstop=false;

    publicboolStop
    ...{
    get...{returnstop;}
    set...{stop=value;}
    }


    publicabstractvoidSort(int[]list);

    protectedvoidGreenDataChanged(intnewIndex)
    ...{
    if(this.GreenDataChangedEvent!=null)
    GreenDataChangedEvent(newIndex);
    }


    protectedvoidRedDataChanged(intnewIndex)
    ...{
    if(this.RedDataChangedEvent!=null)
    RedDataChangedEvent(newIndex);
    }


    protectedvoidDataChanged(intnewIndex)
    ...{
    if(this.DataChangedEvent!=null)
    DataChangedEvent(newIndex);
    }


    protectedvoidswap(intpos1,intpos2)
    ...{
    inttemp=list[pos1];
    list[pos1]
    =list[pos2];
    list[pos2]
    =temp;

    DataChanged(pos1);
    DataChanged(pos2);
    }


    protectedvoidFinished()
    ...{
    GreenDataChanged(
    -1);
    RedDataChanged(
    -1);
    }

    }

    }

  2. BubbleSort2
    usingSystem;

    namespaceCore
    ...{
    internalclassBubbleSort2:AbstractSort
    ...{
    publicoverridevoidSort(int[]list)
    ...{
    this.list=list;

    for(inti=0;i<list.Length-1;i++)
    for(intj=list.Length-1;j>i;j--)
    ...{
    if(stop)
    return;

    if(list[i]>list[j])
    ...{
    GreenDataChanged(i);
    RedDataChanged(j);
    swap(i,j);
    }

    }


    Finished();
    }

    }

    }


    /**//********************************************
    *注意!
    *
    *在使用循环嵌套时,内层循环一定是递减顺序,
    *如果写反,将导致排序效率降低。
    *
    *用户可尝试将上面代码中的循环嵌套改为:
    *
    *for(inti=0;i<list.Length-1;i++)
    *for(intj=i;j<list.Length;j++)
    *{
    *......
    *}
    *
    *观察排序性能收到的影响。
    *
    ******************************************
    */
  3. BubbleSort
    usingSystem;

    namespaceCore
    ...{
    internalclassBubbleSort:AbstractSort
    ...{
    publicoverridevoidSort(int[]list)
    ...{
    this.list=list;

    boolchange=true;
    for(inti=list.Length-1;i>0&&change;--i)
    ...{
    change
    =false;
    for(intj=0;j<i;++j)
    ...{
    if(stop)
    return;

    if(list[j]>list[j+1])
    ...{
    GreenDataChanged(j);
    RedDataChanged(j
    +1);
    swap(j,j
    +1);
    change
    =true;
    }

    }

    }


    Finished();
    }

    }

    }

  4. HeapSort
    usingSystem;

    namespaceCore
    ...{
    internalclassHeapSort:AbstractSort
    ...{
    publicoverridevoidSort(int[]list)
    ...{
    this.list=list;
    intlastOutOfOrder;

    buildHeap();

    for(lastOutOfOrder=list.Length-1;lastOutOfOrder>=0;
    lastOutOfOrder
    --)
    ...{
    if(stop)
    return;

    swap(
    0,lastOutOfOrder);
    heapify(
    0,lastOutOfOrder-1);
    }


    Finished();
    }


    privatevoidbuildHeap()
    ...{
    intindex;

    for(index=list.Length/2-1;index>=0;index--)
    ...{
    if(stop)
    return;

    heapify(index,list.Length
    -1);
    }

    }


    privatevoidheapify(intlow,inthigh)
    ...{
    intlargeIndex;

    inttemp=list[low];//copytherootnodeofthesubtree

    largeIndex
    =2*low+1;//indexoftheleftchild

    while(largeIndex<=high)
    ...{
    if(stop)
    return;

    if(largeIndex<high)
    if(list[largeIndex]<list[largeIndex+1])
    largeIndex
    =largeIndex+1;//indexofthe
    //largestchild

    if(temp>list[largeIndex])//subtreeis
    //alreadyinaheap
    break;
    else
    ...{
    list[low]
    =list[largeIndex];//movethelargerchild
    GreenDataChanged(low);

    //totheroot
    low=largeIndex;//gotothesubtreeto
    //restoretheheap
    largeIndex=2*low+1;
    }

    }


    list[low]
    =temp;//inserttempintothetree,thatis,list
    RedDataChanged(low);
    }

    }

    }

  5. InsertionSort
    usingSystem;

    namespaceCore
    ...{
    internalclassInsertionSort:AbstractSort
    ...{
    publicoverridevoidSort(int[]list)
    ...{
    this.list=list;

    intunsortedIndex,location;
    inttemp;

    for(unsortedIndex=1;unsortedIndex<list.Length;unsortedIndex++)
    border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-le
    分享到:
    评论

    相关推荐

Global site tag (gtag.js) - Google Analytics