Home Web Front-end JS Tutorial JS Bubble Sort Selection Sort and Insertion Sort Example Analysis

JS Bubble Sort Selection Sort and Insertion Sort Example Analysis

Dec 14, 2017 am 09:25 AM
javascript insert choose

This article mainly introduces the bubble sort, selection sort and insertion sort of 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 who need it can refer to it. I hope Can help everyone.

Bubble sorting:

Compare the sizes of two adjacent numbers in order for the data in the array.

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

Time complexityO(n^2)

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)

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)

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

Related recommendations:

Counting sorting and cardinality implemented by JS Sorting algorithm example_javascript skills

Detailed explanation of sorting algorithm

Detailed explanation of javascript array deduplication and quick sort algorithm examples

The above is the detailed content of JS Bubble Sort Selection Sort and Insertion Sort Example Analysis. 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 Article Tags

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)

How to set lock screen wallpaper on wallpaper engine? How to use wallpaper engine How to set lock screen wallpaper on wallpaper engine? How to use wallpaper engine Mar 13, 2024 pm 08:07 PM

How to set lock screen wallpaper on wallpaper engine? How to use wallpaper engine

Can wallpaper engine be shared among families? Can wallpaper engine be shared among families? Mar 18, 2024 pm 07:28 PM

Can wallpaper engine be shared among families?

iBatis vs. MyBatis: Which one is better for you? iBatis vs. MyBatis: Which one is better for you? Feb 19, 2024 pm 04:38 PM

iBatis vs. MyBatis: Which one is better for you?

Using the MINUS operator in SQL Using the MINUS operator in SQL Feb 18, 2024 pm 04:53 PM

Using the MINUS operator in SQL

Is there any virus when watching wallpaper engine movies? Is there any virus when watching wallpaper engine movies? Mar 18, 2024 pm 07:28 PM

Is there any virus when watching wallpaper engine movies?

In which folder are the wallpapers of wallpaper engine located? In which folder are the wallpapers of wallpaper engine located? Mar 19, 2024 am 08:16 AM

In which folder are the wallpapers of wallpaper engine located?

Does wallpaper engine consume a lot of power? Does wallpaper engine consume a lot of power? Mar 18, 2024 pm 08:30 PM

Does wallpaper engine consume a lot of power?

How to change font size in Microsoft Edge browser - How to change font size in Microsoft Edge browser How to change font size in Microsoft Edge browser - How to change font size in Microsoft Edge browser Mar 04, 2024 pm 05:58 PM

How to change font size in Microsoft Edge browser - How to change font size in Microsoft Edge browser

See all articles