C语言实现英雄联盟英雄信息排序 - 冒泡排序示例
#include <stdio.h>
#include <string.h>
#define MAX_HERO_NUM 10
struct Hero {
char name[20];
char position[10];
int health;
int price;
};
void printHero(struct Hero hero) {
printf("Name: %s\n", hero.name);
printf("Position: %s\n", hero.position);
printf("Health: %d\n", hero.health);
printf("Price: %d\n", hero.price);
printf("\n");
}
void printHeroList(struct Hero heroList[], int length) {
for (int i = 0; i < length; i++) {
printHero(heroList[i]);
}
}
void sortHeroListByPrice(struct Hero heroList[], int length) {
for (int i = 0; i < length - 1; i++) {
for (int j = i + 1; j < length; j++) {
if (heroList[i].price > heroList[j].price) {
struct Hero tempHero = heroList[i];
heroList[i] = heroList[j];
heroList[j] = tempHero;
}
}
}
}
int main() {
struct Hero lolHero[MAX_HERO_NUM] = {
{"Ashe", "ADC", 500, 2600},
{"Thresh", "Support", 600, 4800},
{"Zed", "Assassin", 550, 6300},
{"Garen", "Tank", 700, 4500},
{"Kha'Zix", "Assassin", 600, 6300},
{"Sona", "Support", 500, 3150},
{"Vayne", "ADC", 550, 4800},
{"Darius", "Fighter", 700, 4800},
{"Jinx", "ADC", 550, 6300},
{"Yasuo", "Fighter", 550, 6300}
};
printf("Before sorting:\n");
printHeroList(lolHero, MAX_HERO_NUM);
sortHeroListByPrice(lolHero, MAX_HERO_NUM);
printf("After sorting:\n");
printHeroList(lolHero, MAX_HERO_NUM);
return 0;
}
原文地址: http://www.cveoy.top/t/topic/n0Ry 著作权归作者所有。请勿转载和采集!