private int[] iterate()
        {
            if (p == null)
            {
                initializePermutation();
                updatePermutation();
                return(p);
            }//There is no permutation yet

            //Previous permutation exists
            for (int i = indexArray.Length - 1; i > 0; i--)
            {
                if (inOrder(indexArray[i - 1], indexArray[i]))
                {
                    int tearingNumberIndex = i - 1;

                    //Reorder the tail of permutation
                    int startingIndex = tearingNumberIndex + 1;
                    int sortingLength = indexArray.Length - tearingNumberIndex - 1;
                    Array.Sort(indexArray, startingIndex, sortingLength);

                    //Search for the number to swap with tearing number
                    for (int j = tearingNumberIndex; j < indexArray.Length; j++)
                    {
                        //The number should be greater than tearing
                        if (inOrder(indexArray[tearingNumberIndex], indexArray[j]))
                        {
                            swapValues(tearingNumberIndex, j, indexArray);
                            break;
                        }
                    }
                    updatePermutation();
                    return(p);
                }
            }
            int inversionIndex = nextInversionIndex();

            if (inversions == 2 << (permutationLength / 2 - 1))
            {
                return(null);    //There is no suitable permutations left
            }
            pairs1.SwapValuesAtIndex(inversionIndex);
            resetIndexArray();
            updatePermutation();
            return(p);
        }