Blogger Information
Blog 77
fans 0
comment 2
visits 55682
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
一个返回数组最小数字的函数和选择排序
南瓜又个梦
Original
699 people have browsed it

返回最小数

  1. function minn(numbers){
  2. let i=0;
  3. let a=numbers;
  4. min=[];
  5. min[0]=a[0];
  6. for(i=0;i<a.length;i++){
  7. if(min[0]>a[i]){min[0]=a[i];}
  8. else{}}
  9. return console.log(numbers+'数组的最小值是'+min[0])
  10. }

第二种方法

  1. let min = (numbers) => {
  2. if(numbers.length > 2){
  3. return min(
  4. [numbers[0], min(numbers.slice(1))]
  5. )
  6. }else{
  7. return Math.min.apply(null, numbers)
  8. }
  9. }

从小到大排序

  1. function mins(numbers){
  2. let i=0;
  3. let a=numbers;
  4. min=[];
  5. min[0]=a[0];
  6. for(i=0;i<a.length;i++){
  7. if(min[0]>a[i]){min[0]=a[i];}
  8. else{}}
  9. return min[0]
  10. }
  11. function remove (numbers,val) {
  12. var index = indexOf(numbers,val);
  13. if (index > -1) {
  14. numbers.splice(index, 1);
  15. }
  16. return numbers;
  17. }
  18. function indexOf(numbers,val) {
  19. let numbers1=numbers;
  20. let val1=val;
  21. let i=0;
  22. for ( i = 0; i < numbers1.length; i++) {
  23. if (numbers1[i] == val1) return i;
  24. }
  25. return -1;
  26. };
  27. function sort(numbers){
  28. let a=numbers;
  29. let b=[];
  30. let c=[];
  31. let i=0;
  32. let d=numbers.length;
  33. for(i=0;d>i;i++){
  34. c[0]=mins(a);
  35. b.push(c[0]);
  36. indexOf(a,c[0]);
  37. a=remove(a,c);
  38. }
  39. return console.log(b)
  40. }

第二种方法

  1. let min = (numbers) => {
  2. if(numbers.length > 2){
  3. return min(
  4. [numbers[0], min(numbers.slice(1))]
  5. )
  6. }else{
  7. return Math.min.apply(null, numbers)
  8. }
  9. }
  10. let minIndex = (numbers) =>
  11. numbers.indexOf(min(numbers))
  12. let sort = (numbers) => {
  13. if(numbers.length > 2){
  14. let index = minIndex(numbers)
  15. let min = numbers[index]
  16. numbers.splice(index, 1)
  17. return [min].concat(sort(numbers))}else{
  18. return numbers[0]<numbers[1] ? numbers :
  19. numbers.reverse()
  20. }
  21. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post