mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 09:28:26 +08:00
sort/
This commit is contained in:
@ -2,6 +2,8 @@
|
|||||||
* A simple helper function that checks, if the array is
|
* A simple helper function that checks, if the array is
|
||||||
* sorted in ascending order.
|
* sorted in ascending order.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* eslint no-extend-native: ["off", { "exceptions": ["Object"] }] */
|
||||||
Array.prototype.isSorted = function () {
|
Array.prototype.isSorted = function () {
|
||||||
const length = this.length
|
const length = this.length
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
/*
|
/*
|
||||||
* Build a max heap out of the array. A heap is a specialized tree like
|
* Build a max heap out of the array. A heap is a specialized tree like
|
||||||
* data structure that satisfies the heap property. The heap property
|
* data structure that satisfies the heap property. The heap property
|
||||||
* for max heap is the following: "if P is a parent node of C, then the
|
* for max heap is the following: "if P is a parent node of C, then the
|
||||||
* key (the value) of node P is greater than the key of node C"
|
* key (the value) of node P is greater than the key of node C"
|
||||||
* Source: https://en.wikipedia.org/wiki/Heap_(data_structure)
|
* Source: https://en.wikipedia.org/wiki/Heap_(data_structure)
|
||||||
*/
|
*/
|
||||||
|
/* eslint no-extend-native: ["off", { "exceptions": ["Object"] }] */
|
||||||
Array.prototype.heapify = function (index, heapSize) {
|
Array.prototype.heapify = function (index, heapSize) {
|
||||||
let largest = index
|
let largest = index
|
||||||
const leftIndex = 2 * index + 1
|
const leftIndex = 2 * index + 1
|
||||||
@ -29,10 +29,10 @@ Array.prototype.heapify = function (index, heapSize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Heap sort sorts an array by building a heap from the array and
|
* Heap sort sorts an array by building a heap from the array and
|
||||||
* utilizing the heap property.
|
* utilizing the heap property.
|
||||||
* For more information see: https://en.wikipedia.org/wiki/Heapsort
|
* For more information see: https://en.wikipedia.org/wiki/Heapsort
|
||||||
*/
|
*/
|
||||||
function heapSort (items) {
|
function heapSort (items) {
|
||||||
const length = items.length
|
const length = items.length
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* eslint no-extend-native: ["off", { "exceptions": ["Object"] }] */
|
||||||
Array.prototype.wiggleSort = function () {
|
Array.prototype.wiggleSort = function () {
|
||||||
for (let i = 0; i < this.length; ++i) {
|
for (let i = 0; i < this.length; ++i) {
|
||||||
const shouldNotBeLessThan = i % 2
|
const shouldNotBeLessThan = i % 2
|
||||||
|
Reference in New Issue
Block a user