关于#c语言#的成绩排序问题,不太了解冒泡法,请各位专家解答!

img


感觉我已经完全混乱和迷茫了,不太熟悉数据结构和冒泡法,真的会很谢

冒泡就是每次比较两个相邻的元素,如果他们的顺序错误就把他们交换。

  1. 一共进行数组大小-1次的循环
  2. 每一趟排序的次数在逐渐减小
    void BubbleSort(int nums[], int len){
      for(int i = 0; i < len - 1; i++){
         for(int j = 0; j < len - 1 - i; j++){
             if(nums[j] > nums[j + 1]){
                 int tmp = nums[j];
                 nums[j] = nums[j + 1];
                 nums[j + 1] = tmp;
             }
         }
     }
    }
    

https://jingyan.baidu.com/article/49711c61b93bcdfa451b7c6b.html
给你个例子参考一下,有详解

#include<stdio.h>
int main()
{
int a[10],i,j,t,k;
printf("请以次输入10个学生的成绩:");
for(i=0;i<10;i++)scanf("%d",&a[i]);
for(i=0;i<9;i++)
{
k=i;
for(j=i;j<10;j++)
if(a[j]>a[k])k=j;
if(i!=k)
{
t=a[i];
a[i]=a[k];
a[k]=t;
}
}
printf("由高到低排序输出成绩:\n");
for(i=0;i<10;i++)
printf("%d\n",a[i]);
return 0;
}

大概写了一下,和要求并不是完全一致,仅供参考。

#include<stdio.h>
#include<stdlib.h>
#define NUM_COURSES 5
typedef struct
{
    // 学号
    char id[20];
    // 姓名
    char name[32];
    // 5门课程考试成绩
    int score[NUM_COURSES];
    // 平均成绩
    float avg_score;
}student_record_t;

#define NUM_STUDENTS 45
typedef struct
{
    // 一个班有45个学生
    student_record_t students_records[NUM_STUDENTS];
}class_scores_t;

// 有4个班级
#define NUM_CLASSES 4
class_scores_t class_scores[NUM_CLASSES];

// 成绩生成函数,生成全部学生的成绩
void generate_score()
{
    for (int i = 0; i < NUM_CLASSES; ++i)
    {
        for (int j = 0; j < NUM_STUDENTS; ++j)
        {
            for (int k = 0; k < NUM_COURSES; ++k)
            {
                // 每门课程的成绩都是0~100之间的整数
                class_scores[i].students_records[j].score[k] = rand() % 101;
            }
        }
    }
}

// 平均成绩计算函数,计算每个同学的平均成绩
void calculate_average()
{
    for (int i = 0; i < NUM_CLASSES; ++i)
    {
        for (int j = 0; j < NUM_STUDENTS; ++j)
        {
            int sum = 0;
            for (int k = 0; k < NUM_COURSES; ++k)
            {
                sum += class_scores[i].students_records[j].score[k];
            }
            // 将平均成绩存在平均成绩数组中
            class_scores[i].students_records[j].avg_score = ((float)sum) / NUM_COURSES;
        }
    }
}

// 使用冒泡排序对每个同学的成绩按平均成绩进行排序
void bubble_sort()
{
#define NUM_ALL_STUDENTS (NUM_CLASSES * NUM_STUDENTS)
    student_record_t all_students[NUM_CLASSES * NUM_STUDENTS];
    for (int i = 0; i < NUM_CLASSES; ++i)
    {
        for (int j = 0; j < NUM_STUDENTS; ++j)
        {
            all_students[i * NUM_STUDENTS + j] = class_scores[i].students_records[j];
        }
    }

    for (int i = 0; i < NUM_ALL_STUDENTS - 1; i++) {
        for (int j = 0; j < NUM_ALL_STUDENTS - 1 - i; j++) {
            if (all_students[j].avg_score > all_students[j + 1].avg_score) {
                student_record_t tmp = all_students[j];
                all_students[j] = all_students[j + 1];
                all_students[j + 1] = tmp;
            }
        }
    }
}

// 使用选择排序对每个同学的成绩按平均成绩进行班内排序
void select_sort()
{
    for (int i = 0; i < NUM_CLASSES; ++i)
    {
        int max;
        for (int j = 0; j < NUM_STUDENTS - 1; j++)
        {
            max = j;
            for (int k = j + 1; k < NUM_STUDENTS; k++)
            {
                if (class_scores[i].students_records[k].avg_score > class_scores[i].students_records[max].avg_score)
                {
                    max = k;
                }
            }

            if (max != j)
            {
                student_record_t tmp = class_scores[i].students_records[j];
                class_scores[i].students_records[j] = class_scores[i].students_records[max];
                class_scores[i].students_records[max] = tmp;
            }
        }
    }
}

冒泡排序可视化:

img


选择排序可视化:

img


图片引用自:https://zhuanlan.zhihu.com/p/449501682


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 复习CS
{
    class Program
    {
        struct Student
        {
            public int num;
            public string Code;
            public string Name;
            public decimal Score;
        }

        static void Main(string[] args)
        {
            //1、循环添加学生信息
            ArrayList list = new ArrayList();

            for (int i = 1; i < 4; i++)
            {
                Student s = new Student(); //实例化

                Console.Write("请输入第" + i + "个学生的学号:");
                s.Code = Console.ReadLine();
                Console.Write("请输入第" + i + "个学生的姓名:");
                s.Name = Console.ReadLine();
                Console.Write("请输入第" + i + "个学生的成绩:");
                s.Score = Convert.ToDecimal(Console.ReadLine());
                s.num = i;

                list.Add(s);
                Console.WriteLine("===============================");
            }

            Console.WriteLine("-----------------------学生数据展示--------------------------");

            //2、排序

            for (int i = 0; i < list.Count - 1; i++)
            {
                for (int j = i + 1; j < list.Count; j++)
                {
                    Student s1 = (Student)list[i];
                    Student s2 = (Student)list[j];

                    if (s1.Score < s2.Score)
                    {
                        Object ob = list[i];
                        list[i] = list[j];
                        list[j] = ob;
                    }
                }
            }

            //3、打印
            foreach (object o in list)
            {
                Student ss = (Student)o;
                Console.WriteLine("第" + ss.num + "个学生的学号:" + ss.Code + ",姓名:" + ss.Name + ",分数:" + ss.Score + "。");
            }



            Console.ReadKey();
        }
    }
}