快速排序 X 数组对象的应用 以下是某班级一次考试的成绩表。请计算每个学生总成绩,并按总成绩排名。找出各单科成绩第一名,输出其成绩与学号

以下是某班级一次考试的成绩表。请计算每个学生总成绩,并按总成绩排名。找出各单科成绩第一名,输出其成绩与学号
var scoreArr = [{
sno: 1,
chinese: 105,
math: 62,
english: 118
},
{
sno: 2,
chinese: 89,
math: 78,
english: 120
},
{
sno: 3,
chinese: 86,
math: 64,
english: 80
},
{
sno: 4,
chinese: 78,
math: 99,
english: 91
},
{
sno: 5,
chinese: 107.5,
math: 97,
english: 70
},
{
sno: 6,
chinese: 112,
math: 61,
english: 92
},
{
sno: 7,
chinese: 101,
math: 79,
english: 104
},
{
sno: 8,
chinese: 71,
math: 72,
english: 105
},
{
sno: 9,
chinese: 56,
math: 68,
english: 61
},
{
sno: 10,
chinese: 98,
math: 83,
english: 77
}]

           const scoreArr = [
        {
          sno: 1,
          chinese: 105,
          math: 62,
          english: 118,
        },
        {
          sno: 2,
          chinese: 89,
          math: 78,
          english: 120,
        },
        {
          sno: 3,
          chinese: 86,
          math: 64,
          english: 80,
        },
        {
          sno: 4,
          chinese: 78,
          math: 99,
          english: 91,
        },
        {
          sno: 5,
          chinese: 107.5,
          math: 97,
          english: 70,
        },
        {
          sno: 6,
          chinese: 112,
          math: 61,
          english: 92,
        },
        {
          sno: 7,
          chinese: 101,
          math: 79,
          english: 104,
        },
        {
          sno: 8,
          chinese: 71,
          math: 72,
          english: 105,
        },
        {
          sno: 9,
          chinese: 56,
          math: 68,
          english: 61,
        },
        {
          sno: 10,
          chinese: 98,
          math: 83,
          english: 77,
        },
      ];
      let sum,
        maxChinese = 0,
        maxChineseSno,
        maxMath = 0,
        maxMathSno,
        maxEnglish = 0,
        maxEnglishSno;
      // 快速排序时间复杂度O(nlogn)空间复杂度O(1)
      function quickSort(arr) {
        if (arr.length <= 1) return arr;
        const center = Math.trunc(arr.length / 2);
        const temp = arr.splice(center, 1);
        let left = [];
        let right = [];
        const length = arr.length;
        const temp2 = temp[0].chinese + temp[0].math + temp[0].english;
        for (let i = 0; i < length; i++) {
          const sum = arr[i].chinese + arr[i].math + arr[i].english;
          if (sum < temp2) left.push(arr[i]);
          else right.push(arr[i]);
        }
        const newArr = quickSort(left).concat(temp, quickSort(right));
        return newArr;
      }
      console.log(quickSort(scoreArr));
      // for循环时间复杂度O(n)
      for (let i = 0; i < scoreArr.length; i++) {
        sum = scoreArr[i].chinese + scoreArr[i].math + scoreArr[i].english;
        if (scoreArr[i].chinese > maxChinese) {
          maxChinese = scoreArr[i].chinese;
          maxChineseSno = scoreArr[i].sno;
        }
        if (scoreArr[i].math > maxMath) {
          maxMath = scoreArr[i].math;
          maxMathSno = scoreArr[i].sno;
        }
        if (scoreArr[i].english > maxEnglish) {
          maxEnglish = scoreArr[i].english;
          maxEnglishSno = scoreArr[i].sno;
        }
      }
      console.log('\n' + '语文单科成绩最高的是' + maxChinese + '学号为:' + maxChineseSno);
      console.log('数学单科成绩最高的是' + maxMath + '学号为:' + maxMathSno);
      console.log('英语单科成绩最高的是' + maxEnglish + '学号为:' + maxEnglishSno);

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值