按正态分布(高斯分布)对数字数组进行排序
Sort array of numbers by normal distribution (gaussian distribution)
有一个数字数组 setOfNumbers = [0, 3, 3, 2, 7, 1, -2, 9]
我想对这个集合进行排序,使最小的数字位于末尾和开头,最大的位于排序集合的中心 sortedSetNumbers = [0, 2, 3, 9, 7, 3, 1, -2]
.
const setOfNumbers = [0, 3, 3, 2, 7, 1, -2, 9];
const result = [0, 2, 3, 9, 7, 3, 1, -2];
function sortNormal(a, b) {
return true; // Please, change this line
}
const sortedSetNumbers = setOfNumbers.sort((a, b) => sortNormal(a, b));
if (sortedSetNumbers === result) {
console.info('Succeeded Normal Distributed');
} else {
console.warn('Failed Normal Distribution');
}
console.log(sortedSetNumbers);
我确定可以使用 Array.prototype.sort()
方法对这些数字进行排序,但是这个排序函数应该是什么样子?
编辑: 解决方案不必用 .sort()
解决。那只是一个想法。
这可能是最幼稚的做法了,但是排序后不就是简单的左,右,左,右...吗?
const input = [0, 3, 3, 2, 7, 1, -2, 9];
const expected = [0, 2, 3, 9, 7, 3, 1, -2];
const sorted = input.slice().sort();
const output = [];
let side = true;
while (sorted.length) {
output[side ? 'unshift' : 'push'](sorted.pop());
side = !side;
}
console.log(expected.join());
console.log(output.join());
或者简单地说:
const input = [0, 3, 3, 2, 7, 1, -2, 9];
const output = input.slice().sort().reduceRight((acc, val, i) => {
return i % 2 === 0 ? [...acc, val] : [val, ...acc];
}, []);
console.log(output.join());
这个解决方案不是很优雅,但它确实起作用了。
const setOfNumbers = [0, 3, 3, 2, 7, 1, -2, 9];
const alternation = alternate();
const sortedSetNumbers = sortNormal(setOfNumbers);
function sortNormal(start) {
const result = [];
const interim = start.sort((a, b) => {
return b - a;
});
interim.map(n => {
if (alternation.next().value) {
result.splice(0, 0, n);
} else {
result.splice(result.length, 0, n);
}
});
return result;
}
function* alternate() {
let i = true;
while (true) {
yield i;
i = !i;
}
}
console.log(sortedSetNumbers);
稍微不同的方法是对数组进行升序排序。
获取另一个索引数组,并将赔率排序为前半部分递增,偶数排序到末尾倒置蝴蝶随机播放。
然后通过获取排序索引的值来映射排序数组。
[-2, 0, 1, 2, 3, 3, 7, 9] // sorted array
[ 1, 3, 5, 7, 6, 4, 2, 0] // sorted indices
[ 0, 2, 3, 9, 7, 3, 1, -2] // rebuild sorted array
var array = [0, 3, 3, 2, 7, 1, -2, 9].sort((a, b) => a - b);
array = Array
.from(array, (_, i) => i)
.sort((a, b) => b % 2 - a % 2 || (a % 2 ? a - b : b - a))
.map(i => array[i]);
console.log(array);
有一个数字数组 setOfNumbers = [0, 3, 3, 2, 7, 1, -2, 9]
我想对这个集合进行排序,使最小的数字位于末尾和开头,最大的位于排序集合的中心 sortedSetNumbers = [0, 2, 3, 9, 7, 3, 1, -2]
.
const setOfNumbers = [0, 3, 3, 2, 7, 1, -2, 9];
const result = [0, 2, 3, 9, 7, 3, 1, -2];
function sortNormal(a, b) {
return true; // Please, change this line
}
const sortedSetNumbers = setOfNumbers.sort((a, b) => sortNormal(a, b));
if (sortedSetNumbers === result) {
console.info('Succeeded Normal Distributed');
} else {
console.warn('Failed Normal Distribution');
}
console.log(sortedSetNumbers);
我确定可以使用 Array.prototype.sort()
方法对这些数字进行排序,但是这个排序函数应该是什么样子?
编辑: 解决方案不必用 .sort()
解决。那只是一个想法。
这可能是最幼稚的做法了,但是排序后不就是简单的左,右,左,右...吗?
const input = [0, 3, 3, 2, 7, 1, -2, 9];
const expected = [0, 2, 3, 9, 7, 3, 1, -2];
const sorted = input.slice().sort();
const output = [];
let side = true;
while (sorted.length) {
output[side ? 'unshift' : 'push'](sorted.pop());
side = !side;
}
console.log(expected.join());
console.log(output.join());
或者简单地说:
const input = [0, 3, 3, 2, 7, 1, -2, 9];
const output = input.slice().sort().reduceRight((acc, val, i) => {
return i % 2 === 0 ? [...acc, val] : [val, ...acc];
}, []);
console.log(output.join());
这个解决方案不是很优雅,但它确实起作用了。
const setOfNumbers = [0, 3, 3, 2, 7, 1, -2, 9];
const alternation = alternate();
const sortedSetNumbers = sortNormal(setOfNumbers);
function sortNormal(start) {
const result = [];
const interim = start.sort((a, b) => {
return b - a;
});
interim.map(n => {
if (alternation.next().value) {
result.splice(0, 0, n);
} else {
result.splice(result.length, 0, n);
}
});
return result;
}
function* alternate() {
let i = true;
while (true) {
yield i;
i = !i;
}
}
console.log(sortedSetNumbers);
稍微不同的方法是对数组进行升序排序。
获取另一个索引数组,并将赔率排序为前半部分递增,偶数排序到末尾倒置蝴蝶随机播放。
然后通过获取排序索引的值来映射排序数组。
[-2, 0, 1, 2, 3, 3, 7, 9] // sorted array [ 1, 3, 5, 7, 6, 4, 2, 0] // sorted indices [ 0, 2, 3, 9, 7, 3, 1, -2] // rebuild sorted array
var array = [0, 3, 3, 2, 7, 1, -2, 9].sort((a, b) => a - b);
array = Array
.from(array, (_, i) => i)
.sort((a, b) => b % 2 - a % 2 || (a % 2 ? a - b : b - a))
.map(i => array[i]);
console.log(array);