如何使用 swap 方法从最高到最低对数组进行排序

本文关键字:数组 排序 swap 何使用 方法 | 更新日期: 2023-09-27 18:33:39

我需要找一个从高到低对名字和分数进行排序。我不确定如何使用交换方法,如果有人可以帮助我指路,我会很高兴。另外,有没有办法显示分数,使它们对齐得更整齐一些?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace proj09LEA
{
    class Program
    {
        static void Main(string[] args)
        {
            // declare and array of integers          
            string[] name = new string[5];
            int[] score = new int[5];
            Console.WriteLine("'nSaturday Coder's Bowling Team");
            Console.WriteLine("Enter in a name and score for each person on the team.");
            Console.WriteLine("For example, Mary 143. Just hit Enter when you are done.'n");
            // fill an array with user input
            for (int i = 0; i < score.Length; i++)
            {
                Console.WriteLine("Enter in a name and score: ");
                //save the name and score as a string
                string line = Console.ReadLine();
                //split the name and score
                name[i] = line.Substring(0, line.IndexOf(' '));
                score[i] = int.Parse(line.Substring(line.IndexOf(' ') + 1));
            }
            Console.WriteLine("------------ Input Complete ------------'n");
            Console.WriteLine("Here are the scores for this game, from highest to lowest:'n");
            for (int i = 0; i < score.Length; i++)
            {
                if (score[i] >= 300)
                {
                    Console.WriteLine("{0}     {1}*.", name[i], score[i]);
                }
                else
                {
                    Console.WriteLine("{0}     {1}", name[i], score[i]);
                }
            }
            //display the average score in the program 
            AverageScore(score);
            Console.WriteLine("Press Enter to continue. . .");
            //end program 
            Console.ReadLine();
        }
        // find the average score from the score array
        static void AverageScore(int[] score)
        {
            int sum = score.Sum();
            int average = sum / score.Length;
            Console.WriteLine("'nThe average score for this game was {0:D}.'n", average);
        }
    }
}

如何使用 swap 方法从最高到最低对数组进行排序

不要为你的姓名和分数保留单独的数组。 创建具有"名称和分数"属性的类。 在类中实现 IComparable,您可以在类上使用内置的 List.Sort。

public class BowlingScore : IComparable<BowlingScore>
{
    private int _score = 0;
    public string Name { get; set; }
    public bool IsPerfectGame { get; protected set; }
    public int Score
    {
        get { return this._score; }
        set { this._score = value; this.IsPerfectGame = value == 300; }
    }
    public override string ToString()
    {
        if (this.IsPerfectGame)
        {
            return string.Format("{0}'s score was *{1}", this.Name, this.Score);
        }
        else
        {
            return string.Format("{0}'s score was {1}", this.Name, this.Score);
        }
    }
    public int CompareTo(BowlingScore other)
    {
        if (other == null) return 1;
        return -1 * this.Score.CompareTo(other.Score);
    }

此代码将从控制台收集名称和分数,从高到低排序并打印出分数并打印出高、低和平均分数:

        List<BowlingScore> scores = new List<BowlingScore>(5);
        for (int index = 0; index < 5; index++)
        {
            Console.WriteLine("Enter in a name and score: ");
            string line = Console.ReadLine();
            BowlingScore bowlingScore = new BowlingScore();
            bowlingScore.Name = line.Substring(0, line.IndexOf(' '));
            bowlingScore.Score = int.Parse(line.Substring(line.IndexOf(' ') + 1));
            scores.Add(bowlingScore);
        }
        Console.WriteLine();
        Console.WriteLine("Here are the Scores:");
        scores.Sort();
        foreach (BowlingScore score in scores)
        {
            Console.WriteLine(score);
        }
        Console.WriteLine();
        HighScores(scores);
        Console.WriteLine();
        LowScores(scores);
        Console.WriteLine();
        Console.WriteLine(string.Format("Average score:{0}", scores.Average(f => f.Score)));
        Console.WriteLine();
        Console.WriteLine("Press any key...");
        Console.ReadKey();
    static void LowScores(List<BowlingScore> scores)
    {
        int highScore = scores.ElementAt(scores.Count - 1).Score;
        for (int index = scores.Count - 1; index > 0; index--)
        {
            BowlingScore bowlingScore = scores.ElementAt(index);
            if (bowlingScore.Score == highScore)
            {
                Console.WriteLine(string.Format("Low Score: {0} {1}", bowlingScore.Name, bowlingScore.Score));
            }
            else
            {
                break;
            }
        }
    }
    static void HighScores(List<BowlingScore> scores)
    {
        int lowScore = scores.ElementAt(0).Score;
        for (int index = 0; index < scores.Count; index++)
        {
            BowlingScore bowlingScore = scores.ElementAt(index);
            if (bowlingScore.Score == lowScore)
            {
                Console.WriteLine(string.Format("High Score: {0} {1}", bowlingScore.Name, bowlingScore.Score));
            }
            else
            {
                break;
            }
        }
    }