当前位置:首页 » 编程语言 » 查找算法java

查找算法java

发布时间: 2023-01-03 22:15:59

❶ 请教:用java编一个基本查找算法效率比较的程序。

<script>
Array.prototype.swap = function(i, j)
{
var temp = this[i];
this[i] = this[j];
this[j] = temp;
}

Array.prototype.bubbleSort = function()
{
for (var i = this.length - 1; i > 0; --i)
{
for (var j = 0; j < i; ++j)
{
if (this[j] > this[j + 1]) this.swap(j, j + 1);
}
}
}

Array.prototype.selectionSort = function()
{
for (var i = 0; i < this.length; ++i)
{
var index = i;
for (var j = i + 1; j < this.length; ++j)
{
if (this[j] < this[index]) index = j;
}
this.swap(i, index);
}
}

Array.prototype.insertionSort = function()
{
for (var i = 1; i < this.length; ++i)
{
var j = i, value = this[i];
while (j > 0 && this[j - 1] > value)
{
this[j] = this[j - 1];
--j;
}
this[j] = value;
}
}

Array.prototype.shellSort = function()
{
for (var step = this.length >> 1; step > 0; step >>= 1)
{
for (var i = 0; i < step; ++i)
{
for (var j = i + step; j < this.length; j += step)
{
var k = j, value = this[j];
while (k >= step && this[k - step] > value)
{
this[k] = this[k - step];
k -= step;
}
this[k] = value;
}
}
}
}

Array.prototype.quickSort = function(s, e)
{
if (s == null) s = 0;
if (e == null) e = this.length - 1;
if (s >= e) return;
this.swap((s + e) >> 1, e);
var index = s - 1;
for (var i = s; i <= e; ++i)
{
if (this[i] <= this[e]) this.swap(i, ++index);
}
this.quickSort(s, index - 1);
this.quickSort(index + 1, e);
}

Array.prototype.stackQuickSort = function()
{
var stack = [0, this.length - 1];
while (stack.length > 0)
{
var e = stack.pop(), s = stack.pop();
if (s >= e) continue;
this.swap((s + e) >> 1, e);
var index = s - 1;
for (var i = s; i <= e; ++i)
{
if (this[i] <= this[e]) this.swap(i, ++index);
}
stack.push(s, index - 1, index + 1, e);
}
}

Array.prototype.mergeSort = function(s, e, b)
{
if (s == null) s = 0;
if (e == null) e = this.length - 1;
if (b == null) b = new Array(this.length);
if (s >= e) return;
var m = (s + e) >> 1;
this.mergeSort(s, m, b);
this.mergeSort(m + 1, e, b);
for (var i = s, j = s, k = m + 1; i <= e; ++i)
{
b[i] = this[(k > e || j <= m && this[j] < this[k]) ? j++ : k++];
}
for (var i = s; i <= e; ++i) this[i] = b[i];
}

Array.prototype.heapSort = function()
{
for (var i = 1; i < this.length; ++i)
{
for (var j = i, k = (j - 1) >> 1; k >= 0; j = k, k = (k - 1) >> 1)
{
if (this[k] >= this[j]) break;
this.swap(j, k);
}
}
for (var i = this.length - 1; i > 0; --i)
{
this.swap(0, i);
for (var j = 0, k = (j + 1) << 1; k <= i; j = k, k = (k + 1) << 1)
{
if (k == i || this[k] < this[k - 1]) --k;
if (this[k] <= this[j]) break;
this.swap(j, k);
}
}
}

function generate()
{
var max = parseInt(txtMax.value), count = parseInt(txtCount.value);
if (isNaN(max) || isNaN(count))
{
alert("个数和最大值必须是一个整数");
return;
}
var array = [];
for (var i = 0; i < count; ++i) array.push(Math.round(Math.random() * max));
txtInput.value = array.join("\n");
txtOutput.value = "";
}

function demo(type)
{
var array = txtInput.value == "" ? [] : txtInput.value.replace().split("\n");
for (var i = 0; i < array.length; ++i) array[i] = parseInt(array[i]);
var t1 = new Date();
eval("array." + type + "Sort()");
var t2 = new Date();
lblTime.innerText = t2.valueOf() - t1.valueOf();
txtOutput.value = array.join("\n");
}
</script>

<body onload=generate()>
<table style="width:100%;height:100%;font-size:12px;font-family:宋体">
<tr>
<td align=right>
<textarea id=txtInput readonly style="width:100px;height:100%"></textarea>
</td>
<td width=150 align=center>
随机数个数<input id=txtCount value=500 style="width:50px"><br><br>
最大随机数<input id=txtMax value=1000 style="width:50px"><br><br>
<button onclick=generate()>重新生成</button><br><br><br><br>
耗时(毫秒):<label id=lblTime></label><br><br><br><br>
<button onclick=demo("bubble")>冒泡排序</button><br><br>
<button onclick=demo("selection")>选择排序</button><br><br>
<button onclick=demo("insertion")>插入排序</button><br><br>
<button onclick=demo("shell")>谢尔排序</button><br><br>
<button onclick=demo("quick")>快速排序(递归)</button><br><br>
<button onclick=demo("stackQuick")>快速排序(堆栈)</button><br><br>
<button onclick=demo("merge")>归并排序</button><br><br>
<button onclick=demo("heap")>堆排序</button><br><br>
</td>
<td align=left>
<textarea id=txtOutput readonly style="width:100px;height:100%"></textarea>
</td>
</tr>
</table>
</body>

