
正文
常用查找算法java代码 常用查找算法java代码是什么
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java泛型 二分查找
以下代码是关于对象常用查找算法java代码的 二分查找 的例子常用查找算法java代码,已经测试通过常用查找算法java代码,执行即可。
Student 是基本比较对象类
Dichotomy 是二分法执行类
Test 是测试类
package com.dichotomy;
public class Student implements ComparableStudent {
private int id;
private String name;
private String idCard;
private String sex;
private String mobile;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
/**
* 排序控制
* @param o1 Student
* @param o2 Student
* @return int 返回 -1 向前移动, 1 向后移动, 0 不移动
* 这个方法需要自己进行调整常用查找算法java代码,排序比较和二分查找时均使用此方法进行位置调整
* 比较时使用的key自己可以进行修改常用查找算法java代码,不过要保证唯一性,否则查询出来的值会不准确
*/
public int compareTo(Student o) {
//不同的执行次序决定排序和查找次序不同,可以同下面的调换一下
if(this.getId() o.getId()){
return -1;
} else if(this.getId() == o.getId()){
;
} else {
return 1;
}
//不同的执行次序决定排序和查找次序不同
int c = this.getIdCard().compareTo(o.getIdCard());
if(c != 0){
return c;
}
//不同的执行次序决定排序和查找次序不同
int n = this.getName().compareTo(o.getName());
if(n != 0){
return n;
}
return 0;
}
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append(this.getId()).append("\t");
sb.append(this.getName()).append("\t");
sb.append(this.getIdCard()).append("\t");
sb.append(this.getMobile()).append("\t");
sb.append(this.getSex());
return sb.toString();
}
}
相关问答
Q1: JAVA中的查找算法如何实现... 高手帮帮忙
这个。。。我随便乱说几句啊,说的不对别见笑。
有一个数组 当中存有一些字符串
另外有一个字典文件 我也将它导入一个数组 有50000多个单词
然后要找出字符串中包含的单词
由你给的条件可知:
1。数组 应该是从前到后依次顺序扫描字符串。
2。50000多个单词的字典文件一定优化。具体优化要看具体内容吧。
比如你可以按单词的首字母排序,然后分组。等扫描字符串的时候可以分组比较。但这种方法应该没省多少时间。
你还可以把50000多个单词的字典文件按单词的长度进行分组。比如1个字母的分成一组,二个字母的分成一组。。。。N个字母的分成一组,这样就分成了N组。然后扫描字符串的时候你可以按后续匹配(好象叫这个算法吧,名字记不清了)算法,这样就可以省很多时间了。
你还可以这样做,因为你要查的是单词,单词一定有意义。那你可以直接把你的字符串数组先进行语法、语义分析并分割,然后再去匹配你的字典。这样应该是最快的。但这要用到自然语言处理。。。
Q2: 请教:用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"brbr
最大随机数input id=txtMax value=1000 style="width:50px"brbr
button onclick=generate()重新生成/buttonbrbrbrbr
耗时(毫秒):label id=lblTime/labelbrbrbrbr
button onclick=demo("bubble")冒泡排序/buttonbrbr
button onclick=demo("selection")选择排序/buttonbrbr
button onclick=demo("insertion")插入排序/buttonbrbr
button onclick=demo("shell")谢尔排序/buttonbrbr
button onclick=demo("quick")快速排序(递归)/buttonbrbr
button onclick=demo("stackQuick")快速排序(堆栈)/buttonbrbr
button onclick=demo("merge")归并排序/buttonbrbr
button onclick=demo("heap")堆排序/buttonbrbr
/td
td align=left
textarea id=txtOutput readonly style="width:100px;height:100%"/textarea
/td
/tr
/table
/body
这个代码是放在DREAMWEAVER head/head标签里面
Q3: Java用查找算法的一段代码如下: 其中boolean A=false; if(name.equals(arr[i])) 麻烦解释一下 尽量直白
数组从第一个开始比较,完全相同(当前数组值和输入值一模一样)A就赋值为true;不一样A的值不变
常用查找算法java代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于常用查找算法java代码是什么、常用查找算法java代码的信息别忘了在本站进行查找喔。








