javascript basic algorithm summary
- 2021-01-25 07:02:56
- OfStack
In this paper, we share five javascript algorithms for your reference, the specific content is as follows
1. Linear lookup
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Linear search </title>
</head>
<body>
<p> The array is: [2,4,6,23,53,545,65,3,24,5,3,6]</p>
<p> Enter the value to look up :<input type="text" id="serch" onchange="search_index(this.value)"><p>
<p> The value in the array position is: <span id="val"></span><p>
<script>
//1. Declare the lookup function
//Arr For arrays, x Is the value to search for
function search(Arr,x){
for(var i=0; i<Arr.length; i++){
if(Arr[i]==x){
return i; // return x Position in the array;
}
}
return " There is no "; // The end of the loop has not been discovered It returns " There is no ";
}
//2. Examples of practice
var arr=[2,4,6,23,53,545,65,3,24,5,3,6]; // The statement 1 An array
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(" The loop executes :");
for(var i=0; i<Arr.length;i++){
count++
console.log(count);// The number of times the output loop is executed
if(Arr[i]==x){
return i;
}
}
return " The value does not exist ";
}
</script>
</body>
</html>
2.2 points to find
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2 Look up the temperature </title>
</head>
<body>
<script>
//2 The fractional lookup value applies to an already sorted array
//2 Divide is to find the middle Fewer steps
var arr=[-13,2,4,6,8,12,34,35,45,56,57,88,110,234,239,342];// Orderly array
function binarySearch(arr,x){
var low=0,high=arr.length-1;
var count=0;
while(low<=high){
count++;
console.log(" This is the first "+count+" loops ");
var mid=Math.floor((low+high)/2);
if(arr[mid]==x){
console.log("x The guide in the array is :"+mid);
return mid;
}
if(arr[mid]<x){// If the value you're looking for is greater than 2 Score is low=mid+1 ;
low=mid+1;
console.log(" At this time low The value is :"+low);
}else{
high=mid-1;// If the value you're looking for is less than 2 Score is high=mid-1 ;
console.log(" At this time high Values are: "+high);
}
}
}
binarySearch(arr,45);
</script>
</body>
</html>
Bubble Sort
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>javascript Bubble sort </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;// The statement 1 Cache variables
var count_outer=0;// Outer cycle count
var count_inner=0;// Inner cycle count
for(var i=0; i<arr.length;i++){// The first 1 Layer circulation
count_outer++;
console.log(" This is the fourth part of the outer cycle "+count_outer+" time ");
for(var j=arr.length;j>0;j--){// The first 2 Layer circulation
count_inner++;
console.log("................... This is the first part of the inner cycle "+count_inner+" time ");
if(arr[j-1]<arr[j-2]){// Behind the judgment 1 If the value is less than the previous value 1 value
temp=arr[j-2];// So store the previous values temp inside
arr[j-2]=arr[j-1];// And then I'm going to put 1 Straight on the front face value
arr[j-1]=temp;// In the temp I put the value in the back there
}
console.log("....................................... Outer layer of the first "+count_outer+" loops "+" The inner layer first "+count_inner+" loops "+" The result of sorting the array is "+arr)
}
}
return " The final sorted array is :["+arr+"]....1 The total cycle "+count_inner+" time ";
}
console.log(bubbleSort()); // Console output
</script>
</body>
</html>
4. The factorial
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title> factorial </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 is 1 An array of objects Contains the arguments passed in the function He has a 1 A property callee, It is a 1 A pointer to the Point to have this arguments The function of the object is just factorial
}
}
var fac=factorial;// The function name without parentheses is 1 A pointer to the function all fac And now it's also pointing to this factorial function
alert(fac(3));//6
</script>
</body>
</html>
5. Output odd-even control
<html>
<head>
<title> Only odd or even entries are printed </title>
</head>
<body>
<script>
var ck = true;// The global variable
function oddOreven(num) { //num for 0 or 1 Control output Is it an odd number or an even number
for (var i = 0; i < 30; i++) {
if (ck) {
ck = false; // if ck for true Let it is equal to the false
alert(i + num);
} else {
ck = true;
}
}
}
// call
oddOreven(0); // An even number
oddOreven(1) // An odd number
</script>
</body>
</html>
The above is all the content of this article, I hope to help you better learn javascript programming.