Home Web Front-end JS Tutorial Summary of JS sorting algorithm

Summary of JS sorting algorithm

Jun 21, 2018 am 11:02 AM
js Bubble Sort Sorting Algorithm insertion sort selection sort

This article mainly introduces the bubble sort, selection sort and insertion sort of the JS sorting algorithm. It analyzes the concepts, principles and implementation methods of bubble sort, selection sort and insertion sort in the form of examples. Friends in need can refer to it. Next

The examples in this article describe the JS sorting algorithms of bubble sort, selection sort and insertion sort. Share it with everyone for your reference. The details are as follows:

Bubble sort:

Compare the data in the array in sequence between two adjacent ones. The size of the number.

If the previous data is greater than the later data, exchange the two numbers.

Time complexityO(n^2)

1

2

3

4

5

6

7

8

9

10

11

12

function bubble(array){

 var temp;

 for(var i=0; i<arr.length; i++){

  for(var j=0; j<arr.length; j++){

   if(arr[j]>arr[j+1]){

    temp = arr[j+1];

    arr[j+1] =arr[j];

    arr[j]=temp;

   }

  }console.log(arr);

 }

}//冒泡排序

Copy after login

Selection sort:

First Select the smallest data from the original array and exchange it with the data at position 1.

Then select the next smallest data from the remaining n-1 data and exchange it with the data at the second position.

Repeat until the last two data are exchanged.

Time complexityO(n^2)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

function selectionSort(array){

 var min,temp;

 for(var i=0; i<array.length-1; i++){

  min=i;

  for(var j=i+1; j<array.length; j++){

   if(array[j]<array[min]){

    min=j;

   }

  }

  swap(array,min,i);

 }

 console.log(array);

}//选择排序

function swap(array,i,j){

 var temp =array[i];

 array[i]=array[j];

 array[j]=temp;

}//两个数字交换

Copy after login

Insertion sort:

First Compare the first two data from small to large.

Then compare the third data with the first two arranged data, and insert the third data into the appropriate position. And so on.

(Insertion sort has two loops. The outer loop moves the arrays one by one, and the inner loop compares the element selected by the outer loop with the number in front of it.)

Time complexity O(n^2)

1

2

3

4

5

6

7

8

9

10

11

12

function insertSort(arr){

 var temp, j;

 for(var i=1; i<arr.length; i++){

  temp =arr[i];

  j=i;

  while(j>0 && arr[j-1]>temp){

   arr[j]=arr[j-1];

   j--;

  }

  arr[j]=temp;

 }

}

Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

The process of encapsulating and submitting data in a form

Recording the occurrence of repeated elements in JavaScript Times

How to implement multiple AJAX requests using JQUERY

How to call vuex to store interface data in vue.js

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

Java data structures and algorithms: in-depth explanation Java data structures and algorithms: in-depth explanation May 08, 2024 pm 10:12 PM

Data structures and algorithms are the basis of Java development. This article deeply explores the key data structures (such as arrays, linked lists, trees, etc.) and algorithms (such as sorting, search, graph algorithms, etc.) in Java. These structures are illustrated through practical examples, including using arrays to store scores, linked lists to manage shopping lists, stacks to implement recursion, queues to synchronize threads, and trees and hash tables for fast search and authentication. Understanding these concepts allows you to write efficient and maintainable Java code.

Transform code with C++ function pointers: improve efficiency and reusability Transform code with C++ function pointers: improve efficiency and reusability Apr 29, 2024 pm 06:45 PM

Function pointer technology can improve code efficiency and reusability, specifically as follows: Improved efficiency: Using function pointers can reduce repeated code and optimize the calling process. Improve reusability: Function pointers allow the use of general functions to process different data, improving program reusability.

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Guide to writing a custom sorting algorithm for PHP arrays Guide to writing a custom sorting algorithm for PHP arrays Apr 27, 2024 pm 06:12 PM

How to write a custom PHP array sorting algorithm? Bubble sort: Sorts an array by comparing and exchanging adjacent elements. Selection sort: Select the smallest or largest element each time and swap it with the current position. Insertion sort: Insert elements one by one into the sorted part.

Use Python to learn the principles and practical application scenarios of selection sorting algorithm Use Python to learn the principles and practical application scenarios of selection sorting algorithm Feb 03, 2024 am 08:26 AM

Learn the basic idea and application of selection sort through Python Selection sort (SelectionSort) is a simple and intuitive sorting algorithm. Its basic idea is to select the smallest (or largest) element from the data to be sorted and put it at the end of the sorted area. , and then select the smallest (or largest) element from the remaining unsorted data and put it at the end of the sorted area, and so on until all the data is sorted. The specific steps of selection sorting are as follows: First, find the smallest (or largest) element from the data to be sorted

See all articles