


Simple implementation of several sorting algorithms in JavaScript_Basic knowledge
Implementation of sorting algorithm
My JS skills are poor, so I use a method similar to JAVA and C to write the JavaScript sorting algorithm.
And I won’t talk about the algorithm principle here, just the code implementation, there may be bugs, everyone is welcome to comment on the blog for guidance.
Insertion sort
The algorithm description of Insertion-Sort is a simple and intuitive sorting algorithm. It works by constructing an ordered sequence. For unsorted data, it scans from back to front in the sorted sequence, finds the corresponding position and inserts it. In the implementation of insertion sort, in-place sorting is usually used (that is, sorting that only uses O(1) extra space). Therefore, during the scanning process from back to front, it is necessary to repeatedly and gradually shift the sorted elements backward. , providing insertion space for the latest element.
The implementation code is as follows:
function insertSort(arr) { if (!arr) return; var len = arr.length; if (len == 0 || len == 1) return; for (var i = 1, len = arr.length; i < len; i ++) { var stand = arr[i]; for (var j = i - 1; j >= 0; j --) { if (arr[j] > stand) { arr[j + 1] = arr[j]; } else { arr[j + 1] = stand; break; } } } return arr; }
Time complexity is: O(n^2)
Of course, there is room for optimization of this algorithm, such as changing the positional algorithm of search and replacement to binary search.
Bubble sort
Classic sorting algorithm, I feel heartbroken when it comes to bubble sort. When I was an undergraduate, I wrote a required thesis on the improvement of the bubble sort algorithm. As a result, I couldn't fully implement the bubble sort algorithm after I finished writing the thesis. It was so embarrassing.
if (!arr) return; var len = arr.length; if (len == 0 || len == 1) return; for (var i = 0; i < len; i ++) { for (var j = 0; j < len - i - 1; j ++) { if (arr[j] > arr[j + 1]) { var tmp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = tmp; } } } return arr; }
The time complexity is: O(n^2)
Quick Sort
A very classic sorting algorithm. The sorting process is mainly divided into three steps:
- Pick an element from the sequence, called the “pivot”;
- Reorder the sequence so that all elements smaller than the baseline value are placed in front of the baseline, and all elements larger than the baseline value are placed behind the baseline (the same number can be placed on either side). After this partition exits, the base is in the middle of the sequence. This is called a partition operation;
- Recursively sort the subarray of elements smaller than the base value and the subarray of elements greater than the base value.
The implementation code is as follows:
function quickSort(arr, bt, ed) { if (bt < ed) { var pivot = findPartition(arr, bt, ed); quickSort(arr, bt, pivot - 1); quickSort(arr, pivot + 1, ed); } } function findPartition(arr, bt, ed) { var stand = arr[bt]; while (bt < ed) { while (bt < ed && arr[ed] >= stand) { ed --; } if (bt < ed) { arr[bt ++] = arr[ed]; } while (bt < ed && arr[bt] <= stand) { bt ++; } if (bt < ed) { arr[ed --] = arr[bt]; } } arr[bt] = stand; return bt; }
The time complexity is: O(nlogn).
Merge sort
It is also a very classic sorting algorithm. I took the opportunity of learning js to review the classic sorting algorithm. For ideas on merge sort, you can refer to my blog: Merge Sort. I only write js implementation here.
function mergeSort(arr, bt, ed) { if (bt < ed) { var mid = bt + parseInt((ed - bt) / 2); mergeSort(arr, bt, mid); mergeSort(arr, mid + 1, ed); mergeArray(arr, bt, mid, ed); } } function mergeArray(arr, bt, mid, ed) { var mArr = []; var i = bt, j = mid + 1; while (i <= mid && j <= ed) { if (arr[i] <= arr[j]) { mArr.push(arr[i++]); } else { mArr.push(arr[j ++]); } } if (i <= mid) { mArr = mArr.concat(arr.slice(i, mid + 1)); } if (j <= ed) { mArr = mArr.concat(arr.slice(j, ed + 1)); } for (var h = 0; h < mArr.length; h ++) { arr[bt + h] = mArr[h]; } }
There was another little episode when writing the merge sort: js could not automatically round up, so I later used the parseInt method, which was very cute.

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

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.

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.

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
