Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the problems of js algorithm

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you what is the content of the js algorithm. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Bubbling sort

Compare adjacent elements. If the first is bigger than the second, exchange the two of them.

Do the same for each pair of adjacent elements, from the first pair to the last pair. At this point, the last element should be the largest number.

Repeat the above steps for all elements except the last one.

Continue to repeat the above steps for fewer and fewer elements each time, until there are no pairs of numbers to compare.

The best time complexity of bubble sorting is O (n), which is a stable sorting algorithm.

Let arr = [16, 31, 12, 1, 9, 12, 10]

Function bubbleSort (arr) {

Let len = arr.length

For (let I = 0; I

< len - 1; i++){ for (let j = 0; j < len - 1 - i; j++){ if (arr[j] >

Arr [juni1]) {

[arr [j], arr [j + 1]] = [arr [juni1], arr [j]]

}

}

}

Return arr

}

BubbleSort (arr)

two。 Quick sort

The data to be sorted is divided into two independent parts through a sort, and all the data in one part is smaller than all the data in the other part, and then the two parts of data are sorted quickly according to this method. The whole sorting process can be recursive, so that the whole data becomes an ordered sequence.

Quick sort is not a stable sorting algorithm, that is, the relative position of multiple identical values may change at the end of the algorithm. The average time complexity of quick sort is O (n×log (n)).

Let arr = [16, 31, 12, 1, 9, 12, 10]

Function quickSort (arr) {

If (arr.length {

If (v)

< middle) { left.push(v); } else { right.push(v); } }) return quickSort(left).concat(middle, quickSort(right)); } quickSort(arr1); 3. 不指定算法的数组排序 let arr = [16, 31, 12, 1, 9, 12, 10]; arr.sort((a, b) =>

A-b) / / from small to big

4. Find out the three numbers with the largest product in the integer array

Let unsortedArray = [- 10,7,29,30,5,-10,-70]

/ / there are only two cases in which the product is largest:

/ / 1. The product of the largest three numbers

/ / 2. The product of the largest number and the smallest two numbers.

Function multiply (unSortedArr) {

Let arr = unSortedArr.sort ((a, b) = > a-b)

Let len = arr.length

Let result1 = arr [len-1] * arr [len-2] * arr [len-3]

Let result2 = arr [len-1] * arr [0] * arr [1]

Return result1 > result2? Result1: result2

}

Multiply (unsortedArray)

5. Find the missing number in a continuous array

An unordered array is given, which contains n-1 of n consecutive digits. The upper and lower boundaries are known, and it is required to find out the missing numbers with O (n) complexity.

Const arrayOfIntegers = [2, 5, 1, 4, 9, 6, 3,7]

Const upperBound = 9

Const lowerBound = 1

Function findMissingNumber (arr, upper, lower) {

/ / calculate the sum of the current array

/ / reduce is used here

Const sumOfArr = arr.reduce ((pre, cur) = > pre + cur, 0)

/ / calculate the theoretical array sum with the Gaussian summation formula

/ / Gaussian summation formula: [(N * (N + 1)) / 2]-[(M * (M-1)) / 2]

/ / N is the upper boundary and M is the lower boundary

Const theoreticalSum = (upper * (upper + 1)) / 2-(lower * (lower-1)) / 2

Return theoreticalSum-sumOfArr; / / Theory and practice and finding lost numbers

}

FindMissingNumber (arrayOfIntegers, upperBound, lowerBound); / / 8

6. Array deduplication

Given an unordered array, it is required to remove duplicate numbers from the array and return a new unrepeated array.

Const arr = [1, 2,'1, null, undefined, null, undefined]

/ / ES6 Set and Spread operators

Function uniqueArray (arr) {

Return [. New Set (arr)]

}

/ / ES5

Function uniqueArray (arr) {

Return arr.filter ((v, I) = > arr.indexOf (v) = I)

}

UniqueArray (arr) / / [1, 2,'1, null, undefined]

7. Calculation of the maximum difference of elements in an array

Given an unordered array, find the maximum difference between any two elements. Note that the smaller subscript of the element in the difference calculation must be less than the subscript of the larger element. For example, [7, 8, 4, 9, 9, 15, 3, 1, 10] the calculated value of this array is 11 (15-4) instead of 14 (15-1), because the subscript of 15 is less than 1.

Const arr = [7, 8, 4, 9, 9, 15, 3, 1,10]

Function findLargestDifference (array) {

Const len = array.length

/ / if the array has only one element, return-1 directly

If (len currentMin & & (cur-currentMin > currentMaxDifference)) {

CurrentMaxDifference = cur-currentMin

} else if (cur {

Hashmap [v] = 1

})

Arr2.forEach (v = > {)

If (hashmap [v] = 1) {

IntersectionArray.push (v)

Hashmap [v] + +

}

})

Return intersectionArray

}

Intersection (firstArray, secondArray) / / [1,2]

9. Determination of palindromes

Function isPalindrome (word) {

Return word = = word.split (''). Reverse (). Join ('')

}

IsPalindrome ('racecar') / / true

10. Use two stacks to join and leave the team.

Function Queue () {

This.inputStack = []

This.outputStack = []

}

Queue.prototype.enqueue = function (item) {

Return this.inputStack.push (item)

}

Queue.prototype.dequeue = function () {

If (this.outputStack.length 0)

This.outputStack.push (this.inputStack.pop ())

}

Return this.outputStack.pop ()

}

Const Q = new Queue ()

Q.enqueue (1)

Q.enqueue (2)

Q.dequeue () / / 1

Q.dequeue () / / 2

Q.dequeue () / / undefined

11. Find the number of occurrences of each letter in a string

Function countLetter (s) {

Const result = {}

S.split ('') .forEach (v = > {

If (result [v]) result [v] +

Else result [v] = 1

})

Return result

}

CountLetter ('abaabba') / / {a: 4, b: 3}

twelve。 Prototype chain, prototype inheritance

Function O (name) {

This.name = name

O.prototype.countmate +

}

O.prototype.count = 0

Const a = new O ('a')

A.name / /'a'

A.count / / 1

Const b = new O ('b')

B.name / /'b'

B.count / / 2

A.count / / 2

Thank you for reading! This is the end of this article on "what are the js algorithm problems?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report