mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
style: Fixed most styles (according to standardjs)
This commit is contained in:
@ -28,12 +28,12 @@ function introsort (array, compare) {
|
||||
* 0 if a is equal to b
|
||||
* 1 if a greater than b
|
||||
*/
|
||||
var defaultComparator = function (x, y) {
|
||||
const defaultComparator = function (x, y) {
|
||||
if (x === undefined && y === undefined) return 0
|
||||
if (x === undefined) return 1
|
||||
if (y === undefined) return -1
|
||||
var xString = toString(x)
|
||||
var yString = toString(y)
|
||||
const xString = toString(x)
|
||||
const yString = toString(y)
|
||||
if (xString < yString) return -1
|
||||
if (xString > yString) return 1
|
||||
return 0
|
||||
@ -75,8 +75,8 @@ function introsort (array, compare) {
|
||||
* [IIFE](https://en.wikipedia.org/wiki/Immediately_invoked_function_expression)
|
||||
*/
|
||||
return (function (array, comparator) {
|
||||
var swap = function (index1, index2) {
|
||||
var temp = array[index1]
|
||||
const swap = function (index1, index2) {
|
||||
const temp = array[index1]
|
||||
array[index1] = array[index2]
|
||||
array[index2] = temp
|
||||
}
|
||||
@ -85,14 +85,14 @@ function introsort (array, compare) {
|
||||
* If the length of array is less than
|
||||
* this then we simply perform insertion sort
|
||||
*/
|
||||
var THRESHOLD = 16
|
||||
const THRESHOLD = 16
|
||||
/**
|
||||
* @constant TUNEMAXDEPTH
|
||||
* Constant usec to increase or decrease value
|
||||
* of maxDepth
|
||||
*/
|
||||
var TUNEMAXDEPTH = 1
|
||||
var len = array.length
|
||||
const TUNEMAXDEPTH = 1
|
||||
const len = array.length
|
||||
/**
|
||||
* Return if array is only of length 1
|
||||
* Array of size 1 is always sorted
|
||||
@ -104,7 +104,7 @@ function introsort (array, compare) {
|
||||
* Calculate maxDepth = log2(len)
|
||||
* Taken from implementation in stdc++
|
||||
*/
|
||||
var maxDepth = Math.floor(Math.log2(len)) * TUNEMAXDEPTH
|
||||
const maxDepth = Math.floor(Math.log2(len)) * TUNEMAXDEPTH
|
||||
/**
|
||||
* The very first call to quicksort
|
||||
* this initiates sort routine
|
||||
@ -133,7 +133,7 @@ function introsort (array, compare) {
|
||||
heapSort(start, last)
|
||||
return
|
||||
}
|
||||
var pivot = (last + start) >> 1
|
||||
let pivot = (last + start) >> 1
|
||||
pivot = partition(start, last, pivot)
|
||||
quickSort(start, pivot, depth - 1)
|
||||
quickSort(pivot + 1, last, depth - 1)
|
||||
@ -148,8 +148,8 @@ function introsort (array, compare) {
|
||||
function partition (start, last, pivot) {
|
||||
swap(start, pivot)
|
||||
pivot = start
|
||||
var lo = start
|
||||
var hi = last
|
||||
let lo = start
|
||||
let hi = last
|
||||
while (true) {
|
||||
lo++
|
||||
while (comparator(array[lo], array[pivot]) <= 0 && lo !== last) {
|
||||
@ -175,7 +175,7 @@ function introsort (array, compare) {
|
||||
* @param {Number} last one more than last index of array to be sorted
|
||||
*/
|
||||
function insertionSort (start, last) {
|
||||
var i, j
|
||||
let i, j
|
||||
for (i = start + 1; i < last; i++) {
|
||||
j = i - 1
|
||||
while (j >= 0 && comparator(array[j], array[j + 1]) > 0) {
|
||||
@ -192,7 +192,7 @@ function introsort (array, compare) {
|
||||
* @param {Number} last one more than last index of array to be sorted
|
||||
*/
|
||||
function heapSort (start, last) {
|
||||
var x = (last + start) >> 1
|
||||
let x = (last + start) >> 1
|
||||
while (x - start >= 0) {
|
||||
heapify(x, start, last)
|
||||
x--
|
||||
@ -211,8 +211,8 @@ function introsort (array, compare) {
|
||||
* @param {Number} last one more than last index of segment that cur belongs to
|
||||
*/
|
||||
function heapify (cur, start, last) {
|
||||
var size = last - start
|
||||
var max, lt, rt
|
||||
const size = last - start
|
||||
let max, lt, rt
|
||||
cur = cur - start
|
||||
while (true) {
|
||||
max = cur
|
||||
@ -250,9 +250,9 @@ function introsort (array, compare) {
|
||||
(function demo () {
|
||||
const data = []
|
||||
const size = 1000000
|
||||
var i = 0
|
||||
var temp
|
||||
var c = function (a, b) {
|
||||
let i = 0
|
||||
let temp
|
||||
const c = function (a, b) {
|
||||
return a - b
|
||||
}
|
||||
for (i = 0; i < size; i++) {
|
||||
@ -260,7 +260,7 @@ function introsort (array, compare) {
|
||||
data.push(temp)
|
||||
}
|
||||
introsort(data, c)
|
||||
var faulty = false
|
||||
let faulty = false
|
||||
for (i = 1; i < size; i++) {
|
||||
if (data[i] < data[i - 1]) {
|
||||
faulty = true
|
||||
@ -283,8 +283,8 @@ function introsort (array, compare) {
|
||||
const data = []
|
||||
const data2 = []
|
||||
const size = 1000000
|
||||
var i = 0
|
||||
var temp
|
||||
let i = 0
|
||||
let temp
|
||||
for (i = 0; i < size; i++) {
|
||||
temp = Math.random() * Number.MAX_SAFE_INTEGER
|
||||
data.push(temp)
|
||||
@ -292,7 +292,7 @@ function introsort (array, compare) {
|
||||
}
|
||||
introsort(data)
|
||||
data2.sort()
|
||||
var faulty = false
|
||||
let faulty = false
|
||||
for (i = 1; i < size; i++) {
|
||||
if (data[i] !== data2[i]) {
|
||||
faulty = true
|
||||
|
Reference in New Issue
Block a user