这个代码是放在DREAMWEAVER <head></head>标签里面

❷ java学习的主要内容是什么

互联网行业目前还是最热门的行业之一,许多想入行互联网的人都会选择Java学习,发展前景非常好,普通人也可以学习。

如果想达到工作标准可以参考下面的内容:

1.Java SE部分 初级语法,面向对象,异常,IO流,多线程,Java Swing,JDBC,泛型,注解,反射等。

2.数据库部分,基础的sql语句,sql语句调优,索引,数据库引擎,存储过程,触发器,事务等。

3. 前端部分, HTML5 CSS3 JS, HTML DOM Jquery BootStrap等。

4. Java EE部分,Tomcat和Nginx服务器搭建,配置文件,Servlet,JSP,Filter,Listener,http协议,MVC等。

5. 框架部分,每个框架都可以分开学,在去学如何使用SSM 或者SSH框架,如何搭建,如何整合。开发中为什么会用框架,Rest是啥?Spring为啥经久不衰,底层如何实现等。

6.23种设计模式,掌握常用的,比如单例模式的多种实现,责任链模式,工厂模式,装饰器模式等,了解常用场景。

7. 基础算法和数据结构,八大排序算法,查找算法。

8. 熟练使用maven等构建工具,git等版本控制工具,熟悉常用linux命令,log4j,bug,junit单元测试,日志打印工具,Redis等NoSql。

想要系统学习,你可以考察对比一下开设有相关专业的热门学校,好的学校拥有根据当下企业需求自主研发课程的能力,能够在校期间取得大专或本科学历,中博软件学院、南京课工场、南京北大青鸟等开设相关专业的学校都是不错的,建议实地考察对比一下。

祝你学有所成,望采纳。

❸ java关键字查询算法

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;

public class search
{
//查找方法,参数,文件绝对路径,查找关键字
public static boolean search(String filepath,String key)
{
try
{
File f = new File(filepath);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = "";
//int i = 1;
while((s = br.readLine()) != null)
{
if(s.indexOf(key) != -1)
{
return true;
}
}
return false;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
public static void main(String args[])
{
System.out.println(search.search("d://t.txt","l2"));
}
}

修改了下,加两个变量,可以指出查找的位置。
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;

public class search
{
//查找方法,参数,文件绝对路径,查找关键字
public static String search(String filepath,String key)
{
try
{
File f = new File(filepath);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s = "";
int i = 1;
int m = 0;
while((s = br.readLine()) != null)
{
if((m = s.indexOf(key)) != -1)
{
return "第"+i+"段,第"+m+"处";
}
i++;
}
return null;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public static void main(String args[])
{
System.out.println(search.search("d://t.txt","asd"));
}
}

这个,查汉字是没有问题的。
另外,你要全文检索的话,indexOf()还有个方法,indexOf(int start,String key),指定开始查找的位置跟关键字,你查到一处后,将这个数值加1,做为继续查找的开始位置就可以了。

❹ 用JAVA编写一个简单的 查询算法!谢谢大哥大姐了

import java.io.*;
public class Test
{

/**
* @param args
*/
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
System.out.println("请输入要查询的单词");
String s=br.readLine();

int count=0;
int m=0;

int begin=-1;
int end=-1;
while(true)
{
if(s.length()==1)
{
begin=str.indexOf(s);
if(m==0)
{
System.out.println("第一次出现在"+begin+"字节处");
}
m++;
end=begin;
}
else
{
begin=str.indexOf(s.substring(0,1));
if(m==0)
{
System.out.println("第一次出现在"+begin+"字节处");
}
end=str.indexOf(s.substring(s.length()-1));
}
if(begin==-1||end==-1)
{
break;
}

if(s.equals(str.subSequence(begin, end+1)))
{
count++;
str=str.substring(end+1);
}
else
{
str=str.substring(end+1);
}
}

System.out.println("单词"+s+"出现了"+count+"次");
}
}

❺ java关键字查询算法

就用readline方法,
一行一行地查吧,
楼上的算法还是可行的,
要指出在哪一行,
直接在readline中设置一个变量就可以了,
扫过一行自增就行
另外个人认为,
还要考虑这样一种情况,
比如说关键字为
java

ja
va
被分开在两行中了,
这个怎么算呢?

热点内容
快速计算法怎么算 发布:2025-07-05 07:08:12 浏览:138
php判断数组长度 发布:2025-07-05 07:07:26 浏览:145
苹果电脑取消共享文件夹 发布:2025-07-05 07:06:00 浏览:347
机器学习算法应用 发布:2025-07-05 07:01:17 浏览:33
万能解压缩王 发布:2025-07-05 06:51:56 浏览:542
手机怎么修改wifi密码名称 发布:2025-07-05 06:46:13 浏览:381
阿里云服务器bt安装 发布:2025-07-05 06:36:46 浏览:370
数据库组别 发布:2025-07-05 06:15:53 浏览:711
我的世界服务器怎样设置新手装备只能拿一次 发布:2025-07-05 06:15:53 浏览:985
缓存40集电视剧需要多少流量 发布:2025-07-05 05:56:44 浏览:64