今日作业:
实现下列集合的冒泡排序 选择排序 与 插入排序
List list = = new List{33,22,1,7,66,48,95,93,92,91,46};
设计一个Student类 每个学生 都有 语文 数学 英语 成绩,请实现学生平均成绩升序排序
(注:请使用IComparable接口实现)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 冒泡排序 {
class Program {
static void Main (string[] args) {
List<int> intlist = new List<int> ();
Random random = new Random ();
for (int i = 0; i < 20; i++) {
intlist.Add (random.Next (1, 101));
}
ShowListItrms (intlist);
BubbleSort (intlist);
}
//冒泡排序
static void BubbleSort (IList<int> list) {
// for (int i = 0; i < list.Count; i++) {
// for (int j = 0; j < list.Count - 1 - i; j++) {
// if (list[j] > list[j + 1]) {
// int tmp = list[j + 1];
// list[j + 1] = list[j];
// list[j] = tmp;
// }
// }
// }
for (int i = list.Count; i >= 1; i--) {
for (int j = 0; j < i - 1 - i; j++) {
if (list[j] > list[j + 1]) {
int tmp = list[j + 1];
list[j + 1] = list[j];
list[j] = tmp;
}
}
}
System.Console.WriteLine ();
ShowListItrms (list);
}
static void ShowListItrms (IList<int> list) {
foreach (var item in list) {
System.Console.Write ($"{item},");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Say {
class Program {
static void Main (string[] args) {
List<int> intlist = new List<int> ();
intlist.Add (90);
intlist.Add (50);
intlist.Add (30);
intlist.Add (15);
ShowListItrms (intlist);
InsertionSort (intlist);
}
//插入排序
static void InsertionSort (IList<int> list) {
int j, temp;
for (int i = 0; i < list.Count; i++) {
temp = list[i];
j = i;
while (j > 0 && list[j - 1] > temp) {
list[j] = list[j - 1];
j--;
}
list[j] = temp;
}
ShowListItrms (list);
}
static void ShowListItrms (IList<int> list) {
foreach (var item in list) {
System.Console.Write ($"{item},");
}
System.Console.WriteLine ();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Say {
class Program {
static void Main (string[] args) {
List<int> intlist = new List<int> ();
intlist.Add (90);
intlist.Add (50);
intlist.Add (30);
intlist.Add (15);
ShowListItrms (intlist);
InsertionSort (intlist);
}
//插入排序
static void InsertionSort (IList<int> list) {
int j, temp;
for (int i = 0; i < list.Count; i++) {
temp = list[i];
j = i;
while (j > 0 && list[j - 1] > temp) {
list[j] = list[j - 1];
j--;
}
list[j] = temp;
}
ShowListItrms (list);
}
static void ShowListItrms (IList<int> list) {
foreach (var item in list) {
System.Console.Write ($"{item},");
}
System.Console.WriteLine ();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IComparable接口_与_IComparer接口 {
class Program {
static void Main (string[] args) {
List<Student> intlist = new List<Student> { //string 类型需要注意,如果string是动态赋值的,动态赋值的时候不会自拘留池中存在如果是在常量池当中没问题
new Student { Name = "张三", Ianguage = 80, Mathematics = 90, English = 100 },
new Student { Name = "李四", Ianguage = 90, Mathematics = 89, English = 88 },
new Student { Name = "王五", Ianguage = 95, Mathematics = 67, English = 66 },
new Student { Name = "张二", Ianguage = 60, Mathematics = 99, English = 77 },
new Student { Name = "李三", Ianguage = 75, Mathematics = 100, English = 68 },
new Student { Name = "王四", Ianguage = 96, Mathematics = 91, English = 99 },
new Student { Name = "张一", Ianguage = 66, Mathematics = 76, English = 66 },
};
// intlist.Sort ();
// intlist.Sort (new Fruit());
Student[] tmps = new Student[] {
new Student { Name = "李二", Ianguage = 90, Mathematics = 80, English = 90 },
new Student { Name = "王三", Ianguage = 70, Mathematics = 90, English = 90 },
new Student { Name = "刘二", Ianguage = 80, Mathematics = 90, English = 50 },
};
var p = new Student () { Name = "李七", Ianguage = 30, Mathematics = 99, English = 100 };
intlist.AddRange (tmps); //只要实现了IEnumerable接口的集合,就可以同时添加多个
intlist.Sort ();
// intlist.Insert (1, new Fruit () { Name = "橘子", Price = 6 }); //从指定位置插入元素
intlist.Insert (1, p);
intlist.RemoveAt (1); //RemoveAt系列删除相关的各种操作
System.Console.WriteLine (intlist.Contains (p)); //判断是否存在
// intlist.Clear (); //清空所有
displayListItems (intlist);
}
static void displayListItems (IList<Student> list) {
foreach (var item in list) {
System.Console.WriteLine (item);
}
}
}
class Student : IComparable<Student>, IComparer<Student> {
public string Name { get; set; }
public int Ianguage { get; set; }
public int Mathematics { get; set; }
public int English { get; set; }
public int Average { get { return (Ianguage + Mathematics + English) / 3; } }
public int Compare (Student x, Student y) {
return x.Average - y.Average; //调换位置从大到小
}
public int CompareTo (Student obj) {
if (obj == null) return 1;
// if (this.Price == obj.Price) return 0;
// if (this.Price < obj.Price) return -1;
// return 1;//从大到小
return this.Average - obj.Average; //从大到小,兑换位置从小到大
// return Price.CompareTo (obj.Price);//从小到大
}
public override string ToString () {
return $"{this.Name }:{this.Ianguage}分,{this.Mathematics}分,{this.English}分,平均分为{this.Average}";
}
}
}