search
HomeWeb Front-endJS TutorialSummary of JS array sorting skills (bubble, sort, fast, Hill, etc. sorting)_javascript skills

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;
}

② js uses sort to sort

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

③ 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; 
}

④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;
}

⑤ 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; 
}

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

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
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
Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

See all articles

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 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),