返回最小数
function minn(numbers){
let i=0;
let a=numbers;
min=[];
min[0]=a[0];
for(i=0;i<a.length;i++){
if(min[0]>a[i]){min[0]=a[i];}
else{}}
return console.log(numbers+'数组的最小值是'+min[0])
}
第二种方法
let min = (numbers) => {
if(numbers.length > 2){
return min(
[numbers[0], min(numbers.slice(1))]
)
}else{
return Math.min.apply(null, numbers)
}
}
从小到大排序
function mins(numbers){
let i=0;
let a=numbers;
min=[];
min[0]=a[0];
for(i=0;i<a.length;i++){
if(min[0]>a[i]){min[0]=a[i];}
else{}}
return min[0]
}
function remove (numbers,val) {
var index = indexOf(numbers,val);
if (index > -1) {
numbers.splice(index, 1);
}
return numbers;
}
function indexOf(numbers,val) {
let numbers1=numbers;
let val1=val;
let i=0;
for ( i = 0; i < numbers1.length; i++) {
if (numbers1[i] == val1) return i;
}
return -1;
};
function sort(numbers){
let a=numbers;
let b=[];
let c=[];
let i=0;
let d=numbers.length;
for(i=0;d>i;i++){
c[0]=mins(a);
b.push(c[0]);
indexOf(a,c[0]);
a=remove(a,c);
}
return console.log(b)
}
第二种方法
let min = (numbers) => {
if(numbers.length > 2){
return min(
[numbers[0], min(numbers.slice(1))]
)
}else{
return Math.min.apply(null, numbers)
}
}
let minIndex = (numbers) =>
numbers.indexOf(min(numbers))
let sort = (numbers) => {
if(numbers.length > 2){
let index = minIndex(numbers)
let min = numbers[index]
numbers.splice(index, 1)
return [min].concat(sort(numbers))}else{
return numbers[0]<numbers[1] ? numbers :
numbers.reverse()
}
}