惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - kary

VS 2005 Web Deployment Projects GridView绑定数据源,无记录时表头也显示的一种解决方法 Web Deployment Projects with Visual Studio 2005 asp.net2.0如何加密数据库联接字符串 - kary - 博客园 随页面移动的浮动广告代码 - kary - 博客园 MSDN转:自定义实体类简介 aspnet2.0 Menu控件上传到服务器小问题 ASP.NET2.0的快速入门站点 AJAX开发简略 - kary 用人之道(二)-- 何管理软件开发团队 转:用人之道(一)-- 如何组建软件开发队伍 袁峰写的开发之路 雅虎公司C#笔试题 转:产品开发模式管理网站开发 Windows IIS 6安全保护贴—URL授权全攻略! Access库的小问题 要做互联星空的SP接口,一点头绪都没有 windows server 2003 出错提示"请求的资源在使用中"解决方案 针对SQL INJECTION的SQL SERVER安全设置初级篇
快速排序与二分查找算法Javascript版本 - kary - 博客园
kary · 2006-05-15 · via 博客园 - kary

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> (快速排序与二分查找)</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="dolphin">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<div id="sourceNum"></div>
<div id="sortNum"></div>
<div id="findProcess"></div>
<INPUT TYPE="text" NAME="FindNum" id="FindNum" size="10"><button onclick='javascript: if (/^\d+$/.test(document.getElementById("FindNum").value)){ a.HalfFind(a.sortArray,document.getElementById("FindNum").value)}else{alert("请输入正整数!");document.getElementById("FindNum").value="";document.getElementById("FindNum").focus()}'>Find</button>
<SCRIPT LANGUAGE="JavaScript">
<!--
function Half(sAry)
{
  this.sortArray = sAry;
  this.count = 0;

  this.swap = function(i,j)
  {
    var tmp = this.sortArray[i];this.sortArray[i] = this.sortArray[j];this.sortArray[j] = tmp;
    this.count++;
  }
  this.partition = function(low,high)
  {
    var pivot,pos,tmp,i,j;
    pos = low;
    pivot = this.sortArray[pos];
    for(i=low+1;i<=high;i++)
    {
      if (this.sortArray[i]<pivot)
      {
        pos++;
        this.swap(pos,i);
      }
    }
    this.swap(low,pos);
    return pos;
  }
  this.QuickSort = function(low,high)
  {
    var pivot;
    if (low<high)
    {
      pivot = this.partition(low,high);
      this.QuickSort(low,pivot-1);
      this.QuickSort(pivot+1,high);
    }
  }
  this.HalfFind = function(sArray,fNum)
  {
    var low,high,i,t;
    low = 0;
    i = 1;
    high = sArray.length-1;
    t = Math.floor((high+low) / 2);
    document.getElementById("findProcess").innerHTML="";
    while ((sArray[t]!=fNum) && (sArray[low]!=fNum) && (sArray[high]!=fNum) && (low < high))
    {
      document.getElementById("findProcess").innerHTML+="第"+i+"次查找 -> "+t+"的位置:"+sArray[t]+"&nbsp;&nbsp;&nbsp;&nbsp;low="+low+"&nbsp;high="+high+"<br>";

            if (fNum>sArray[t])
      {
        low = t+1;
        t = Math.floor((high+low) / 2);
      }
      else
      {
        high = t-1;
        t = Math.floor((high+low) / 2);
      }
      i++
    }
    if ((sArray[t]!=fNum) && (sArray[low]!=fNum) && (sArray[high]!=fNum) && low >= high)
    {
      document.getElementById("findProcess").innerHTML+="第"+i+"次查找 -> "+t+"的位置:失败 此队列没有"+fNum+"该数&nbsp;&nbsp;&nbsp;&nbsp;low="+low+"&nbsp;high="+high+"<br>";
    }
    else
    {
      if(sArray[t]==fNum)
        tt = t;
      else if(sArray[low]==fNum)
        tt = low;
      else
        tt = high;

            document.getElementById("findProcess").innerHTML+="第"+i+"次查找 -> "+tt+"的位置:"+sArray[tt]+" 找到了!&nbsp;&nbsp;&nbsp;&nbsp;low="+low+"&nbsp;high="+high+"<br>";
    }
    return t;
  }
}
var randLen = Math.floor(Math.random()*100);
var stAry = new Array(randLen);
var i,low,high;
for(i=0;i<randLen;i++)
  stAry[i] = Math.floor(Math.random()*100);
var a = new Half(stAry);
low = 0;
high = stAry.length-1;
document.getElementById("sourceNum").innerHTML="排序前:"+a.sortArray+"&nbsp;&nbsp;排序个数:"+(high+1);
a.QuickSort(low,high);
document.getElementById("sortNum").innerHTML="排序后:"+a.sortArray+"&nbsp;&nbsp;排序次数:"+a.count;
//-->
</SCRIPT>
</BODY>
</HTML>