2

Here is my code :

<TITLE>Permutation Generator</TITLE>
<link rel="stylesheet" type="text/css" href="standard.css">
<SCRIPT language=JavaScript SRC="feedback.js"></SCRIPT>

<script language=JavaScript>
function count()
{
    var number = parseInt(calc.number.value, 10);
    var select = parseInt(calc.select.value, 10);

    // check input
    if (isNaN(number)||(number <= 0)||isNaN(select)||(select <= 0)||(select > number))
    {
        calc.permutations.value = "Error";
        return;
    }

    var c = Math.round(permutations(number, select));
    calc.permutations.value = c;
}

// list permutations in a separate window
function listing()
{
    var number = parseInt(calc.number.value);
    var select = parseInt(calc.select.value);

    // check input
    if (isNaN(number)||(number <= 0)||isNaN(select)||(select <= 0)||(select > number))
    {
        calc.permutations.value = "Error";
        return;
    }

    var c = Math.round(permutations(number, select));
    calc.permutations.value = c;

    msgWindow = window.open("","msgWindow","toolbar=no,status=no,menubar=yes,scrollbars=yes,width=550,height=400");
    msgWindow.document.open();
    msgWindow.document.writeln("<html><head><title>Listing</title></head>");
    msgWindow.document.writeln("<body>");
    msgWindow.document.writeln("<p><tt><sub>" + number + "</sub>P<sub>" + select + "</sub> = " + c + "</tt>");
    msgWindow.document.writeln("<p>listing of permutations");
    msgWindow.document.writeln("<p>");
    msgWindow.document.writeln("<pre>");

    value = new Array()

    var a = new Array();                           // initialize
    for (i = 0; i < select; i++) 
        a[i] = i + 1;                              // 1 - 2 - 3 - 4 - ...

    while (true)
    {       
        for (n = 0; n < select; n++) 
            value[n] = a[n]; 

        // for this combination, list permutations in lexicographical order
        put_next(select);
        while (get_next(select))
            put_next(select);

        // generate next combination in lexicographical order
        i = select - 1;                            // start at last item        
        while (a[i] == (number - select + i + 1))  // find next item to increment 
            --i;

        if (i < 0) break;                          // all done
        ++a[i];                                    // increment

        for (j = i + 1; j < select; j++)           // do next combination 
            a[j] = a[i] + j - i;
    }

    msgWindow.document.writeln("</pre>");
    msgWindow.document.writeln("<p>All done!");
    msgWindow.document.writeln("</body></html>");

    msgWindow.document.close();
}

// compute the number of permutations of r objects from a set on n objects
function permutations( n, r )
{
    var c = parseInt("1");
    if (r > n) return 0;
    var d = n - r;

    while (n > 0)
    {
        c = c * n;
        --n; 
    }

    while (d)
    {
        c = c / d;
        --d;
    }

    return c;
}

var value;

// compute the next permutation given the current permutation
function get_next( k )
{
    var i = k - 1;
    while (value[i-1] >= value[i]) 
        i--;

    if (i < 1) return false;                       // all in reverse order 

    var j = k;
    while (value[j-1] <= value[i-1]) 
        j--;

    swap(i - 1, j - 1);

    i++; 
    j = k;

    while (i < j)
    {
        swap(i - 1, j - 1);
        i++;
        j--;
    }

    return true;
}

function swap( a, b )
{
    temp     = value[a];
    value[a] = value[b];
    value[b] = temp;
}

function put_next( k )
{
    for (i = 0; i < k; i++)
        msgWindow.document.write("  " + value[i]);
    msgWindow.document.writeln("");
}

// unhide -->
</SCRIPT>
<style type="text/css">
<!--
.style2 {color: #FFFFFF}
-->
</style>
</head>
<body bgcolor="white" text="black">
<p><strong>PERMUTATION LISTING GENERATER </strong></p>
<p>Make sure the device has enough <i>Primary Memory</i> to allocate. It can hang the system while running full function, not an infinite process. </p>
<p>&nbsp;</p>
<LEFT>
<FORM name=calc>
<table border=1 bordercolor=darkblue cellspacing=0>
<tr><td>
 <table>
 <tr><td><b>Number<span class="style2"> - n</span> &nbsp;&nbsp;</b>
 <td><INPUT maxLength=20 name=number> 
 <tr><td><b>Select<span class="style2"> - k &nbsp;&nbsp;</span></b>
   <td><input maxlength=20 name=select>
    <tr><td><b>Permutations &nbsp;&nbsp;</b>
 <td><INPUT maxLength=20 readonly name=permutations>
 </table>
</tr>
</table>
</FORM>
<INPUT onclick=listing(); type=button value=Listing>
</LEFT>
</html>

Here the permutation list is not following any order. Coming like this way :

5P3 = 60

listing of permutations

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

1 2 4

1 4 2

etc..I need these numbers in ascending order. Please help me.

  • 1
    Where are you getting stuck? – T.J. Crowder Dec 29 '13 at 08:34
  • 1
    Why not store it inside an array, instead of displaying it immediately, then call a sort() function and then display it? – Noam Rathaus Dec 29 '13 at 08:36
  • Just sort them(by designing your own code(for bubble sort, or something), or using a library, like [underscore](http://underscorejs.org/)) after all the permutations have been generated. – tewathia Dec 29 '13 at 08:36
  • You should do what I suggested in the last comment, change your code so that the permutations are stored in an array, sort that array and print it. But here's a quick fix. Put this `msgWindow.document.querySelector('pre').innerText = _.sortBy(msgWindow.document.querySelector('pre').innerText.split(' ').join('').split('\n'), function(num){ return num; }).join('\n').split('').join(' ');` after the `msgWindow.document.close();` line(and import the [underscore library](http://underscorejs.org/underscore-min.js), obviously) – tewathia Dec 29 '13 at 09:40
  • **Already solved: javascript solution:** http://stackoverflow.com/a/6800054/684229 – Tomas Dec 29 '13 at 12:22

0 Answers0