当前位置: 移动技术网 > IT编程>开发语言>.net > C#冒泡排序程序

C#冒泡排序程序

2018年10月30日  | 移动技术网IT编程  | 我要评论

透明薄纱,功夫派碧露丸,吕婉柔35分钟

考虑到很多面试可能会考察冒泡排序的用法,所以特地花时间厘清了一下思路。下面说一下我的思路:
冒泡排序核心就是比较方法,冒泡排序的比较方法顾名思义就是像气泡一样,最大(或者最小)的数往上冒。
普通比较几个数,我们可以用if(a>b)然后c=a;b=a 。。。。这类方法,把大数暂存到c中,然后小的数存到
原本的比较小的数继续跟其他数比较。冒泡排序也是如此,不过冒泡排序比较的数据比较多,需要用到for循环,
一个数比较完,比较下一个,一直循环到最后一个,先找出最大的数,然后再找第二大的,以此类推。
实现程序如下:

 1 using system;
 2 using system.collections.generic;
 3 using system.componentmodel;
 4 using system.data;
 5 using system.drawing;
 6 using system.linq;
 7 using system.text;
 8 using system.windows.forms;
 9 
10 namespace bubbleupsort
11 {
12     public partial class frm_main : form
13     {
14         public frm_main()
15         {
16             initializecomponent();
17         }
18 
19         private int[] g_int_value;//定义数组字段
20 
21         private random g_random = new random();//创建随机数对象
22 
23         private void btn_sort_click(object sender, eventargs e)
24         {
25             if (g_int_value != null)
26             {
27                 //定义两个int类型的变量,分别用来表示数组下标和存储新的数组元素
28                 int j, temp;
29                 for (int i = 0; i < g_int_value.length - 1; i++)//根据数组下标的值遍历数组元素
30                 {
31                     j = i + 1;
32                 id://定义一个标识,以便从这里开始执行语句
33                     if (g_int_value[i] > g_int_value[j])//判断前后两个数的大小
34                     {
35                         temp = g_int_value[i];//将比较后大的元素赋值给定义的int变量
36                         g_int_value[i] = g_int_value[j];//将后一个元素的值赋值给前一个元素
37                         g_int_value[j] = temp;//将int变量中存储的元素值赋值给后一个元素
38                         goto id;//返回标识,继续判断后面的元素
39                     }
40                     else
41                         if (j < g_int_value.length - 1)//判断是否执行到最后一个元素
42                         {
43                             j++;//如果没有,则再往后判断 
44                             goto id;//返回标识,继续判断后面的元素
45                         }
46                 }
47                 txt_str2.clear();//清空控件内字符串
48                 foreach (int i in g_int_value)//遍历字符串集合
49                 {
50                     txt_str2.text += i.tostring() + ", ";//向控件内添加字符串
51                 }
52             }
53             else
54             {
55                 messagebox.show("首先应当生成数组,然后再进行排序。", "提示!");
56             }
57         }
58 
59         private void btn_generate_click(object sender, eventargs e)
60         {
61             g_int_value = new int[g_random.next(10, 20)];//生成随机长度数组
62             for (int i = 0; i < g_int_value.length; i++)//遍历数组
63             {
64                 g_int_value[i] = g_random.next(0, 100);//为数组赋随机数值
65             }
66             txt_str.clear();//清空控件内字符串
67             foreach (int i in g_int_value)//遍历字符串集合
68             {
69                 txt_str.text += i.tostring() + ", ";//向控件内添加字符串
70 
71             }
72         }
73     }
74 }

设计代码如下:

namespace bubbleupsort
{
    partial class frm_main
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private system.componentmodel.icontainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.dispose();
            }
            base.dispose(disposing);
        }

        #region windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void initializecomponent()
        {
            this.btn_sort = new system.windows.forms.button();
            this.btn_generate = new system.windows.forms.button();
            this.groupbox1 = new system.windows.forms.groupbox();
            this.txt_str = new system.windows.forms.textbox();
            this.groupbox2 = new system.windows.forms.groupbox();
            this.txt_str2 = new system.windows.forms.textbox();
            this.groupbox1.suspendlayout();
            this.groupbox2.suspendlayout();
            this.suspendlayout();
            // 
            // btn_sort
            // 
            this.btn_sort.location = new system.drawing.point(107, 67);
            this.btn_sort.name = "btn_sort";
            this.btn_sort.size = new system.drawing.size(86, 23);
            this.btn_sort.tabindex = 0;
            this.btn_sort.text = "冒泡排序";
            this.btn_sort.usevisualstylebackcolor = true;
            this.btn_sort.click += new system.eventhandler(this.btn_sort_click);
            // 
            // btn_generate
            // 
            this.btn_generate.location = new system.drawing.point(107, 70);
            this.btn_generate.name = "btn_generate";
            this.btn_generate.size = new system.drawing.size(86, 23);
            this.btn_generate.tabindex = 1;
            this.btn_generate.text = "生成随机数组";
            this.btn_generate.usevisualstylebackcolor = true;
            this.btn_generate.click += new system.eventhandler(this.btn_generate_click);
            // 
            // groupbox1
            // 
            this.groupbox1.controls.add(this.txt_str);
            this.groupbox1.controls.add(this.btn_generate);
            this.groupbox1.location = new system.drawing.point(12, 10);
            this.groupbox1.name = "groupbox1";
            this.groupbox1.size = new system.drawing.size(305, 100);
            this.groupbox1.tabindex = 2;
            this.groupbox1.tabstop = false;
            this.groupbox1.text = "生成随机数组";
            // 
            // txt_str
            // 
            this.txt_str.location = new system.drawing.point(6, 20);
            this.txt_str.multiline = true;
            this.txt_str.name = "txt_str";
            this.txt_str.size = new system.drawing.size(293, 44);
            this.txt_str.tabindex = 4;
            // 
            // groupbox2
            // 
            this.groupbox2.controls.add(this.txt_str2);
            this.groupbox2.controls.add(this.btn_sort);
            this.groupbox2.location = new system.drawing.point(12, 116);
            this.groupbox2.name = "groupbox2";
            this.groupbox2.size = new system.drawing.size(305, 97);
            this.groupbox2.tabindex = 3;
            this.groupbox2.tabstop = false;
            this.groupbox2.text = "排序随机数组";
            // 
            // txt_str2
            // 
            this.txt_str2.location = new system.drawing.point(6, 20);
            this.txt_str2.multiline = true;
            this.txt_str2.name = "txt_str2";
            this.txt_str2.size = new system.drawing.size(293, 41);
            this.txt_str2.tabindex = 5;
            // 
            // frm_main
            // 
            this.autoscaledimensions = new system.drawing.sizef(6f, 12f);
            this.autoscalemode = system.windows.forms.autoscalemode.font;
            this.clientsize = new system.drawing.size(329, 219);
            this.controls.add(this.groupbox2);
            this.controls.add(this.groupbox1);
            this.name = "frm_main";
            this.startposition = system.windows.forms.formstartposition.centerscreen;
            this.text = "使用冒泡排序法对一维数组进行排序";
            this.groupbox1.resumelayout(false);
            this.groupbox1.performlayout();
            this.groupbox2.resumelayout(false);
            this.groupbox2.performlayout();
            this.resumelayout(false);

        }

        #endregion

        private system.windows.forms.button btn_sort;
        private system.windows.forms.button btn_generate;
        private system.windows.forms.groupbox groupbox1;
        private system.windows.forms.groupbox groupbox2;
        private system.windows.forms.textbox txt_str;
        private system.windows.forms.textbox txt_str2;
    }
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网