# javascript算法（持更）

#### 快速排序

```let arr = [85, 24, 63, 45, 77, 17, 31, 96, 50]
function quickSort (sortArr) {
if (sortArr.length <= 1) return sortArr // 如果筛选数组只有一个元素就直接return
let currentIndex = Math.round(sortArr.length - 1) // 生成数组随机索引
let currentVal = sortArr.splice(currentIndex, 1)[0] // 找出随机元素
let left = [], // 存放比当前元素小的值
right = [] // 存放比当前元素大的值
for (let i = 0;i < sortArr.length; i++) {
if (sortArr[i] > currentVal) {
right.push(sortArr[i])
} else {
left.push(sortArr[i])
}
}
//
return quickSort(left).concat([currentVal], quickSort(right))
}
let result = quickSort(arr)```

#### 冒泡排序

```function sort (sortArr) {
for (let i = 0; i < sortArr.length; i++) {
for (let j = i + 1; j < sortArr.length; j++) {
if (sortArr[i] > sortArr[j]) {
let temp = sortArr[i]
sortArr[i] = sortArr[j]
sortArr[j] = temp
}
}
}
return sortArr
}
let arr = [85, 24, 63, 45, 77, 17, 31, 96, 50]
let result = sort(arr)```

#### 找出数组中最大的数

```function findMax(arr) {
let maxVal = 0
arr.forEach(element => {
if (element > maxVal) {
maxVal = element
}
})
return maxVal
}
let arr = [85, 24, 63, 45, 77, 17, 31, 96, 50]
let result = findMax(arr)```

#### 翻转字符串

```function reverseString(str) {
return str.split('').reverse().join('')
}
let strings = '来吧来吧相约一九九八'
let result = reverseString(strings)```