script
創(chuàng)新互聯(lián)提供高防服務(wù)器租用、云服務(wù)器、香港服務(wù)器、電信內(nèi)江機(jī)房等
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("個(gè)數(shù)和最大值必須是一個(gè)整數(shù)");
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
隨機(jī)數(shù)個(gè)數(shù)input id=txtCount value=500 style="width:50px"brbr
最大隨機(jī)數(shù)input id=txtMax value=1000 style="width:50px"brbr
button onclick=generate()重新生成/buttonbrbrbrbr
耗時(shí)(毫秒):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
這個(gè)代碼是放在DREAMWEAVER head/head標(biāo)簽里面
數(shù)組從第一個(gè)開始比較,完全相同(當(dāng)前數(shù)組值和輸入值一模一樣)A就賦值為true;不一樣A的值不變
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("請(qǐng)輸入要查詢的單詞");
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("第一次出現(xiàn)在"+begin+"字節(jié)處");
}
m++;
end=begin;
}
else
{
begin=str.indexOf(s.substring(0,1));
if(m==0)
{
System.out.println("第一次出現(xiàn)在"+begin+"字節(jié)處");
}
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+"出現(xiàn)了"+count+"次");
}
}
public class Demo{
public static void main(String[] args){
int a[] = {28,39,49,78,23};
int x = 49;
//下面是最簡(jiǎn)單的冒泡排序
int temp;
for(int i=0; ia.length;++i){
? ? for(int j=a.length-1;ji;--j){
? ? ? ? if(a[j] a[j-1]){
? ? ? ? ? ?temp = a[j];
? ? ? ? ? ?a[j] = a[j-1];
? ? ? ? ? ?a[j-1] = temp;
? ? ? ?}
? ? }
}
System.out.println("排序完成:");
for(int emp:a){
System.out.print(emp+" ");
}
//下面是二分法查找(折中查找)
int first = 0;
int last = a.length-1;
int mid;
while(first=last){
mid = (first+last)/2;
if(a[mid]==x){
System.out.println("\n查找到x,在數(shù)組的第"+(mid+1)+"位");
break;
}
if(a[mid]x)
last = mid-1;
if(a[mid]x)
first = mid+1;
}
}
}
折中查詢也叫折半查詢,是一種查詢方法,折中查詢方法針對(duì)的是已經(jīng)排好序的數(shù)列來說!
例如:有一組有序數(shù)列:3,6,8,10,20,23,28
現(xiàn)在讓你用算法實(shí)現(xiàn)看看次數(shù)列中有沒有15.。一般的方法是將數(shù)列中的數(shù)一個(gè)一個(gè)的跟15比較,直到結(jié)尾,這樣要比較7次才能得出結(jié)果!
折中查詢是這樣的:
這是一個(gè)已經(jīng)排好序的數(shù)列,所以找到這個(gè)數(shù)列中間位置的那個(gè)數(shù),這里是10,用10跟15比較,發(fā)現(xiàn)要找的15比10大,所以10前面的數(shù)你就不用管了,只去10后面的數(shù)里面找,只剩:20,23,28了,看看有沒有15,在10后面的數(shù)里,再找當(dāng)中的那個(gè)數(shù),這里是23,23要比15大,所以在去10到23之間里面找,15跟23里面已經(jīng)沒有數(shù)了,所以這個(gè)數(shù)列里面沒有15.這種方法得出沒有15的結(jié)果只做了2次比較,省事多了!
就用readline方法,
一行一行地查吧,
樓上的算法還是可行的,
要指出在哪一行,
直接在readline中設(shè)置一個(gè)變量就可以了,
掃過一行自增就行
另外個(gè)人認(rèn)為,
還要考慮這樣一種情況,
比如說關(guān)鍵字為
java
但
ja
va
被分開在兩行中了,
這個(gè)怎么算呢?
本文標(biāo)題:java折中查詢算法代碼 折半查找java代碼
文章位置:http://chinadenli.net/article44/hgdehe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、品牌網(wǎng)站設(shè)計(jì)、全網(wǎng)營(yíng)銷推廣、服務(wù)器托管、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)