

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Which algorithm does the JavaScript Array#sort() function use?
Javascript specification doesn't specify a particular algorithm to be used in the Array.sort implementation. This is left on the implementor to decide. So different JS engines use different sorting algorithms.
Mozilla(Spider Monkey JS engine) uses mergeSort. You can see the code written for it in C in the Mozilla repository: https://dxr.mozilla.org/seamonkey/source/js/src/jsarray.c
WebKit(Chrome, Safari, etc) do not directly use a sorting algorithm, instead they choose the algorithm based on element types and length of the array. For example,
Numeric arrays use C++ Std library's quick sort function.
Non-numeric arrays use merge sort.
In some other cases it uses selection sort.
It depends on the datatype and size of array elements which algorithm will be used to sort the array.
- Related Questions & Answers
- How does the MD5 Algorithm works?
- How does the k-means algorithm work?
- How does Secure Hash Algorithm works?
- How does the Lossy Counting algorithm find frequent items?
- What is the use of JavaScript eval function?
- How does recursion function work in JavaScript?
- What does the exclamation mark do before the function in JavaScript?
- The Floyd-Warshall algorithm in Javascript
- What is the use of Math.imul( ) Function in JavaScript?
- What is the use of Math.hypot() function in JavaScript?
- What is the use of apply() function in JavaScript?
- What does a +function() { } notation do in JavaScript?
- Which is the fastest algorithm to find prime numbers using C++?
- PHP string cast vs strval function, which one should I use?
- JavaScript Algorithm - Removing Negatives from the Array