本文介绍了插入排序C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你们能帮帮我基本插在C#中的排序。我有名字的数组列表和居住的城市,需要通过比较在本市居住的这个数组排序。列表中包含了按照字母顺序进行排序。比较已经成立和运作,我只是有点与插入分拣机编程失去了,因为这是第一次,我们正在做排序的方法。
下面是我'已经到目前为止已经试过:
公共无效插入排序() {。对于(INT I = 0; I<计数;我++) {学生CUR =与会者[I] 为(INT J = 0; J<计数; J ++) {学生SEL =与会者[J]。 如果(cur.CompareTo(SEL)小于0) {学生TEMP =与会者[J]。 与会者[J] =与会者[I] 为(INT K = 1; K>焦耳; K--)与会者[K] =与会者[K - 1]; 与会者[K + 1] =温度; } } } }解决方案
尝试像这样...
公共无效插入排序() $ { b $ b。对于(INT I = 0; I<计数;我++) { INT J =; 虽然(J&0) {学生CUR =与会者[J]。 学生SEL =与会者[J-1]; 如果(cur.CompareTo(SEL)小于0) {学生TEMP = CUR; CUR = SEL; SEL =温度; j-- } ,否则中断; } } }
Can you guys please help me with basic insertion sorting in C#. I have a list of names and city of residence in a array and need to sort this array by comparing the city of residence. List has to be sorted in alphabetical order. Comparator has been set up and works I'm just kinda lost with the insertion sorter programming as this is the first time we are doing that method of sorting.
Here's what I've tried so far:
public void InsertionSort() { for (int i = 0; i < Count; i++) { Student cur = Attendees[i]; for (int j = 0; j < Count; j++) { Student Sel = Attendees[j]; if (cur.CompareTo(Sel) < 0) { Student temp = Attendees[j]; Attendees[j] = Attendees[i]; for (int k = i; k > j; k--) Attendees[k] = Attendees[k - 1]; Attendees[k + 1] = temp; } } } }解决方案
Try like this...
public void InsertionSort() { for (int i = 0; i < Count; i++) { int j = i; While(j > 0) { Student cur = Attendees[j]; Student sel = Attendees[j-1]; if (cur.CompareTo(Sel) < 0) { Student temp = cur; cur = sel; sel = temp; j-- } else break; } } }
更多推荐
插入排序C#
发布评论