Home > Web Front-end > JS Tutorial > Sharing of several common sorting codes in JS

Sharing of several common sorting codes in JS

零到壹度
Release: 2018-03-20 11:27:08
Original
1505 people have browsed it

This article mainly introduces the specific steps and related operation skills of several common JS sorting codes. Friends in need can refer to it. I hope it can help everyone.

4.2.1 Bubble sort
Algorithm introduction
Analysis:
Compare two adjacent elements, if the previous If one is larger than the last one, the positions are swapped.
The last element should be the largest one in the first round.
Follow the method in step 1 to compare two adjacent elements. At this time, since the last element is already the largest, there is no need to compare the last element.

js code implementation

function bubble_sort(arr){
  for(var i=0;i<arr.length-1;i++){
    for(var j=0;j<arr.length-i-1;j++){
      if(arr[j]>arr[j+1]){
        var swap=arr[j];
        arr[j]=arr[j+1];
        arr[j+1]=swap;
      }
    }
  }
}
var arr=[3,1,5,7,2,4,9,6,10,8];
bubble_sort(arr);
console.log(arr);
Copy after login

4.2.2 Quick sort
js code implementation
Analysis: Quick sort is An improvement on bubble sort, the first pass of sorting divides the data into two parts, one part is smaller than all the data in the other part. Then call it recursively, performing quick sort on both sides.

function quick_sort(arr){
  if(arr.length<=1){
    return arr;
  }
  var pivotIndex=Math.floor(arr.length/2);
  var pivot=arr.splice(pivotIndex,1)[0];
  var left=[];
  var right=[];
  for(var i=0;i<arr.length;i++){
    if(arr[i]<pivot){
      left.push(arr[i]);
    }else{
      right.push(arr[i]);
    }
  }
  return quick_sort(left).concat([pivot],quick_sort(right));
}
var arr=[5,6,2,1,3,8,7,1,2,3,4,7];
console.log(quick_sort(arr));
Copy after login

4.2.3 Insertion sort
Algorithm introduction
Analysis:
Starting from the first element, this element can Considered to have been sorted
Take out the next element and scan from back to front in the sorted element sequence
If the element (sorted) is greater than the new element, move the element to the next position
Repeat Step 3, until you find the position where the sorted element is less than or equal to the new element
Insert the new element into the next position
Repeat step 2

js code implementation

function insert_sort(arr){
  var i=1,
  j,key,len=arr.length;
  for(;i<len;i++){
    var j=i;
    var key=arr[j];
    while(--j>-1){
      if(arr[j]>key){
        arr[j+1]=arr[j];
      }else{
        break;
      }
    }
    arr[j+1]=key;
  }
  return arr;
}
insert_sort([2,34,54,2,5,1,7]);
Copy after login

The above is the detailed content of Sharing of several common sorting codes in JS. 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