经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C#冒泡排序程序
来源:cnblogs  作者:雁过留毛  时间:2018/10/30 9:04:38  对本文有异议

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

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

设计代码如下:

  1. namespace BubbleUpSort
  2. {
  3. partial class Frm_Main
  4. {
  5. /// <summary>
  6. /// 必需的设计器变量。
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9. /// <summary>
  10. /// 清理所有正在使用的资源。
  11. /// </summary>
  12. /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  13. protected override void Dispose(bool disposing)
  14. {
  15. if (disposing && (components != null))
  16. {
  17. components.Dispose();
  18. }
  19. base.Dispose(disposing);
  20. }
  21. #region Windows 窗体设计器生成的代码
  22.  
  23. /// <summary>
  24. /// 设计器支持所需的方法 - 不要
  25. /// 使用代码编辑器修改此方法的内容。
  26. /// </summary>
  27. private void InitializeComponent()
  28. {
  29. this.btn_sort = new System.Windows.Forms.Button();
  30. this.btn_Generate = new System.Windows.Forms.Button();
  31. this.groupBox1 = new System.Windows.Forms.GroupBox();
  32. this.txt_str = new System.Windows.Forms.TextBox();
  33. this.groupBox2 = new System.Windows.Forms.GroupBox();
  34. this.txt_str2 = new System.Windows.Forms.TextBox();
  35. this.groupBox1.SuspendLayout();
  36. this.groupBox2.SuspendLayout();
  37. this.SuspendLayout();
  38. //
  39. // btn_sort
  40. //
  41. this.btn_sort.Location = new System.Drawing.Point(107, 67);
  42. this.btn_sort.Name = "btn_sort";
  43. this.btn_sort.Size = new System.Drawing.Size(86, 23);
  44. this.btn_sort.TabIndex = 0;
  45. this.btn_sort.Text = "冒泡排序";
  46. this.btn_sort.UseVisualStyleBackColor = true;
  47. this.btn_sort.Click += new System.EventHandler(this.btn_sort_Click);
  48. //
  49. // btn_Generate
  50. //
  51. this.btn_Generate.Location = new System.Drawing.Point(107, 70);
  52. this.btn_Generate.Name = "btn_Generate";
  53. this.btn_Generate.Size = new System.Drawing.Size(86, 23);
  54. this.btn_Generate.TabIndex = 1;
  55. this.btn_Generate.Text = "生成随机数组";
  56. this.btn_Generate.UseVisualStyleBackColor = true;
  57. this.btn_Generate.Click += new System.EventHandler(this.btn_Generate_Click);
  58. //
  59. // groupBox1
  60. //
  61. this.groupBox1.Controls.Add(this.txt_str);
  62. this.groupBox1.Controls.Add(this.btn_Generate);
  63. this.groupBox1.Location = new System.Drawing.Point(12, 10);
  64. this.groupBox1.Name = "groupBox1";
  65. this.groupBox1.Size = new System.Drawing.Size(305, 100);
  66. this.groupBox1.TabIndex = 2;
  67. this.groupBox1.TabStop = false;
  68. this.groupBox1.Text = "生成随机数组";
  69. //
  70. // txt_str
  71. //
  72. this.txt_str.Location = new System.Drawing.Point(6, 20);
  73. this.txt_str.Multiline = true;
  74. this.txt_str.Name = "txt_str";
  75. this.txt_str.Size = new System.Drawing.Size(293, 44);
  76. this.txt_str.TabIndex = 4;
  77. //
  78. // groupBox2
  79. //
  80. this.groupBox2.Controls.Add(this.txt_str2);
  81. this.groupBox2.Controls.Add(this.btn_sort);
  82. this.groupBox2.Location = new System.Drawing.Point(12, 116);
  83. this.groupBox2.Name = "groupBox2";
  84. this.groupBox2.Size = new System.Drawing.Size(305, 97);
  85. this.groupBox2.TabIndex = 3;
  86. this.groupBox2.TabStop = false;
  87. this.groupBox2.Text = "排序随机数组";
  88. //
  89. // txt_str2
  90. //
  91. this.txt_str2.Location = new System.Drawing.Point(6, 20);
  92. this.txt_str2.Multiline = true;
  93. this.txt_str2.Name = "txt_str2";
  94. this.txt_str2.Size = new System.Drawing.Size(293, 41);
  95. this.txt_str2.TabIndex = 5;
  96. //
  97. // Frm_Main
  98. //
  99. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  100. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  101. this.ClientSize = new System.Drawing.Size(329, 219);
  102. this.Controls.Add(this.groupBox2);
  103. this.Controls.Add(this.groupBox1);
  104. this.Name = "Frm_Main";
  105. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  106. this.Text = "使用冒泡排序法对一维数组进行排序";
  107. this.groupBox1.ResumeLayout(false);
  108. this.groupBox1.PerformLayout();
  109. this.groupBox2.ResumeLayout(false);
  110. this.groupBox2.PerformLayout();
  111. this.ResumeLayout(false);
  112. }
  113. #endregion
  114.  
  115. private System.Windows.Forms.Button btn_sort;
  116. private System.Windows.Forms.Button btn_Generate;
  117. private System.Windows.Forms.GroupBox groupBox1;
  118. private System.Windows.Forms.GroupBox groupBox2;
  119. private System.Windows.Forms.TextBox txt_str;
  120. private System.Windows.Forms.TextBox txt_str2;
  121. }
  122. }

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号