第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

生成集合的排列(最有效)

生成集合的排列(最有效)

瀟瀟雨雨 2019-09-20 15:14:52
我想生成一個集合(集合)的所有排列,如下所示:Collection: 1, 2, 3Permutations: {1, 2, 3}              {1, 3, 2}              {2, 1, 3}              {2, 3, 1}              {3, 1, 2}              {3, 2, 1}一般而言,這不是“如何”的問題,而是關(guān)于如何最有效的問題。此外,我不想生成所有排列并返回它們,但一次只生成一個排列,并且只在必要時繼續(xù)(很像迭代器 - 我也嘗試過,但結(jié)果卻少了有效)。我已經(jīng)測試了很多算法和方法,并提出了這個代碼,這是我嘗試過的最有效的代碼:public static bool NextPermutation<T>(T[] elements) where T : IComparable<T>{    // More efficient to have a variable instead of accessing a property    var count = elements.Length;    // Indicates whether this is the last lexicographic permutation    var done = true;    // Go through the array from last to first    for (var i = count - 1; i > 0; i--)    {        var curr = elements[i];        // Check if the current element is less than the one before it        if (curr.CompareTo(elements[i - 1]) < 0)        {            continue;        }        // An element bigger than the one before it has been found,        // so this isn't the last lexicographic permutation.        done = false;        // Save the previous (bigger) element in a variable for more efficiency.        var prev = elements[i - 1];            {                curr = tmp;                currIndex = j;            }        }}它的用法是發(fā)送一個元素數(shù)組,然后返回一個布爾值,指示這是否是最后一個詞典排列,以及將數(shù)組改為下一個排列。用法示例:var arr = new[] {1, 2, 3};PrintArray(arr);while (!NextPermutation(arr)){    PrintArray(arr);}問題是我對代碼的速度感到不滿意。迭代大小為11的數(shù)組的所有排列大約需要4秒。雖然它可以被認為是令人印象深刻的,因為一組11號的可能排列量11!接近4000萬。邏輯上,對于大小為12的數(shù)組,它將花費大約12倍的時間,因為12!是11! * 12,并且對于大小為13的數(shù)組,它將花費大約13倍于12大小的時間,依此類推。所以你可以很容易地理解如何使用12或更大的數(shù)組,它需要很長時間才能完成所有排列。而且我有一種強烈的預感,我可以以某種方式減少那么多的時間(沒有切換到C#以外的語言 - 因為編譯器優(yōu)化確實非常好地優(yōu)化,我懷疑我可以在Assembly中手動優(yōu)化)。有沒有人知道以其他方式更快地完成這項工作?您是否知道如何使當前算法更快?請注意,我不想使用外部庫或服務來實現(xiàn)這一點 - 我希望擁有代碼本身,并希望它盡可能高效。
查看完整描述

3 回答

?
人到中年有點甜

TA貢獻1895條經(jīng)驗 獲得超7個贊

這可能就是你要找的東西。


    private static bool NextPermutation(int[] numList)

    {

        /*

         Knuths

         1. Find the largest index j such that a[j] < a[j + 1]. If no such index exists, the permutation is the last permutation.

         2. Find the largest index l such that a[j] < a[l]. Since j + 1 is such an index, l is well defined and satisfies j < l.

         3. Swap a[j] with a[l].

         4. Reverse the sequence from a[j + 1] up to and including the final element a[n].


         */

        var largestIndex = -1;

        for (var i = numList.Length - 2; i >= 0; i--)

        {

            if (numList[i] < numList[i + 1]) {

                largestIndex = i;

                break;

            }

        }


        if (largestIndex < 0) return false;


        var largestIndex2 = -1;

        for (var i = numList.Length - 1 ; i >= 0; i--) {

            if (numList[largestIndex] < numList[i]) {

                largestIndex2 = i;

                break;

            }

        }


        var tmp = numList[largestIndex];

        numList[largestIndex] = numList[largestIndex2];

        numList[largestIndex2] = tmp;


        for (int i = largestIndex + 1, j = numList.Length - 1; i < j; i++, j--) {

            tmp = numList[i];

            numList[i] = numList[j];

            numList[j] = tmp;

        }


        return true;

    }


查看完整回答
反對 回復 2019-09-20
  • 3 回答
  • 0 關(guān)注
  • 811 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號