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

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace BubbleUpSort
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
} private int[] G_int_value;//定义数组字段 private Random G_Random = new Random();//创建随机数对象 private void btn_sort_Click(object sender, EventArgs e)
{
if (G_int_value != null)
{
//定义两个int类型的变量,分别用来表示数组下标和存储新的数组元素
int j, temp;
for (int i = ; i < G_int_value.Length - ; i++)//根据数组下标的值遍历数组元素
{
j = i + ;
id://定义一个标识,以便从这里开始执行语句
if (G_int_value[i] > G_int_value[j])//判断前后两个数的大小
{
temp = G_int_value[i];//将比较后大的元素赋值给定义的int变量
G_int_value[i] = G_int_value[j];//将后一个元素的值赋值给前一个元素
G_int_value[j] = temp;//将int变量中存储的元素值赋值给后一个元素
goto id;//返回标识,继续判断后面的元素
}
else
if (j < G_int_value.Length - )//判断是否执行到最后一个元素
{
j++;//如果没有,则再往后判断
goto id;//返回标识,继续判断后面的元素
}
}
txt_str2.Clear();//清空控件内字符串
foreach (int i in G_int_value)//遍历字符串集合
{
txt_str2.Text += i.ToString() + ", ";//向控件内添加字符串
}
}
else
{
MessageBox.Show("首先应当生成数组,然后再进行排序。", "提示!");
}
} private void btn_Generate_Click(object sender, EventArgs e)
{
G_int_value = new int[G_Random.Next(, )];//生成随机长度数组
for (int i = ; i < G_int_value.Length; i++)//遍历数组
{
G_int_value[i] = G_Random.Next(, );//为数组赋随机数值
}
txt_str.Clear();//清空控件内字符串
foreach (int i in G_int_value)//遍历字符串集合
{
txt_str.Text += i.ToString() + ", ";//向控件内添加字符串 }
}
}
}

设计代码如下:

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(, );
this.btn_sort.Name = "btn_sort";
this.btn_sort.Size = new System.Drawing.Size(, );
this.btn_sort.TabIndex = ;
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(, );
this.btn_Generate.Name = "btn_Generate";
this.btn_Generate.Size = new System.Drawing.Size(, );
this.btn_Generate.TabIndex = ;
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(, );
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(, );
this.groupBox1.TabIndex = ;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "生成随机数组";
//
// txt_str
//
this.txt_str.Location = new System.Drawing.Point(, );
this.txt_str.Multiline = true;
this.txt_str.Name = "txt_str";
this.txt_str.Size = new System.Drawing.Size(, );
this.txt_str.TabIndex = ;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.txt_str2);
this.groupBox2.Controls.Add(this.btn_sort);
this.groupBox2.Location = new System.Drawing.Point(, );
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(, );
this.groupBox2.TabIndex = ;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "排序随机数组";
//
// txt_str2
//
this.txt_str2.Location = new System.Drawing.Point(, );
this.txt_str2.Multiline = true;
this.txt_str2.Name = "txt_str2";
this.txt_str2.Size = new System.Drawing.Size(, );
this.txt_str2.TabIndex = ;
//
// Frm_Main
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
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;
}
}

