Examples of JavaScript ordering are the following code:
- <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
- <title>www.cxybl.com</title>
- <style type= "Text/css" >
- *{font-family:arial, Helvetica, Sans-serif}
- . div1{
- Background-color: #35BDDB;
- Float:left;
- margin-left:1px;
- Color: #fff;
- Vertical-align:middle;
- font-size:12px;
- }
- . sorted{
- Background-color: #CFD0D2}
- #content {width:100%; height:601px; Background-color: #222222; text-align:center;}
- </style>
- <script type= "Text/javascript" src= "Jquery-1.4.2.js" ></script>
- <script type= "Text/javascript" >
- var numbs = [];
- var numbers = 40;
- var divwidth = Math.floor (1000/numbers);
- var ti = 1;
- var step = 0;
- var step1 = 0;
- function init () {
- Step1 = 0;
- Numbers = $ ("#numb"). Val ();
- if (isNaN (numbers) number (numbers) <= 0) {
- Numbers = 40;
- $ ("#numb"). Val (40);
- }
- if (number (numbers) > 500) {
- Numbers = 500;
- $ ("#numb"). Val (500);
- }
- Numbs = [];
- Step = 0;
- TI = 1;
- $ ("#content"). HTML ("");
- while (Numbs.length < numbers) {
- var t = Math.floor (600*math.random ());
- if (Numbs.indexof (T) < 0) {
- Numbs.push (t);
- }
- }
- $ ("#step"). HTML ("");
- $.each (numbs, function (i,n) {
- $ ("<div>"). AddClass ("Div1"). CSS ({width: (100/numbers-0.1) + "%", Height:n + "px", "margin-top": (600-n) + "px"})
- . attr ("id", N). Appendto ($ ("#content"));
- });
- }
- $ (function () {
- Init ();
- $ ("#random"). Click (init);
- $ ("#bubbleSort"). Click (function () {
- $ (": Input"). attr ("Disabled", "disabled");
- if (step1!= 0) {
- Init ();
- }
- Bubblesort ();
- });
- $ ("#QuickSort"). Click (function () {
- $ (": Input"). attr ("Disabled", "disabled");
- if (step1!= 0) {
- Init ();
- }
- QuickSort (numbs, 0, numbs.length);
- });
- });
- function Bubblesort () {
- for (i = 0; i< numbs.length;i++) {
- for (j = i + 1;j<numbs.length;j++) {
- if (Numbs[i] > Numbs[j]) {
- Swap (Numbs, I, J);
- }
- }
- }
- }
- function swap (arrays, I, j) {
- var t = arrays[i];
- var m = arrays[j];
- Arrays[i] = Arrays[j];
- ARRAYS[J] = t;
- step1++;
- settimeout ("Swapdiv" (' # ' + "+t+"), $ (' # ' + "+m+")) ", (ti++) *50);
- }
- function Swapdiv (DIV1, Div2) {
- $ (". Div1"). Removeclass ("sorted");
- var Tempdiv = $ ("<div>");
- Div1.after (TEMPDIV);
- Div2.after (DIV1);
- Tempdiv.after (DIV2);
- Tempdiv.remove ();
- Div1.addclass ("sorted");
- Div2.addclass ("sorted");
- $ ("#step"). HTML (++step);
- if (step = = Step1) {
- $ (": Input"). attr ("Disabled", "");
- $ (". Div1"). Removeclass ("sorted");
- }
- }
- function QuickSort (PData, left, right) {
- var i,j;
- var middle,temp;
- i = left;
- j = right;
- middle = pdata[left];
- while (true) {
- while ((++i) <right-1 && pdata[i]<middle);
- while ((--j) >left && pdata[j]>middle);
- if (I>=J)
- Break
- Swap (PData, I, J);
- }
- Swap (PData, left, J);
- if (LEFT<J)
- QuickSort (PDATA,LEFT,J);
- if (right>i)
- QuickSort (Pdata,i,right);
- }
- </script>
- <body>
- <div id= "num" ></div>
- <button id= "Bubblesort" >bubbleSort</button>
- <button id= "QuickSort" >QuickSort</button>
- <input id= "Numb" value= "/>"
- <button id= "Random" >init</button>
- Step:<span id= "Step" ></span>
- <div id= "Content" ></div>
- </body>