javascript实现基本排序操作

来源:互联网 发布:尚学堂 java视频 编辑:程序博客网 时间:2024/05/29 09:37

当时在加入学校实验室时,我们导师(老杨)让我们一个星期时间使用javascript完成最基本,最常见的排序查找。

现在想来,那是一次快乐的过程。偷笑

我们常见的排序查找算法归类一共有一下几种,我会全部贴出他们的javascript源码!基本原理我相信学过数据结构的童鞋都知道。

(1)冒泡排序

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>冒泡排序</title></head><body><input type="text" id="2" style="font-size:20px; width:100%"><input type='button' value='排序' onclick='sort()'/> <div id="277" style="font-size:20px;border:1px solid;height:50px;"></div></body><script type="text/javascript">    function sort(){var scr=document.getElementById("2").value;var cee=scr.split(" ");var cee1=[],len=cee.length;for(var i=0;i<len;i++){cee1.push(parseInt(cee[i]));}var i;var j;var temp;for(i=0;i<len-1;i++){for(j=len-1;j>i;j--){//比较,找出本趟关键字最小的元素if(cee1[j]<cee1[j-1]){temp=cee1[j];//交换。前移;cee1[j]=cee1[j-1];cee1[j-1]=temp;}}}var rets = cee1.join(',');document.getElementById('277').innerHTML = rets;}    </script></html>

(2)选择排序

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>选择排序</title></head><body><input type="text" id="2" style="font-size:20px; width:100%">    <input type="button" value="排序" onclick="sort()"/>    <div id="27" style="font-size:20px;border:1px solid;height:50px;"></div></body><script type="text/javascript">    function sort(){var lq=document.getElementById("2").value;var cee=lq.split(" ");var cee1=[],len=cee.length;for(var i=0;i<len;i++){cee1.push(parseInt(cee[i]));}var i,j,k;var temp;for(i=0;i<len-1;i++){k=i;for(j=i+1;j<len;j++)if(cee1[j]<cee1[k])k=j;if(k!=i){temp=cee1[i];cee1[i]=cee1[k];cee1[k]=temp;}}var rets = cee1.join(',');document.getElementById('27').innerHTML = rets;}    </script></html>

(3)折半插入排序

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>折半插入排序</title></head><body><input type="text" style="width:150px;height:30px;" onblur="InsertSort(this)"/></body><script type="text/javascript">    function InsertSort(obj){var look = obj.value;var ceer = look.split(" ");var crte1 = [];len = ceer.length;for(var i=0;i<len;i++){crte1.push(parseInt(ceer[i]));}//alert(crte1);var i,j,low,high,mid;var temp;for(i=1;i<len;i++){temp=crte1[i];low=0;high=i-1;while(low<=high){if((low+high)%2!=0)mid=(low+high-1)/2;elsemid=(low+high)/2;if(temp<crte1[mid]){high = mid - 1;}else{low = mid + 1;}}for(j = i-1;j>=high+1;j--){crte1[j+1]=crte1[j];}//元素后移crte1[high+1] = temp;//插入}alert(crte1);/*var i,j,low,high,mid;//var temp;for(i=2;i<=len;++i){crte1[0]=crte1[i];low=1;high=i-1;while(low<=high){mid=(low+high)/2;if(crte1[0]<crte1[mid])high=mid-1;elselow=mid+1;}for(j=i-1;j>=high+1;--j){  crte1[j+1]=crte1[j];  //i--;  }  crte1[high+1]=crte1[0]; }*/   }   </script></html>

(4)折半查找

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title></head><body></body><script type="text/javascript">function search(arr,findval,left,right){var midlndex=Math.floor((left+right)/2);var midval=arr[midlndex];if(left>right){document.writeln("找不到");return;}if(midval>findval){search(arr,findval,left,midlndex-1);}else if(midval<findval){search(arr,findval,midlndex+1,right);}else{alert(midlndex);//document.writeln(midlndex);}}var arr=[1,3,12,24,44,54,67];search(arr,67,0,arr.length-1);    </script></html>

(5)二叉树的前序 中序 后序 遍历

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>二叉树的遍历</title><style type="text/css">     P{font-size:20px;font-family:Verdana, Geneva, sans-serif;}    </style></head><body></body><script type="text/javascript">    function BTNode(tree){this.tree=tree;this.left=null;this.rigth=null;}function show(tree){document.write(tree+" ");}function BuildTree(){var farther=new BTNode(1);farther.left=new BTNode(2);farther.right=new BTNode(3);farther.left.left=new BTNode(4);farther.left.right=new BTNode(5);farther.right.right=new BTNode(6);farther.left.left.right=new BTNode(7);farther.left.right.left=new BTNode(8);farther.right.right.left=new BTNode(9);farther.right.right.right=new BTNode(10);return farther;}function qianorder(farther){if(farther==null)return;show(farther.tree);qianorder(farther.left);qianorder(farther.right);}function zhongorder(farther){if(farther==null)return;zhongorder(farther.left);show(farther.tree);zhongorder(farther.right);}function houorder(farther){if(farther==null)return;houorder(farther.left);houorder(farther.right);show(farther.tree);}function DisLeaf(farther){if(farther==null)return;{if(farther.left==null&&farther.right==null)show(farther.tree);DisLeaf(farther.left);DisLeaf(farther.right);}}function HighTree(farther){var Lh,Rh;if(farther==null)return 0;else{Lh=HighTree(farther.left);Rh=HighTree(farther.right);return (Lh>Rh)?(Lh+1):(Rh+1);}}var count=1;function Treekudu(farther){if(farther!=null){if(farther.left!=null&&farther.right!=null){count++;}Treekudu(farther.left);Treekudu(farther.right);}}function KSTree(farther){if(farther!=null){if(farther.right==null){count++}if(farther.left==null){count++}KSTree(farther.left);KSTree(farther.reght);}}function DisBTNode(farther){if(farther!=null){show(farther.tree);if(farther.left!=null||farther.right!=null){document.write("(");DisBTNode(farther.right);if(farther.right!=null)document.write(",");DisBTNode(farther.right);document.write(")");}}}function countSearch(farther){if(farther!=null){if(farther.right!=null){count++;}countSearch(farther.left);countSearch(farther.right);}}var farther=BuildTree();document.write("<p>前序遍历:")qianorder(farther);document.write("</p><p>中序遍历:")zhongorder(farther);document.write("</p><p>后序遍历:")houorder(farther);document.write("</p><p>所有叶子节点:")DisLeaf(farther);document.write("</p><p>二叉树的深度:")show(HighTree(farther));document.write("</p><p>二叉树的输出:")//有问题,,不知道咋改DisBTNode(farther);document.write("</p><p>二叉树宽度:")Treekudu(farther);document.write(count);document.write("</p><p>二叉树中空链域个数:")KSTree(farther);document.write(count-1);document.write("</p><p>输出利用二叉树存储的普通树的度:");//function T(farther){//if(farther.right!=null)//{//document.write(error);//}//else{countSearch(farther);document.write(count+"这个有问题");//}//}document.write("</p>");         </script></html>
(6)链表操作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>链表操作</title>    <style type="text/css">    p{font-size:20px;font-family:Verdana, Geneva, sans-serif;}    </style>    <script type = "text/javascript">          var array = new Array(2,7,5,2,1,3,4);          var Node = function(newData){              this.next = null;              this.data = null;              this.Init = function(){                  this.data = newData;              };              this.Init();          }                    var List = function(){              this.head = null;              this.size = 0;              this.Init = function(){                  this.head = null;                  this.size = 0;              }              this.Init();                            this.Insert = function(newData){                  this.size += 1;                  var newNode = new Node(newData);                  if(this.head == null){                      this.head = newNode;                      return;                  }                  var tempNode = this.head;                  while(tempNode.next != null)                      tempNode = tempNode.next;                  tempNode.next = newNode;              };                            this.GetData = function(pos){                  if(pos >= this.size || pos < 0)                      return null;                    else{                      tempNode = this.head;                      for(i = 0;i < pos;i++)                            tempNode = tempNode.next;                        return tempNode.data;                     }              };                            //删除元素            this.Remove = function(pos){                  if(pos >= this.size || pos < 0)                      return null;                      this.size -= 1;                  tempNode = this.head;                  if(pos == 0){                      this.head = this.head.next;                      return this.head;                  }                  for(i = 0;i < pos - 1;i++){                      tempNode = tempNode.next;                  }                  tempNode.next = tempNode.next.next;                  return tempNode.next;                                    }                            this.Print = function(){                  document.write("<p>链表元素如下:");                  tempNode = this.head;                  while(tempNode != null){                      document.write(tempNode.data + " ");                      tempNode = tempNode.next;                  }                  document.write("<br>");              };          };                    //运行链表         var list = new List();          var array = new Array(2,7,5,2,1,3,4);          for(i = 0;i < array.length;i++){              list.Insert(array[i]);          }          list.Print();          list.Remove(5);          list.Print();          document.write("</p><p>删除的位置:  " + list.size);          document.write("</p>");    </script>  </head>    <body>  </body>  </html>  
(7)希尔排序

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>希尔排序</title></head><body><input type="text" style="width:30%;height:30px;" onblur="get(this)"/></body><script type="text/javascript">    function get(obj){var ct = obj.value;var Text=ct.split(" ");var arr=[];len=Text.length;for(var i=0;i<len;i++){arr.push(parseInt(Text[i]));}for(var F=Math.floor(len/2);F>0;F=Math.floor(F/2)){for(var i=F;i<len;i++){for(var j=i-F;j>=0&&arr[j]>arr[F+j];j-=F){var temp=arr[j];arr[j]=arr[F+j];arr[F+j]=temp;}}}alert(arr);}        </script></html>

微笑微笑微笑

原创粉丝点击