配列の次元を指定された順序で並べ替え
const permute = str => {
if (str.length === 1) {
return [str]
};
let arr = str.split('');
let result = [];
for (let i = 0; i < arr.length; i++) {
rArr = arr.filter(item => item !== arr[i]);
let nArr = permute(rArr.join(''));
result = result.concat(nArr.map(item => arr[i] + item));
}
return result;
}
console.log(permute('hello'));
数字のみが含まれた配列要素のクイックソート
const qSort = array => {
if (array.length <= 1) return array
let mi = Math.floor(array.length / 2)
let m = array.splice(mi, 1)[0]
let left = []
let right = []
for (let i = 0; i < array.length; i++) {
array[i] < m ? left.push(array[i]) : right.push(array[i])
}
return [...qSort(left), m, ...qSort(right)]
}
console.log(qSort([0,-5,10,-8,8]));