An array that increases first and then decreases, how to sort it is the simplest.

for example

var arr = [1, 3, 6, 10, 2, 7, 9];

after sorting

var arr = [1, 2, 3, 6, 7, 9, 10];

! In particular, I wrote it correctly! It"s not what you think. Increase first and then decrease! Those who take it for granted and step on other people"s questions are afraid that they are not doing too many conventional questions!

I met this when I went out for an interview, and that person specially stressed that it was not an increase and then a decrease!

! You can"t write

in a pure native method instead of using the sort method.
Mar.09,2021

your example is wrong, shouldn't it be

var arr = [1, 3, 6, 10, 9, 7, 2];

in this case, maintain two pointers, one from behind, one from back to front, who is in a small row, the two pointers are all lined up.


let arr = [1, 3, 6, 10, 9, 7, 2];
function trans (arr) {
  let i = 0
  let j = arr.length - 1
  let result = []
  while (i <= j) {
    if (arr[i] <= arr[j]) {
      result.push(arr[iPP])
    } else {
      result.push(arr[j--])
    }
  }
  return result
}
trans(arr)

if you determine the array that increases first and then decreases, the time complexity of merging directly from head to tail is o (n)

ideas

let i=0
let j=arr.length-1
let newArr = []
if(arr[i]<arr[j]){
    newArr.push(arr[iPP])
}else{
    newArr.push(arr[j--])
}

add a loop and you can arrange it

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea1ffa-69311.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea1ffa-69311.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?