How to use JavaScript to implement sorting algorithms

How to use JavaScript to implement sorting algorithms

Bubble Sort

Bubble sort is to repeatedly compare the sizes of two adjacent numbers starting from the right side of the sequence, and then swap the positions of the two numbers based on the results.

JavaScript code implementation:

Code introduction: declare an array variable, assign values ​​to the array variable through while, stop inputting when "#" is input, then traverse two adjacent numbers, arrange the two adjacent numbers in ascending order, and traverse n-1 times to achieve sorting;

 var a = Array();
    flag=true;
    var i = 0;
    var j = 0;
    var temp = 0;
    while(flag){
        var b = prompt("Please enter the i-th number:");
        if(b=="#"){
          flag=false;
        }else{
            a[i] = b;
        }
        i++;
    }
   
    s = a.length;
    for(j=0;j<s;j++){
        console.log(a[j]); 
    }
    for(j=1;j<s;j++){
       for(i=0;i<sj;i++){
           if(a[i]>a[i+1]){
                temp=a[i];
                a[i]=a[i+1];
                a[i+1]=temp;
           }
       }
    }
    for(j=0;j<s;j++){
        console.log("Ascending order:",a[j]); 
    }

Enter 3, 4, 2, 8, 6. The output is as follows:

Selection Sort

Selection sorting is to repeatedly find the minimum value from the data to be sorted and exchange it with the leftmost number in the sequence.

JavaScript code implementation:

    var a = Array();
    flag=true;
    var i = 0;
    var j = 0;
    var temp = 0;
    while(flag){
        var b = prompt("Please enter the i-th number:");
        if(b=="#"){
          flag=false;
        }else{
            a[i] = b;
        }
        i++;
    }
    s = a.length;
    for(j=0;j<s;j++){
        console.log("array:",a[j]); 
    }
    for(i=0;i<s;i++){
        for(j=0;j<s;j++){
            if(a[i]>a[j]){
                temp = a[j];
                a[j] = a[i];
                a[i] = temp;
            }
        }
    }
    for(j=s-1;j>=0;j--){
        console.log("Select sort:",a[j]); 
    }

Insertion Sort

Insertion sort algorithm: By building a storage of ordered array elements, for unsorted array elements, traverse from the last element to the first element in the sorted array, find the corresponding position and insert it.

js code implementation:

var flag = true;
    var a = new Array();
    var i=0;
    while(flag){
        
        var s = prompt('Please enter a number:');
        if(s=='#'){
            flag=false;
        }else{
            a[i] = s;
        }
        i++;
    }
    console.log(a);
    for(var i=1;i<a.length;i++){
    for(var j = i;j>0;j--){
        if(a[j-1]>a[j]){
            var temp = a[j-1];
            a[j-1] = a[j];
            a[j] = temp;
        }
    }
}
for(var i=0;i<a.length;i++){
console.log(a[i]);
}

Enter 5, 3, 7, 4, 8, 1, 6. The output is as follows:

Summarize

This is the end of this article on how to use JavaScript to implement sorting algorithms. For more relevant js implementation of sorting algorithms, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Using JS to implement binary tree traversal algorithm example code
  • JavaScript programming through Matlab centroid algorithm positioning learning
  • Binary Search Tree Algorithm Tutorial for JavaScript Beginners
  • Summary of seven sorting algorithms implemented in JavaScript (recommended!)
  • A brief discussion on an efficient algorithm for constructing tree structures in JavaScript
  • How to Learn Algorithmic Complexity with JavaScript
  • js implements the algorithm for specifying the order and amount of red envelopes
  • How to use javascript to do simple algorithms

<<:  Design theory: On the issues of scheme, resources and communication

>>:  HTML uncommon tags optgroup, sub, sup and bdo example code

Recommend

Example of Form action and onSubmit

First: action is an attribute of form. HTML5 has d...

Writing High-Quality Code Web Front-End Development Practice Book Excerpts

(P4) Web standards are composed of a series of sta...

Analysis of the Principle and Method of Implementing Linux Disk Partition

remember: IDE disk: the first disk is hda, the se...

Docker private warehouse harbor construction process

1. Preparation 1.1 harbor download harbor downloa...

A brief discussion on group by in MySQL

Table of contents 1. Introduction 2. Prepare the ...

CSS3 gradient background compatibility issues

When we make a gradient background color, we will...

Detailed explanation of how to create an array in JavaScript

Table of contents Creating Arrays in JavaScript U...

Solution to the low writing efficiency of AIX mounted NFS

Services provided by NFS Mount: Enable the /usr/s...

Zabbix configuration DingTalk alarm function implementation code

need Configuring DingTalk alarms in Zabbix is ​​s...

Share CSS writing standards and order [recommended for everyone to use]

CSS writing order 1. Position attributes (positio...

Steps to install MySQL using Docker under Linux

As a tester, you may often need to install some s...

Sample code for separating the front-end and back-end using FastApi+Vue+LayUI

Table of contents Preface Project Design rear end...

Vue uses element-ui to implement menu navigation

This article shares the specific code of Vue usin...