Home Web Front-end JS Tutorial Summary of JS array sorting skills (bubble, sort, fast, Hill, etc. sorting)_javascript skills

Summary of JS array sorting skills (bubble, sort, fast, Hill, etc. sorting)_javascript skills

May 16, 2016 pm 03:30 PM
js js array sorting Array sort

The examples in this article summarize JS array sorting techniques. Share it with everyone for your reference, the details are as follows:

① Bubble sort

bubbleSort:function(array){
var i = 0, len = array.length, j, d; for(; i<len; i++){ 
for(j=0; j<len; j++){ 
if(array[i] < array[j]){ 
d = array[j]; array[j] = array[i]; array[i] = d; 
} 
} 
}
return array;
}

Copy after login

② js uses sort to sort

systemSort:function(array)
{ return array.sort(function(a, b)
{ return a - b; }); 
}

Copy after login

③ Quick sort

quickSort:function(array)
{ //var array = [8,4,6,2,7,9,3,5,74,5]; //var array = [0,1,2,44,4,324,5,65,6,6,34,4,5,6,2,43,5,6,62,43,5,1,4,51,56,76,7,7,2,1,45,4,6,7]; 
var i = 0; var j = array.length - 1; var Sort = function(i, j){ 
// 结束条件 
if(i == j ){ return }; var key = array[i]; var tempi = i;
// 记录开始位置 var tempj = j; // 记录结束位置 
while(j > i){ 
// j <<-------------- 向前查找 
if(array[j] >= key){ j--; }
else{ 
array[i] = array[j] //i++ ------------>>向后查找 
while(j > ++i){ if(array[i] > key){ 
array[j] = array[i]; break; 
} 
} 
} 
} // 如果第一个取出的 key 是最小的数 
if(tempi == i){ 
Sort(++i, tempj); return ; 
} 
// 最后一个空位留给 
key array[i] = key; 
// 递归 Sort(tempi, i); 
Sort(j, tempj); } Sort(i, j); 
return array; 
}

Copy after login

④Hill sort

//Jun.array.shellSort(Jun.array.df(10000)); 
shellSort:function(array){
// var array = [13,14,94,33,82,25,59,94,65,23,45,27,73,25,39,10]; 
var tempArr = [1750, 701, 301, 132, 57, 23, 10, 4, 1]; 
// reverse() 在维基上看到这个最优的步长 较小数组 
//var tempArr = [1031612713, 217378076, 45806244, 9651787, 2034035, 428481, 90358, 19001, 4025, 836, 182, 34, 9, 1] 
//针对大数组的步长选择 
var i = 0; var tempArrtempArrLength = tempArr.length; var len = array.length; var len2 = parseInt(len/2); for(;i < tempArrLength; i++){ 
if(tempArr[i] > len2){ continue; } tempSort(tempArr[i]); 
} 
// 排序一个步长 
function tempSort(temp){
//console.log(temp) 使用的步长统计 
var i = 0, j = 0, f, tem, key; 
var tempLen = len%temp > 0 &#63; parseInt(len/temp) + 1 : len/temp; 
for(;i < temp; i++){
// 依次循环列 
for(j=1;/*j < tempLen && */temp * j + i < len; j++){ 
//依次循环每列的每行 
tem = f = temp * j + i; key = array[f]; 
while((tem-=temp) >= 0){ 
// 依次向上查找 
if(array[tem] > key){
array[tem+temp] = array[tem]; 
}else{ break; 
} 
} array[tem + temp ] = key; 
} 
} 
} return array;
}

Copy after login

⑤ Insertion sort

insertSort:function(array){
/ /var array = [0,1,2,44,4,324,5,65,6,6,34,4,5,6,2,43,5,6,62,43,5,1,4,51,56,76,7,7,2,1,45,4,6,7]; 
var i = 1, j, temp, key, len = array.length; 
for(; i < len; i++){ 
temp = j = i; key = array[j]; while(--j > -1){ 
if(array[j] > key){ array[j+1] = array[j]; }else{ break; 
} 
}
array[j+1] = key; 
} return array; 
}

Copy after login

Attachment: Notes on sorting arrays (Array) in js

var arrDemo = new Array();
arrDemo[0] = 10;
arrDemo[1] = 50;
arrDemo[2] = 51;
arrDemo[3] = 100;
arrDemo.sort(); //调用sort方法后,数组本身会被改变,即影响原数组
alert(arrDemo);//10,100,50,51 默认情况下sort方法是按ascii字母顺序排序的,而非我们认为是按数字大小排序
arrDemo.sort(function(a,b){return a>b&#63;1:-1});//从小到大排序
alert(arrDemo);//10,50,51,100
arrDemo.sort(function(a,b){return a<b&#63;1:-1});//从大到小排序
alert(arrDemo);//100,51,50,10

Copy after login

Conclusion:

1. After the array calls the sort method, it will affect itself (rather than generating a new array)
2. The sort() method sorts by characters by default, so when sorting a numeric array, don’t take it for granted that it will be sorted by numerical size!
3. To change the default sort behavior (that is, sort by characters), you can specify the sorting rule function yourself (as shown in this example)

I hope this article will be helpful to everyone in JavaScript programming.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

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

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

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Fast array sorting method that preserves key names in PHP Fast array sorting method that preserves key names in PHP May 02, 2024 pm 03:06 PM

Fast array sorting method in PHP that preserves key names: Use the ksort() function to sort the keys. Use the uasort() function to sort using a user-defined comparison function. Practical case: To sort an array of user IDs and scores by score while retaining the user ID, you can use the uasort() function and a custom comparison function.

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

How to use JS and Baidu Map to implement map click event processing function How to use JS and Baidu Map to implement map click event processing function Nov 21, 2023 am 11:11 AM

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement map polygon drawing function How to use JS and Baidu Maps to implement map polygon drawing function Nov 21, 2023 am 10:53 AM

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file

Custom sorting: Implementation method of sorting using JS array sort() method Custom sorting: Implementation method of sorting using JS array sort() method Dec 28, 2023 am 10:59 AM

How to use JS array sorting: sort() method for custom sorting In JavaScript, arrays are a very common and important data type. When we need to sort the elements in an array, we can use the sort() method of the array. The sort() method sorts the array elements according to the default sorting rules, but sometimes we may need to customize the sorting of the array according to our own needs. This article will introduce in detail how to use the sort() method for custom sorting, and provide specific

See all articles