C#冒泡排序 用for循环 和 数组做的 您所在的位置:网站首页 for循环排序数组 C#冒泡排序 用for循环 和 数组做的

C#冒泡排序 用for循环 和 数组做的

2024-07-10 09:01| 来源: 网络整理| 查看: 265

1、分析: (1)冒泡排序口诀:N个数字来排队,两两相比小靠前。外层循环N-1,内层循环N-1-i; (2)先设计页面ListBox1和ListBox2,还有Button (3)在load中添加一个数组,然后for循环遍历每一项,循环条件是数组的长度,ListBox1.Items[i]中的每一项添加到shuzu[i]数组中 (4)在点击事件中添加冒泡排序 (5)在里面添加两个for循环嵌套,排序好添加到ListBox2 (6)从小到大排序

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 数组案例_冒泡排序 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //添加数组 int[] shuzu; private void Form1_Load(object sender, EventArgs e) { //把ListBox1.Itmes里面集合的项数获取到 shuzu = new int[listBox1.Items.Count]; for (int i = 0; i < shuzu.Length; i++)//循环出shuzu的长度 { //把遍历的每一项放到数组中 shuzu[i] = Convert.ToInt32(listBox1.Items[i]); } } private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < shuzu.Length-1; i++)//循环出每一项 { for (int j = 0; j < shuzu.Length-1-i; j++) { if (shuzu[j]


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有