Home > Web Front-end > JS Tutorial > body text

JS implements the simplest search, sorting, and deduplication algorithm

php中世界最好的语言
Release: 2018-05-28 14:31:33
Original
1594 people have browsed it

This time I will bring you the simplest search, sorting, and deduplication algorithm implemented in JS. What are the things to pay attention to in JS implementation of search, sorting, and deduplication? The following is a practical case. Let’s take a look. one time. Today I summarized a simple sorting algorithm

[Custom sorting]

First find the smallest number, and then compare this number with other numbers in the array. If you find a number smaller than this number, swap the two numbers, and then continue to find the next smallest number for the next round of comparison

var arr = [31, 6, 19, 8, 2, 3];
function findMin(start, arr) {
  var iMin = arr[start];
  var iMinIndex = start;
  for (var i = start + 1; i < arr.length; i++) {
    if (arr[i] < iMin) {
      iMin = arr[i];
      iMinIndex = i;
    }
  }
  return iMinIndex;
}
function sort1(arr) {
  for (var i = 0; i < arr.length; i++) {
    var iMinIndex = findMin(i, arr);
    var car;
    car = arr[i];
    arr[i] = arr[iMinIndex];
    arr[iMinIndex] = car;
  }
  return arr;
}
document.write(sort1(arr));
Copy after login

[Linear Search]: Search one by one

//不重复 有序
var arr = [0];
for (var i = 1; i < 100000; i++) {
  arr[i] = arr[i - 1] + Math.floor(Math.random() * 4 + 1);
}
function find1(n, arr) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == n) {
      return true;
    }
  }
  return false;
}
//测试性能
var t1 = new Date().getTime();
for (var i = 0; i < 10000; i++) {
  var n = Math.random() * 10000;
  find2(n, 0, arr.length - 1)
}
alert(new Date().getTime() - t1);
Copy after login

Binary search

】: Keep dividing it into two parts. Partial search is a universal method, not necessarily the best, but a guaranteed method. . (Divide and conquer method)

***Add the middle values ​​and divide by two, uniformly left, round down

//不重复 有序
var arr = [12, 17, 23, 34, 45, 76, 89];
function find2(n, s, e) {
  //边界处理
  if (s > e) {
    return false;
  } else if (s == e) {
    if (arr[s] == n) {
      return true;
    } else {
      return false;
    }
  }
  var c = Math.floor((s + e) / 2);
  if (arr[c] == n) {
    return true;
  } else {
    if (n < arr[c]) {
      return find2(n, s, c);
    } else {
      return find2(n, c + 1, e);
    }
  }
}
alert(find2(34, 0, arr.length - 1)); //true false
Copy after login

[Boundary processing]-----Recursion, one level Find one layer down

//要求数组不重复有顺序\
var arr = [12, 23, 34, 45, 56, 67, 78]
function find2(n, s, e) {
  if (s > e) {
    return fasle;
  } else if (s == e) {
    if (arr[s] == e) {
      return true;
    } else {
      return false;
    }
  }
  var c = Math.floor((s + e) / 2);
  if (arr[c] == n) {
    return true;
  } else {
    if (n < arr[c]) {
      return find2(n, s, c);
    } else {
      return find2(n, c + 1, e);
    }
  }
}
alert(find2(12, arr.length + 1, 78));
Copy after login

Application

【Find minimum value】

var arr = [12, 54, 32, 9, 5, 3, 1, 101, -100, -1000];
function findMin(s, e) {
  if (s > e) {
    return [];
  } else if (s == e) {
    return arr[s];
  } else if (s == e - 1) {
    if (arr[s] < arr[e]) {
      return arr[s];
    } else {
      return arr[e];
    }
  }
  var c = Math.floor((s + e) / 2);
  var l = findMin(s, c);
  var r = findMin(c + 1, e);
  if (l < r) {
    return l;
  } else {
    return r;
  }
}
alert(findMin(0, arr.length - 1));
Copy after login

【Array deduplication】

var arr = [1, 2, 3, 4, 5, 4, 3, 4, 5, 2, 1, 4, 2, 1, 5, 7];
function findInArr(n, arr) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == n) {
      return true;
    }
  }
  return false;
}
function removeCopy(s, e) {
  if (s > e) {
    return [];
  } else if (s == e) {
    return [arr[s]];
  } else if (s == e - 1) {
    if (arr[s] == arr[e]) {
      return [arr[s]];
    } else {
      return [arr[s], arr[e]]
    }
  }
  var c = Math.floor((s + e) / 2);
  var l = removeCopy(s, c);
  var r = removeCopy(c + 1, e);
  for (var i = 0; i < r.length; i++) {
    if (!findInArr(r[i], l)) {
      l.push(r[i]);
    }
  }
  return l;
}
document.write(removeCopy(0, arr.length - 1));
Copy after login

Array sorting

Bubble sort

BubbleSortLoop, take out two values ​​​​each time and compare them two by two. If the next value is smaller than the current one, then Swap positions

The outer loop is to loop to get the numbers, and the inner loop is to swap the numbers in pairs

var arr = [ - 122, -2, 5, 6, 73, 34, 5, 2];
function BubbleSort(arr) {
  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr.length - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        var tmp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = tmp
      }
    }
  }
  return arr;
}
document.write(BubbleSort(arr));
Copy after login

[Quick Sort] -------quickSort

Get For the number in the middle of the array, place the number smaller than the middle number on the left side of the middle number, and the larger number than the middle number on the right side, and then link the two times together

function quickSort(arr, s, e) {
  //边界处理 参与流程
  if (arr.length == 0) {
    return [];
  }
  var c = Math.floor((s + e) / 2);
  var arrC = arr.splice(c, 1);
  var l = [];
  var r = [];
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] < arrC) {
      l.push(arr[i]);
    } else {
      r.push(arr[i]);
    }
  }
  return quickSort(l).concat(arrC, quickSort(r));
}
var arr = [5, 5, 12, 56, 1, 67, -1, -23 - 1];
document.write(quickSort(arr, 0, arr.length - 1));
Copy after login

[Hash] hash Hash array ------ Commonly used structures in js

Add

var arr = [];
arr.length = 0;
var cont = 0;
function hash_add(n) {
  var pos = n % arr.length;
  //当空间不足的时候
  if (arr[pos]) {
    while (arr[pos]) {
      cont++;
      if (arr[pos] == n) {
        return;
      } else {
        pos++;
        if (pos == arr.length) {
          pos = 0;
        }
      }
    }
    arr[pos] = n;
  } else {
    arr[pos] = n;
  }
  //空间不足的时候的扩建
  if (cont == arr.length) {
    //d等呗扩建
    var oldArr = arr;
    arr.length = oldArr.length * 2;
    arr = [];
    for (var i = 0; i < oldArr.length; i++) {
      arr.push(oldArr[i]);
      count = 0;
    }
  }
}
hash_add();
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to use the Vue-based lazy loading plug-in vue-view-lazy


##How to use jquery toggleClass () attribute to create article paragraphs and change the background color

The above is the detailed content of JS implements the simplest search, sorting, and deduplication algorithm. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!