查找演算法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
被分開在兩行中了,
這個怎麼算呢?