C#冒泡排序程序的更多相关文章

  1. C#实现随机抽奖和冒泡排序

    随机抽奖程序 string[] s = new string[] { "A", "B", "C", "D", " ...

  2. mpi冒泡排序并行化

    一.实验目的与实验要求 1.实验目的 (1)学会将串行程序改为并行程序. (2)学会mpich2的使用. (3)学会openmp的配置. (4)mpi与openmp之间的比较. 2.实验要求 (1)将 ...

  3. Java基础语法(三)---数组

    一.概念         同一种类型数据的集合.简单的来说就是一容器,用来装东西的. 使用数组的好处:可以自动给数组中的元素从0开始编号,方便操作这些元素. 二.一维数组的格式 格式1:元素类型 [ ...

  4. Java Collections API和泛型

    Java Collections API和泛型 数据结构和算法 学会一门编程语言,你可以写出一些可以工作的代码用计算机来解决一些问题,然而想要优雅而高效的解决问题,就要学习数据结构和算法了.当然对数据 ...

  5. 高射炮打蚊子丨在VS 2017里用C语言写经典的冒泡排序

    ​上一期<高射炮打蚊子丨用Visual Studio 2017写最初级的C语言程序>中,我们用Visual Studio “全宇宙最强IDE”这门大炮,打了“C语言写Hello World ...

  6. 060 01 Android 零基础入门 01 Java基础语法 06 Java一维数组 07 冒泡排序

    060 01 Android 零基础入门 01 Java基础语法 06 Java一维数组 07 冒泡排序 本文知识点:冒泡排序 冒泡排序 实际案例分析冒泡排序流程 第1轮比较: 第1轮比较的结果:把最 ...

  7. 暑假自学java第十一天

    1,使用java.util.Arrays类处理数组 (1 ) public static void sort(数值类型 [ ] a):对指定的数值型数组按数字升序进行排序.在数组排序中设计一个简单的冒 ...

  8. vue源码逐行注释分析+40多m的vue源码程序流程图思维导图 (diff部分待后续更新)

    vue源码业余时间差不多看了一年,以前在网上找帖子,发现很多帖子很零散,都是一部分一部分说,断章的很多,所以自己下定决定一行行看,经过自己坚持与努力,现在基本看完了,差ddf那部分,因为考虑到自己要换 ...

  9. 冒泡,选择,插入,快速排序在Java中的实现

    近几天再重新看数据结构的书时,根据各种排序的空间复杂度,发现快速排序所用时间是最短的,也即是说快速排序的速度最快.因此想验证一下具体这几个排序发的快慢,所以在Java中得以实现,同时在运行时,发现虽然 ...

随机推荐

  1. Java基础笔记(十三)——面向对象

    类和对象 类是具有相同属性和方法的一组对象的集合,好比一个虚拟的轮廓(抽象的概念),确定对象将会拥有的特征(属性)和行为(方法). 对象是类的实例表现,是一个具体的东西(一个看得到.摸得着的具体实体) ...

  2. shell学习(13)- vim

    其中部分内容是转载的. 在命令状态下对当前行用== (连按=两次), 或对多行用n==(n是自然数)表示自动缩进从当前行起的下面n行.你可以试试把代码缩进任意打乱再用n==排版,相当于一般IDE里的c ...

  3. Jmeter 添加CSV Data set config 文件的相对路径及编码在Windows和Linux下的兼容性(转)

    简介: Jmeter实际上是不需要安装的,只需要有ApacheJMeter.jar.启动批处理文件(jmeter.bat或jmeter).配置文件(jmeter.properties.user.pro ...

  4. Luogu P2391 白雪皑皑 && BZOJ 2054: 疯狂的馒头 并查集

    4月的时候在luogu上做过 白雪皑皑 这道题,当时一遍AC可高兴了qwq,后来去了个厕所,路上忽然发现自己的做法是错的qwq...然后就咕咕了qwq 今天看到了 疯狂的馒头 ,发现一毛一样OvO.. ...

  5. 华东交通大学2017年ACM“双基”程序设计竞赛 1009

    Problem Description MDD随机生成了n(n<le5)个随机数x(x<=1e9),这n个随机数排成一个序列,MDD有q(q<=le5)个询问,每个询问给你一个a,问 ...

  6. 09.Spring Bean 注册 - BeanDefinitionRegistry

    基本概念 BeanDefinitionRegistry ,该类的作用主要是向注册表中注册 BeanDefinition 实例,完成 注册的过程. 它的接口定义如下: public interface ...

  7. LeetCode 088 Merge Sorted Array 合并两个有序数组

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You ...

  8. LeetCode 583 Delete Operation for Two Strings 删除两个字符串的不同部分使两个字符串相同,求删除的步数

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  9. Stars(树状数组)

    算法学习:http://www.cnblogs.com/George1994/p/7710886.html 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid ...

  10. FZU Problem 2244 Daxia want to buy house

    模拟题,注意: 1.那两个贷款都是向银行贷的,就是两个贷款的总额不能超过70%,就算公积金贷款能贷也不行,我开始的时候以为公积金贷款是向公司借的,,欺负我这些小白嘛.... 2.最坑的地方 *0.7是 ...