星期二, 10月 10, 2006

[.Net]雞尾酒排序

早上翻譯了這篇:雞尾酒排序,翻譯的不是很好~以後再求改進吧。
順手再寫了 c# 的版本。

public static void cocktailSort( int[] theList )
{
    int bottom = 0;
    int top = theList.Length-1;
    bool swapped = true;
    
    while( swapped == true )
    {
        swapped = false;
        for( int i=bottom; i<top; i++ )
        {
            if( theList[i] > theList[i+1] )
            {
                int tmp = theList[i+1];
                theList[i+1]=theList[i];
                theList[i]=tmp;
                swapped = true;
            }
        }
            
        top = top-1;
        for( int i=top; i>bottom; i-- )
        {
            if( theList[i]<theList[i-1] )
            {
                int tmp = theList[i-1];
                theList[i-1]=theList[i];
                theList[i]=tmp;
                swapped = true;
            }
        }
            
        bottom = bottom+1;
    }
}

沒有留言:

您或許對這些文章有興趣

Related Posts Plugin for WordPress, Blogger...