javascript数据结构与算法---检索算法(二分查找法、计算重复次数)

前端之家收集整理的这篇文章主要介绍了javascript数据结构与算法---检索算法(二分查找法、计算重复次数)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
javascript数据结构与算法---检索算法(二分查找法、计算重复次数
404_3@404_3@404_3@ 404_3@404_3@404_3@ (arr.length == 0404_3@404_3@404_3@404_3@ left = [];404_3@404_3@ 404_3@ right = [];404_3@404_3@ 404_3@ pivot = arr[0404_3@404_3@ (404_3@ i = 1; i < arr.length; i++404_3@404_3@ (arr[i] <404_3@404_3@404_3@404_3@ qSort(left).concat(pivot,qSort(right));404_3@404_3@ @H_4043@<span style="color: #008000;">/*@H404_3@<span style="color: #008000;">二分查找法,基本原理如下:

  • 将数组的第一个位置设置为下边界(0).将数组的最后一个元素所在的位置设置为上边界(数组的长度减1)。
  • 若下边界等于或小于上边界,则做如下操作:
  • (1).将中点设置为(上边界加上下边界) 除以2.
  • (2). 如果中点的元素小于查询的值,则将下边界设置为中点元素所在下标加1.
  • (3). 如果中点的元素大于查询的值,则将上边界设置为中点元素所在下标减1.
  • (4). 否则中点元素即为要查找 的数据,可以进行返回。@H_4043@<span style="color: #008000;">*/@H4043@
    <span style="color: #0000ff;">function@H
    4043@<span style="color: #000000;"> binSearch(arr,data) {
    @H
    4043@<span style="color: #0000ff;">var@H4043@ lowerBound = 0<span style="color: #000000;">;
    @H
    4043@<span style="color: #0000ff;">var@H4043@ upperBound = arr.length - 1<span style="color: #000000;">;
    @H
    4043@<span style="color: #0000ff;">while@H4043@(lowerBound <=<span style="color: #000000;"> upperBound) {
    @H
    4043@<span style="color: #0000ff;">var@H4043@ mid = Math.floor((upperBound + lowerBound)/2);
    <span style="color: #0000ff;">if@H
    4043@(arr[mid] <<span style="color: #000000;"> data) {
    lowerBound @H
    4043@= mid + 1<span style="color: #000000;">;
    }@H
    4043@<span style="color: #0000ff;">else@H4043@ <span style="color: #0000ff;">if@H4043@(arr[mid] ><span style="color: #000000;"> data) {
    upperBound @H
    4043@= mid - 1<span style="color: #000000;">;
    }@H
    4043@<span style="color: #0000ff;">else@H4043@<span style="color: #000000;"> {
    @H
    4043@<span style="color: #0000ff;">return@H4043@<span style="color: #000000;"> mid;
    }
    }
    @H
    4043@<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@H
    4043@<span style="color: #000000;"> count(arr,data) {
    @H
    4043@<span style="color: #0000ff;">var@H4043@ count = 0<span style="color: #000000;">;
    @H
    4043@<span style="color: #0000ff;">var@H4043@ position =<span style="color: #000000;"> binSearch(arr,data);
    @H
    4043@<span style="color: #0000ff;">if@H4043@ (position > -1<span style="color: #000000;">) {
    @H
    4043@++<span style="color: #000000;">count;
    @H
    4043@<span style="color: #0000ff;">for@H4043@ (<span style="color: #0000ff;">var@H4043@ i = position-1; i > 0; --<span style="color: #000000;">i) {
    @H
    4043@<span style="color: #0000ff;">if@H4043@ (arr[i] ==<span style="color: #000000;"> data) {
    @H
    4043@++<span style="color: #000000;">count;
    }
    @H
    4043@<span style="color: #0000ff;">else@H4043@<span style="color: #000000;"> {
    @H
    4043@<span style="color: #0000ff;">break@H4043@<span style="color: #000000;">;
    }
    }
    @H
    4043@<span style="color: #0000ff;">for@H4043@ (<span style="color: #0000ff;">var@H4043@ i = position+1; i < arr.length; ++<span style="color: #000000;">i) {
    @H
    4043@<span style="color: #0000ff;">if@H4043@ (arr[i] ==<span style="color: #000000;"> data) {
    @H
    4043@++<span style="color: #000000;">count;
    }
    @H
    4043@<span style="color: #0000ff;">else@H4043@<span style="color: #000000;"> {
    @H
    4043@<span style="color: #0000ff;">break@H4043@<span style="color: #000000;">;
    }
    }
    }
    @H
    4043@<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(@H
4043@"需要查找的数据为: " +<span style="color: #000000;"> findnum);
@H
4043@<span style="color: #0000ff;">var@H4043@ retVal =<span style="color: #000000;"> binSearch(list,findnum);
@H
4043@<span style="color: #0000ff;">if@H4043@ (retVal >= 0<span style="color: #000000;">) {
console.log( @H
4043@"找到 " + findnum + "的位置为: "+<span style="color: #000000;">retVal);
}@H
4043@<span style="color: #0000ff;">else@H4043@<span style="color: #000000;"> {
console.log(@H
4043@" is not in array."<span style="color: #000000;">);
}
console.log(findnum @H
404_3@+ "重复次数为"+count(list,findnum));

 

猜你在找的JavaScript相关文章