javascript数据结构与算法---检索算法(二分查找法、计算重复次数)
- 将数组的第一个位置设置为下边界(0).将数组的最后一个元素所在的位置设置为上边界(数组的长度减1)。
- 若下边界等于或小于上边界,则做如下操作:
- (1).将中点设置为(上边界加上下边界) 除以2.
- (2). 如果中点的元素小于查询的值,则将下边界设置为中点元素所在下标加1.
- (3). 如果中点的元素大于查询的值,则将上边界设置为中点元素所在下标减1.
- (4). 否则中点元素即为要查找 的数据,可以进行返回。@H_4043@<span style="color: #008000;">*/@H4043@
<span style="color: #0000ff;">function@H4043@<span style="color: #000000;"> binSearch(arr,data) {
@H4043@<span style="color: #0000ff;">var@H4043@ lowerBound = 0<span style="color: #000000;">;
@H4043@<span style="color: #0000ff;">var@H4043@ upperBound = arr.length - 1<span style="color: #000000;">;
@H4043@<span style="color: #0000ff;">while@H4043@(lowerBound <=<span style="color: #000000;"> upperBound) {
@H4043@<span style="color: #0000ff;">var@H4043@ mid = Math.floor((upperBound + lowerBound)/2);
<span style="color: #0000ff;">if@H4043@(arr[mid] <<span style="color: #000000;"> data) {
lowerBound @H4043@= mid + 1<span style="color: #000000;">;
}@H4043@<span style="color: #0000ff;">else@H4043@ <span style="color: #0000ff;">if@H4043@(arr[mid] ><span style="color: #000000;"> data) {
upperBound @H4043@= mid - 1<span style="color: #000000;">;
}@H4043@<span style="color: #0000ff;">else@H4043@<span style="color: #000000;"> {
@H4043@<span style="color: #0000ff;">return@H4043@<span style="color: #000000;"> mid;
}
}
@H4043@<span style="color: #0000ff;">return@H404_3@ -1<span style="color: #000000;">;
}
@H_4043@<span style="color: #008000;">/*@H404_3@<span style="color: #008000;">
计算重复次数
当binSearch()函数找到某个值时,如果在数据集中还有其他相同的值出现,那么该函数会定位在类似值的附近。
换句话说,其他相同的值可能会出现已找到值的左边或右边。
如果在数据集中能找到这个值,那么这个函数将开始通过两个循环来统计这个值出现的次数。
第一个循环向下遍历数组,统计找到的值出现的次数,当下一个值与要查找的值不匹配时则停止计数。
第二个循环向上遍历数组,统计找到的值出现的次数,当下一个值与要查找的值不匹配时则停止计数。
- @H_4043@<span style="color: #008000;">*/@H4043@
<span style="color: #0000ff;">function@H4043@<span style="color: #000000;"> count(arr,data) {
@H4043@<span style="color: #0000ff;">var@H4043@ count = 0<span style="color: #000000;">;
@H4043@<span style="color: #0000ff;">var@H4043@ position =<span style="color: #000000;"> binSearch(arr,data);
@H4043@<span style="color: #0000ff;">if@H4043@ (position > -1<span style="color: #000000;">) {
@H4043@++<span style="color: #000000;">count;
@H4043@<span style="color: #0000ff;">for@H4043@ (<span style="color: #0000ff;">var@H4043@ i = position-1; i > 0; --<span style="color: #000000;">i) {
@H4043@<span style="color: #0000ff;">if@H4043@ (arr[i] ==<span style="color: #000000;"> data) {
@H4043@++<span style="color: #000000;">count;
}
@H4043@<span style="color: #0000ff;">else@H4043@<span style="color: #000000;"> {
@H4043@<span style="color: #0000ff;">break@H4043@<span style="color: #000000;">;
}
}
@H4043@<span style="color: #0000ff;">for@H4043@ (<span style="color: #0000ff;">var@H4043@ i = position+1; i < arr.length; ++<span style="color: #000000;">i) {
@H4043@<span style="color: #0000ff;">if@H4043@ (arr[i] ==<span style="color: #000000;"> data) {
@H4043@++<span style="color: #000000;">count;
}
@H4043@<span style="color: #0000ff;">else@H4043@<span style="color: #000000;"> {
@H4043@<span style="color: #0000ff;">break@H4043@<span style="color: #000000;">;
}
}
}
@H4043@<span style="color: #0000ff;">return@H404_3@<span style="color: #000000;"> count;
}
@H_4043@<span style="color: #0000ff;">var@H404_3@ nums = [90,43,49,15,23,2,70,20,95,69,29,26<span style="color: #000000;">];
@H_4043@<span style="color: #0000ff;">var@H404_3@ list =<span style="color: #000000;"> qSort(nums);
console.log(list);
@H_4043@<span style="color: #0000ff;">var@H4043@ findnum = 23<span style="color: #000000;">;
console.log(@H4043@"需要查找的数据为: " +<span style="color: #000000;"> findnum);
@H4043@<span style="color: #0000ff;">var@H4043@ retVal =<span style="color: #000000;"> binSearch(list,findnum);
@H4043@<span style="color: #0000ff;">if@H4043@ (retVal >= 0<span style="color: #000000;">) {
console.log( @H4043@"找到 " + findnum + "的位置为: "+<span style="color: #000000;">retVal);
}@H4043@<span style="color: #0000ff;">else@H4043@<span style="color: #000000;"> {
console.log(@H4043@" is not in array."<span style="color: #000000;">);
}
console.log(findnum @H404_3@+ "重复次数为"+count(list,findnum));