插入排序C#

编程入门 行业动态 更新时间:2024-10-12 05:48:36
本文介绍了插入排序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#

本文发布于:2023-11-29 05:50:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645519.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!