javascript数据结构与算法---检索算法(顺序查找、最大最小值、自组织查询)
一、顺序查找法
( i = 0; i < arr.length; ++ (arr[i] == -1<span style="color: #0000ff;">function<span style="color: #000000;"> dispArr(arr) {
<span style="color: #0000ff;">var str = ""<span style="color: #000000;">;
<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < arr.length; ++<span style="color: #000000;">i) {
str += arr[i] + " "<span style="color: #000000;">;
<span style="color: #0000ff;">if ((i > 0)&&(i % 10 == 0<span style="color: #000000;">)) {
str += "\n"<span style="color: #000000;">;
}
}
<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < 100; ++<span style="color: #000000;">i) {
nums[i] = Math.floor(Math.random() * 101<span style="color: #000000;">);
}
dispArr(nums);
console.log("Enter a number to search for: "<span style="color: #000000;">);
<span style="color: #0000ff;">var num = parseInt(23<span style="color: #000000;">);
console.log();
<span style="color: #0000ff;">var index =<span style="color: #000000;"> seqSearch(nums,num);
<span style="color: #0000ff;">if (index > -1<span style="color: #000000;">) {
console.log(num + " 存在数组中."+"在数组中的索引位置为" +<span style="color: #000000;"> index);
}<span style="color: #0000ff;">else<span style="color: #000000;"> {
console.log(num + " 不存在数组中"<span style="color: #000000;">);
}
<span style="color: #0000ff;">var str = ""<span style="color: #000000;">;
<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < arr.length; ++<span style="color: #000000;">i) {
str += arr[i] + " "<span style="color: #000000;">;
<span style="color: #0000ff;">if ((i > 0)&&(i % 10 == 0<span style="color: #000000;">)) {
str += "\n"<span style="color: #000000;">;
}
}
console.log(str);
}
<span style="color: #0000ff;">var nums =<span style="color: #000000;"> [];<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < 100; ++<span style="color: #000000;">i) {
nums[i] = Math.floor(Math.random() * 101<span style="color: #000000;">);
}
dispArr(nums);
console.log("Enter a number to search for: "<span style="color: #000000;">);
<span style="color: #0000ff;">var num = parseInt(23<span style="color: #000000;">);
console.log();
<span style="color: #0000ff;">var index =<span style="color: #000000;"> seqSearch(nums,num);
<span style="color: #0000ff;">if (index > -1<span style="color: #000000;">) {
console.log(num + " 存在数组中."+"在数组中的索引位置为" +<span style="color: #000000;"> index);
}<span style="color: #0000ff;">else<span style="color: #000000;"> {
console.log(num + " 不存在数组中"<span style="color: #000000;">);
}
二、最大最小值、自组织查询
min = arr[0 ( i = 1; i < arr.length; ++ (arr[i] <=
max = arr[0 ( i = 1; i < arr.length; ++ (arr[i] >=<span style="color: #008000;">/*<span style="color: #008000;">自组织查询
<span style="color: #008000;">/
<span style="color: #0000ff;">function<span style="color: #000000;"> seqSearch(arr,data) {
<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < arr.length; ++<span style="color: #000000;">i) {
<span style="color: #0000ff;">if (arr[i] == data && i > (arr.length 0.2<span style="color: #000000;">)) {
swap(arr,i,0<span style="color: #000000;">);
<span style="color: #0000ff;">return <span style="color: #0000ff;">true<span style="color: #000000;">;
}
<span style="color: #0000ff;">else <span style="color: #0000ff;">if (arr[i] ==<span style="color: #000000;"> data) {
<span style="color: #0000ff;">return <span style="color: #0000ff;">true<span style="color: #000000;">;
}
}
<span style="color: #0000ff;">return <span style="color: #0000ff;">false<span style="color: #000000;">;
} <span style="color: #0000ff;">function<span style="color: #000000;"> dispArr(arr) {
<span style="color: #0000ff;">var str = ""<span style="color: #000000;">;
<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < arr.length; ++<span style="color: #000000;">i) {
str += arr[i] + " "<span style="color: #000000;">;
<span style="color: #0000ff;">if ((i > 0)&&(i % 10 == 0<span style="color: #000000;">)) {
str += "\n"<span style="color: #000000;">;
}
}
<span style="color: #0000ff;">function<span style="color: #000000;"> seqSearch(arr,data) {
<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < arr.length; ++<span style="color: #000000;">i) {
<span style="color: #0000ff;">if (arr[i] == data && i > (arr.length 0.2<span style="color: #000000;">)) {
swap(arr,i,0<span style="color: #000000;">);
<span style="color: #0000ff;">return <span style="color: #0000ff;">true<span style="color: #000000;">;
}
<span style="color: #0000ff;">else <span style="color: #0000ff;">if (arr[i] ==<span style="color: #000000;"> data) {
<span style="color: #0000ff;">return <span style="color: #0000ff;">true<span style="color: #000000;">;
}
}
<span style="color: #0000ff;">return <span style="color: #0000ff;">false<span style="color: #000000;">;
} <span style="color: #0000ff;">function<span style="color: #000000;"> dispArr(arr) {
<span style="color: #0000ff;">var str = ""<span style="color: #000000;">;
<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < arr.length; ++<span style="color: #000000;">i) {
str += arr[i] + " "<span style="color: #000000;">;
<span style="color: #0000ff;">if ((i > 0)&&(i % 10 == 0<span style="color: #000000;">)) {
str += "\n"<span style="color: #000000;">;
}
}
console.log(str);
}
<span style="color: #0000ff;">var nums =<span style="color: #000000;"> [];
<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < 100; ++<span style="color: #000000;">i) {
nums[i] = Math.floor(Math.random() * 101<span style="color: #000000;">);
}
<span style="color: #0000ff;">var minValue =<span style="color: #000000;"> findMin(nums);
dispArr(nums);
console.log("The minimum value is: " +<span style="color: #000000;"> minValue);
<span style="color: #0000ff;">var maxValue =<span style="color: #000000;"> findMax(nums);
console.log("The maximum value is: " + maxValue);