Home  >  Article  >  Web Front-end  >  jsDE basic algorithm summary

jsDE basic algorithm summary

怪我咯
怪我咯Original
2017-07-06 11:38:342886browse

This article mainly introduces the basic algorithm of javascript in detail, including output odd and even number control algorithm, Bubble sorting, etc. Interested friends can refer to it

This article shares five javascript algorithms for your reference. The specific contents are as follows

1. Linear search

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>线性查找</title>
</head>
<body>

 <p>数组为:[2,4,6,23,53,545,65,3,24,5,3,6]</p>
 <p>输入要查的值:<input type="text" id="serch" onchange="search_index(this.value)"><p>
 <p>这个值在数组的位置是:<span id="val"></span><p>



 <script>   
  //1.声明查找函数
  //Arr为数组,x为要搜索的值
   function search(Arr,x){
    for(var i=0; i<Arr.length; i++){
     if(Arr[i]==x){
      return i; //返回x在数组中的位置;
     }
    }
    return "不存在"; //循环结束还未发现的话 则返回"不存在";
   }
  
   //2.实例练习
   var arr=[2,4,6,23,53,545,65,3,24,5,3,6]; //声明一个数组  
   function $$(id){
    return document.getElementById(id);
   }

  function search_index(value){
    var val=getX(arr,value)
    $$("val").innerHTML=val;
  }

   function getX(Arr,x){
    var count=0;
    console.log("循环执行了:");
    for(var i=0; i<Arr.length;i++){
     count++
     console.log(count);//输出循环执行的次数
     if(Arr[i]==x){
      return i;
     }
    }
    return "该值不存在";
   }  
     
  
 </script>
</body>
</html>

2.Binary search

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>二分查找温故</title>
</head>
<body>
 
 <script>
 //二分查找值适用于已经排好序的数组中
 //二分就是逢中查找 步骤较少
 var arr=[-13,2,4,6,8,12,34,35,45,56,57,88,110,234,239,342];//有序数组
 
 function binarySearch(arr,x){
  var low=0,high=arr.length-1;
  var count=0;
  while(low<=high){
   count++;
   console.log("这是第"+count+"次循环");
   var mid=Math.floor((low+high)/2);
   if(arr[mid]==x){
    console.log("x所在数组内的引索是:"+mid);
    return mid;
   }
   if(arr[mid]<x){//如果要查找的值大于二分值则low=mid+1;

    low=mid+1;
    console.log("此时low的值是:"+low);
   }else{
    high=mid-1;//如果要查找的值小于二分值则high=mid-1;
    console.log("此时high的值是:"+high);
   }

  }

 }
binarySearch(arr,45);
 </script>
</body>
</html>

3.Bubble sort

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>javascript冒泡排序</title>
</head>
<body>
 <script>
  var arr=new Array(34,-3,43,67,12,44,21,34,5,645,64,3,43,23,25);

  function bubbleSort(){
   var temp;//声明一个缓存变量
   var count_outer=0;//外层循环计数
   var count_inner=0;//内层循环计数

   for(var i=0; i<arr.length;i++){//第一层循环
    count_outer++;
    console.log("这是外层循环的第"+count_outer+"次");
    for(var j=arr.length;j>0;j--){//第二层循环
     count_inner++;
     console.log("...................这是内层循环的第"+count_inner+"次");
     if(arr[j-1]<arr[j-2]){//判断后面一值如果小于前面一值
      temp=arr[j-2];//那么将前面的值存放在temp里面
      arr[j-2]=arr[j-1];//然后将后面一直放在前面值的位置
      arr[j-1]=temp;//在把temp里的值放在后面那个位置
     }
     console.log(".......................................外层第"+count_outer+"次循环"+"内层第"+count_inner+"次循环"+"后的数组排序结果是"+arr)
    }
   }
   return "最终排序后的数组是:["+arr+"]....一共循环了"+count_inner+"次";

  }
  
console.log(bubbleSort()); //控制台输出

 </script>
</body>
</html>

4.Factorial

<!doctype html>
<html>
<head>
 <meta charset="UTF-8">
 <title>阶乘</title>
</head>
<body>
 <script>
 //created in 2014-04-30 
 //factorial function

  function factorial(num){
   if(num<=1){
    return 1;
   }else{
    return num*arguments.callee(num-1);//arguments 是一个类似数组的对象 包含函数中传入的参数 他有一个属性callee,它是一个指针 指向拥有这个arguments对象的函数也就是factorial
   }
  }

  var fac=factorial;//不带括号的函数名是一个指向该函数的指针 所有fac现在也指向这个阶乘函数
  alert(fac(3));//6

 </script>
</body>
</html>

5.Output odd and even number control

<html>
 <head>
  <title>只输出奇数或者偶数项</title>
 </head>
 <body>
  <script>
  var ck = true;//全局变量
  function oddOreven(num) { //num为0或1 控制输出结果 是奇数还是偶数
   for (var i = 0; i < 30; i++) {
    if (ck) {
     ck = false; //如果ck为true 让其等于false
     alert(i + num);
    } else {
     ck = true;
    }
   }
  }
  //调用
  oddOreven(0); //偶数
  oddOreven(1) //奇数
  </script>
 </body>
</html>

The above is the detailed content of jsDE basic algorithm summary. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn